Skip to content

Instantly share code, notes, and snippets.

@sreevardhanreddi
Created February 9, 2019 12:48
Show Gist options
  • Save sreevardhanreddi/16c1bbfbef574c6d7ba33f087e696363 to your computer and use it in GitHub Desktop.
Save sreevardhanreddi/16c1bbfbef574c6d7ba33f087e696363 to your computer and use it in GitHub Desktop.
import requests
import re
api_key = 'AIzaSyDb3Rl__2TZ1uo-P9BBTZo29FGQ7OlvdLM'
website = input('enter a website with format "http://www.example.com" : ')
regex = re.compile(
r'^(?:http)s?://' # http:// or https://
# domain...
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|'
r'localhost|' # localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
while True:
if re.match(regex, website):
break
else:
website = input('enter a valid website : ')
url_mobile = 'https://www.googleapis.com/pagespeedonline/v5/runPagespeed?key={}&locale=en_US&url={}%2F&strategy=mobile'.format(
api_key, website)
url_desktop = 'https://www.googleapis.com/pagespeedonline/v5/runPagespeed?key={}&locale=en_US&url={}%2F&strategy=desktop'.format(
api_key, website)
response_mobile = requests.get(url_mobile)
response_desktop = requests.get(url_desktop)
# key = key_m = 'lighthouseResult__categories__performance__score'
if response_desktop.status_code == 200:
try:
print('Score for Desktop is {}'.format((response_desktop.json()[
'lighthouseResult']['categories']['performance']['score']) * 100))
except Exception as e:
print('invalid score', e)
else:
print('invalid website or page, check input')
if response_mobile.status_code == 200:
try:
print('Score for Mobile is {}'.format((response_mobile.json()[
'lighthouseResult']['categories']['performance']['score']) * 100))
except Exception as e:
print('invalid score', e)
else:
print('invalid website or page, check input')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment