google-page-speed-insight.pl 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/perl -w
  2. # Klimbim Software collection, A bag full of things
  3. # Copyright (C) 2011-2023 Johannes 'Banana' Keßler
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. # 2020 by Johannes 'Banana' Keßler
  18. # get Google page spedd insight data fpr given URLs
  19. # https://developers.google.com/speed/pagespeed/insights/
  20. # https://developers.google.com/speed/docs/insights/v5/reference/pagespeedapi/runpagespeed#response
  21. # retrieves the json and stores it into a file.
  22. use warnings;
  23. use strict;
  24. use LWP::UserAgent;
  25. use HTTP::Request;
  26. use utf8;
  27. use JSON;
  28. use URI::Encode qw(uri_encode uri_decode);
  29. my $localtime = localtime;
  30. my ($day,$month,$date,$hour,$year) = split /\s+/,scalar localtime;
  31. my @urlsToCheck = (
  32. 'https://www.some.tld',
  33. 'https://www.some.tld/with/path',
  34. 'https://www.some-other.tld'
  35. );
  36. my $request_headers = [
  37. 'user-agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/999.99 (KHTML, like Gecko) Chrome/79.0.3945.131 Safari/537.36',
  38. 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
  39. 'Accept-Charset' => 'iso-8859-1,*,utf-8',
  40. 'Accept-Language' => 'en-US',
  41. 'accept-encoding' => 'gzip, deflate, br',
  42. 'Cache-Control' => 'no-cache',
  43. ];
  44. my $runFilename = $year."-".$month."-".$day.".log";
  45. open(my $fh, '>>', $runFilename) or die "Could not open file '$runFilename' $!";
  46. print "Getting page speed info from:\n";
  47. foreach my $url(@urlsToCheck) {
  48. print $url."\n";
  49. print $fh $url;
  50. check_url($url);
  51. sleep(rand(10));
  52. }
  53. close $fh;
  54. sub check_url {
  55. my ($target) = @_;
  56. $target = "https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=".uri_encode($target);
  57. my $ua = LWP::UserAgent->new;
  58. #$ua->proxy(['https','http'], 'http://10.0.0.1:80/'); # proxy if you need one
  59. my $req = HTTP::Request->new(GET => $target, $request_headers);
  60. my $res = $ua->request($req);
  61. if ($res->is_success) {
  62. my $resultData = $res->decoded_content();
  63. my $jsonData = decode_json($resultData);
  64. my $score = $jsonData->{'lighthouseResult'}->{'categories'}->{'performance'}->{'score'};
  65. print "Score: ".$score."\n";
  66. $resultData =~ s/\R//g;
  67. print $fh " ".$score." ".$resultData."\n";
  68. }
  69. else {
  70. print $res->code ."\n";
  71. print $res->status_line ."\n";
  72. print "Something went wrong\n";
  73. }
  74. }