Skip to content

Instantly share code, notes, and snippets.

@jeffehobbs
Last active November 9, 2018 16:29
Show Gist options
  • Select an option

  • Save jeffehobbs/018fccc0250fe25cb536f38d4d017966 to your computer and use it in GitHub Desktop.

Select an option

Save jeffehobbs/018fccc0250fe25cb536f38d4d017966 to your computer and use it in GitHub Desktop.
Take numerically arranged screenshots with gated timeouts
#!/usr/bin/env python3
# screenshot.py // take screenshots of URLs
import asyncio, argparse, time, os, glob, signal
from time import strftime
from pyppeteer import launch
parser = argparse.ArgumentParser()
parser.add_argument('--site', help='site to screenshot')
args = parser.parse_args()
root = os.path.dirname(os.path.realpath(__file__))
if not os.path.exists('screenshots'):
os.makedirs('screenshots')
async def times_up(signum, frame):
raise Exception("render taking longer than 10 seconds")
async def main():
try:
print("RENDERING " + args.site.upper())
# make the directory
directory = strftime('%Y-%m-%d')
timestamp = strftime('%H-%M')
if not os.path.exists(root + '/screenshots/' + args.site + '/' + directory):
os.makedirs(root+ '/screenshots/' + args.site + '/' + directory)
# get the latest file
folder = root + '/screenshots/' + args.site + '/' + directory + '/'
list_of_files = glob.glob(folder + '*')
try:
latest_file = max(list_of_files, key=os.path.getctime)
latest_file_num = latest_file.replace(folder,'').replace('.png','').replace('.jpg','')
latest_file_num = int(latest_file_num) + 1
new_file = str(latest_file_num)
except:
new_file = 0
# create the save path
path = '/screenshots/' + args.site + '/' + directory + '/' + str(new_file).zfill(4) + '.jpg'
# take the screenshot
start_time = time.time()
browser = await launch()
page = await browser.newPage()
await page.setViewport({'width': 1280, 'height': 1024})
await page.goto('https://' + args.site + '?noads=true', timeout=0)
await page.screenshot({'path': root + path})
await browser.close()
seconds = time.time() - start_time
seconds_round = round(seconds, 2)
print(str(seconds_round) + " seconds")
except:
pass
signal.signal(signal.SIGALRM, times_up)
signal.alarm(10)
asyncio.get_event_loop().run_until_complete(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment