Skip to content

Instantly share code, notes, and snippets.

@AlJohri
Last active October 25, 2018 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlJohri/b2d2abbb751a8b6ec12caf35bb0799fe to your computer and use it in GitHub Desktop.
Save AlJohri/b2d2abbb751a8b6ec12caf35bb0799fe to your computer and use it in GitHub Desktop.
import os
import time
import arrow
import requests
API = "https://projects.fivethirtyeight.com/election-night-api/tests/default/projected_winners.json"
NUM_TIMES = 4
TIME_TO_SLEEP = int(60/NUM_TIMES)
for i in range(NUM_TIMES):
print(f'running iteration {i+1} of {NUM_TIMES}')
timestamp = arrow.utcnow().to('US/Eastern').isoformat()
# get last response
try:
with open('latest.json', 'rb') as f:
last_response = f.read()
except IOError:
last_response = None
# get new response
response = requests.get(API)
# check and save if different
if response.content != last_response:
print(f'response was different, writing file at {timestamp}...')
with open(f'latest.json', 'wb') as f:
f.write(response.content)
with open(f'{timestamp}.json', 'wb') as f:
f.write(response.content)
else:
print("response was the same, not saving")
if i != NUM_TIMES-1:
print(f"sleeping for {TIME_TO_SLEEP}...")
time.sleep(TIME_TO_SLEEP)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment