Skip to content

Commit

Permalink
Python3 version
Browse files Browse the repository at this point in the history
  • Loading branch information
pdwarkanath authored Jul 22, 2019
1 parent 000cc9d commit 0113b98
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions websiteScreenshot.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
import urllib2
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&strategy=mobile&url=" + urllib2.quote(site)
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(urllib2.urlopen(api))
except urllib2.URLError:
print "Unable to retreive data"
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()
print("Invalid JSON encountered.")
sys.exit()

# Google has a weird way of encoding the Base64 data
screenshot_encoded = screenshot_encoded.replace("_", "/")
Expand All @@ -29,6 +35,8 @@
# Decode the Base64 data
screenshot_decoded = base64.b64decode(screenshot_encoded)


# Save the file
with open('screenshot.jpg', 'w') as file_:
file_.write(screenshot_decoded)
with open('screenshot.jpg', 'wb+') as file_:
file_.write(screenshot_decoded, )

0 comments on commit 0113b98

Please sign in to comment.