forked from edent/Python-Twitter-Hacks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsiteScreenshot.py
42 lines (27 loc) · 965 Bytes
/
websiteScreenshot.py
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
import urllib
import json
import base64
import sys
# The website's URL
site = "https://twitter.com/edent"
# The Google API. Remove "&strategy=mobile" for a desktop screenshot
api = "https://www.googleapis.com/pagespeedonline/v1/runPagespeed?screenshot=true&url=" + urllib.parse.quote(site)
# Get the results from Google
try:
site_data = json.load(urllib.request.urlopen(api))
except urllib.error.URLError:
print("Unable to retreive data")
sys.exit()
try:
screenshot_encoded = site_data['screenshot']['data']
except ValueError:
print("Invalid JSON encountered.")
sys.exit()
# Google has a weird way of encoding the Base64 data
screenshot_encoded = screenshot_encoded.replace("_", "/")
screenshot_encoded = screenshot_encoded.replace("-", "+")
# Decode the Base64 data
screenshot_decoded = base64.b64decode(screenshot_encoded)
# Save the file
with open('screenshot.jpg', 'wb+') as file_:
file_.write(screenshot_decoded, )