-
Notifications
You must be signed in to change notification settings - Fork 501
Expand file tree
/
Copy pathpagespeed.py
More file actions
62 lines (56 loc) · 2.17 KB
/
pagespeed.py
File metadata and controls
62 lines (56 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import requests
# Change the file name as per requirements.
#
with open("pagespeed.txt") as pagespeedurls:
download_dir = "pagespeed-results.csv"
# Start writing the CSV file
file = open(download_dir, "w")
content = pagespeedurls.readlines()
content = [line.rstrip("\n") for line in content]
# Populate Columns
columnTitleRow = "URL, First Contentful Paint, First Interactive\n"
file.write(columnTitleRow)
# For loop for reading separate URLs from text file.
for line in content:
# Merging the Url from text file in order to successfully request from API
# Strategy is an optional argument
# It can be-
"""
"desktop": Fetch and analyze the URL for desktop browsers
"mobile": Fetch and analyze the URL for mobile devices
"""
api_url = f"https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url={line}&strategy=desktop"
print(f"Requesting {api_url}...")
r = requests.get(api_url)
final = r.json()
try:
url_ID = final["id"]
# This splits the absolute url from the api key parameter
split = url_ID.split("?")
# This reassigns url_ID to the absolute url
url_ID = split[0]
ID = f"URL ~ {url_ID}"
ID2 = str(url_ID)
urlfcp = final["lighthouseResult"]["audits"]["first-contentful-paint"][
"displayValue"
]
FCP = f"First Contentful Paint ~ {str(urlfcp)}"
FCP2 = str(urlfcp)
urlfi = final["lighthouseResult"]["audits"]["interactive"]["displayValue"]
FI = f"First Interactive ~ {str(urlfi)}"
FI2 = str(urlfi)
except KeyError:
print(f"<KeyError> One or more keys not found {line}.")
try:
row = f"{ID2},{FCP2},{FI2}\n"
file.write(row)
except NameError:
print(f"NameError in {line}.")
file.write(f"Failing because of inconsistent key in {line}." + "\n")
try:
print(ID)
print(FCP)
print(FI)
except NameError:
print(f"KeyError in {line}.")
file.close()