Skip to content

Commit 4f7e3b1

Browse files
committed
Added PagesppedAPI code
1 parent cc673a3 commit 4f7e3b1

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

Python/Pagespeed_API/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Pagespeed_API
2+
3+
This self explanatory script, uses [Pagespeed_API](https://developers.google.com/speed/docs/insights/v5/get-started).
4+
It can be used to -
5+
6+
- Measure the performance of a web page. (Achieved in this Script)
7+
- Improve accessibility, and SEO. (Out of Scope)
8+
9+
## Setup instructions and Guidelines
10+
11+
This setup has only one requirement, `request`. Usually it is auto-downloaded with other packages.
12+
13+
If that's not the case `pip install request` will do the job.
14+
15+
Further more, the script requires a separate `pagespeed.txt.` file (Name can be changed).
16+
This file is supposed to contain a list of urls, which are to be measured using the API.
17+
18+
The script has proper comments which should be followed. The script is easy to use.
19+
20+
## Output
21+
This Snapshot clearly shows the Interactive time of 2 webpages.
22+
23+
![](img/snap.PNG)
24+
25+
## Author(s)
26+
27+
Made by [Vybhav Chaturvedi](https://www.linkedin.com/in/vybhav-chaturvedi-0ba82614a/).
28+
29+
## Disclaimers, if any
30+
31+
Kindly go through this [page](https://nodepit.com/node/com.mmiagency.knime.nodes.google.pagespeed.GooglePageSpeedNodeFactory) and follow the guidelines and rules related to **PageSpeed API**.

Python/Pagespeed_API/img/snap.PNG

27.5 KB
Loading

Python/Pagespeed_API/pagespeed.py

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

Python/Pagespeed_API/pagespeed.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
https://slop.dscdaiict.in/leaderboard
2+
3+
https://github.com/HarshCasper/Rotten-Scripts

0 commit comments

Comments
 (0)