]> 91.132.146.200 Git - klimbim.git/commitdiff
simple perl script to get the json from google page speed insights
authorBanana <banana@starscream.de>
Wed, 11 Mar 2020 07:57:42 +0000 (08:57 +0100)
committerBanana <banana@starscream.de>
Wed, 11 Mar 2020 07:57:42 +0000 (08:57 +0100)
perl/google-page-speed-insight.pl [new file with mode: 0644]
perl/mecha.pl

diff --git a/perl/google-page-speed-insight.pl b/perl/google-page-speed-insight.pl
new file mode 100644 (file)
index 0000000..780545c
--- /dev/null
@@ -0,0 +1,75 @@
+#!/usr/bin/perl -w
+
+# 2020 by Johannes 'Banana' Keßler
+# get Google page spedd insight data fpr given URLs
+# https://developers.google.com/speed/pagespeed/insights/
+# https://developers.google.com/speed/docs/insights/v5/reference/pagespeedapi/runpagespeed#response
+
+# retrieves the json and stores it into a file.
+
+use warnings;
+use strict;
+use LWP::UserAgent;
+use HTTP::Request;
+use utf8;
+use JSON;
+use URI::Encode qw(uri_encode uri_decode);
+
+my $localtime = localtime;
+my ($day,$month,$date,$hour,$year) = split /\s+/,scalar localtime;
+
+my @urlsToCheck = (
+       'https://www.some.tld',
+       'https://www.some.tld/with/path',
+       'https://www.some-other.tld'
+);
+
+my $request_headers = [
+  'user-agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/999.99 (KHTML, like Gecko) Chrome/79.0.3945.131 Safari/537.36',
+  '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',
+  'Accept-Charset' => 'iso-8859-1,*,utf-8',
+  'Accept-Language' => 'en-US',
+  'accept-encoding' => 'gzip, deflate, br',
+  'Cache-Control' => 'no-cache',
+];
+
+my $runFilename = $year."-".$month."-".$day.".log";
+open(my $fh, '>>', $runFilename) or die "Could not open file '$runFilename' $!";
+
+print "Getting page speed info from:\n";
+foreach my $url(@urlsToCheck) {
+       print $url."\n";
+       print $fh $url;
+
+       check_url($url);
+       sleep(rand(10));
+}
+
+close $fh;
+
+sub check_url {
+       my ($target) = @_;
+       $target = "https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=".uri_encode($target);
+
+       my $ua = LWP::UserAgent->new;
+       #$ua->proxy(['https','http'], 'http://10.0.0.1:80/'); # proxy if you need one
+    my $req = HTTP::Request->new(GET => $target, $request_headers);
+       my $res = $ua->request($req);
+
+       if ($res->is_success) {
+               my $resultData = $res->decoded_content();
+
+
+               my $jsonData = decode_json($resultData);
+               my $score = $jsonData->{'lighthouseResult'}->{'categories'}->{'performance'}->{'score'};
+               print "Score: ".$score."\n";
+
+               $resultData =~ s/\R//g;
+               print $fh " ".$score." ".$resultData."\n";
+       }
+       else {
+               print $res->code ."\n";
+               print $res->status_line ."\n";
+               print "Something went wrong\n";
+       }
+}
index 2b9458dc55777da6bfca9c206200258d1bf9f2a0..53c57dcf1130e35816384bd252f7a88ecfa24a6f 100644 (file)
@@ -17,16 +17,16 @@ use File::Temp 'tempdir';
 my @timesToCheck = (1..10);
 # the urls to be called
 my @urlsToCheck = (
-       'https://www.some.tld'
+       'https://www.some.tld',
        'https://www.some.tld/with/path',
        'https://www.some-other.tld'
 );
 
 my $mech = WWW::Mechanize::Chrome->new(
-       launch_exe => '/usr/bin/google-chrome-stable', # path to your chome 
+       launch_exe => '/usr/bin/google-chrome-stable', # path to your chome
        incognito => 1,
        data_directory => tempdir(CLEANUP => 1 ),
-       launch_arg => [ 
+       launch_arg => [
                #"--headless", # headless or not
                "--disk-cache-dir=/dev/null",
                "--aggressive-cache-discard",
@@ -41,7 +41,7 @@ my $mech = WWW::Mechanize::Chrome->new(
 $mech->add_header(
        'user-agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/999.99 (KHTML, like Gecko) Chrome/79.0.3945.131 Safari/537.36',
        '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',
-       'Accept-Charset' => 'iso-8859-1,*,utf-8', 
+       'Accept-Charset' => 'iso-8859-1,*,utf-8',
        'Accept-Language' => 'en-US',
        'accept-encoding' => 'gzip, deflate, br',
        'Cache-Control' => 'no-cache',
@@ -59,7 +59,7 @@ foreach my $url(@urlsToCheck) {
                        $time = ($time - $start);
 
                        my $out = sprintf "%s %.4f sec \n", $displayUrl, $time;
-                       print $out;     
+                       print $out;
                }
                $mech->sleep( 2 );
        }