From be5bc1198aad6402ab64579a85be7056dc0ea72d Mon Sep 17 00:00:00 2001 From: Banana Date: Wed, 11 Mar 2020 08:57:42 +0100 Subject: [PATCH] simple perl script to get the json from google page speed insights --- perl/google-page-speed-insight.pl | 75 +++++++++++++++++++++++++++++++ perl/mecha.pl | 10 ++--- 2 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 perl/google-page-speed-insight.pl diff --git a/perl/google-page-speed-insight.pl b/perl/google-page-speed-insight.pl new file mode 100644 index 0000000..780545c --- /dev/null +++ b/perl/google-page-speed-insight.pl @@ -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"; + } +} diff --git a/perl/mecha.pl b/perl/mecha.pl index 2b9458d..53c57dc 100644 --- a/perl/mecha.pl +++ b/perl/mecha.pl @@ -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 ); } -- 2.39.5