I'm using the Google Pagespeed API v4 to extract Pagespeed scores for a large number of URLs. I want to test for both desktop and mobile with the argument strategy
within my Pagespeed call. This is possible in the general API call, but I can't find a way to pass these arguments in the batch call.
Minimal working example:
from apiclient.discovery import build
import requests
#define Google API key and API call
google_api_key = "MyKey"
#build service object to call PageSpeed, version 4, with MyKey
ps_service = build('pagespeedonline', 'v4', developerKey = google_api_key)
list_of_urls = [a list of several URLs]
#define function; make list of URLs with column for PageSpeed score
def list_websites(request_id, response, exception):
if exception is not None:
print("This is an exception: " + str(exception) + " " + request_id)
else:
score = response['ruleGroups']['SPEED']['score']
print(score)
#create URL batch
ps_batch = ps_service.new_batch_http_request(callback = list_websites)
service_list = []
for url in list_of_urls:
service_list.append(ps_service.pagespeedapi().runpagespeed(url = url))
for req in service_list:
ps_batch.add(req)
#execute API call by batch
ps_batch.execute()
Although this method works fine for doing batch requests to the API, by default it calculates a speed score based on a desktop view of the said URLs (the analysis strategy is set to desktop), whereas I would also like to receive a score for a batch request in which strategy is set to mobile, to get a score based on the mobile view of the URLs.
My question is, how do I add an extra argument to the build() function in which I can make a distinction between mobile and desktop?
Like this:
for url in list_of_urls:
service_list.append(ps_service.pagespeedapi().runpagespeed(url = url, strategy='mobile'))
Every parameter, used by google can be sent that way