Handle tracker being down more gracefully.

main
Ryan Rix 2017-07-03 19:09:24 -05:00
parent 438ff5e3e2
commit 4dad65bd90
1 changed files with 21 additions and 11 deletions

View File

@ -12,6 +12,9 @@ CACHE_LOCATION='/tmp/runs.json'
logger = logging.getLogger(__name__)
class GDQException(Exception):
pass
def inject_run_class(runs, selected_runs):
output = list()
for run in runs:
@ -29,22 +32,29 @@ def fetch_or_cache_runs():
if age > CACHE_AGE_MAX:
return fetch_runs()
else:
with open(CACHE_LOCATION, 'r') as f:
string = f.read()
return transform_runs(json.loads(string))
return transform_runs(load_run_cache())
else:
return fetch_runs()
def load_run_cache():
with open(CACHE_LOCATION, 'r') as f:
string = f.read()
return json.loads(string)
def fetch_runs():
logger.warn("Re-fetching cache")
r = requests.get('https://gamesdonequick.com/tracker/search/?type=run&event=20')
if r.status_code != 200:
raise Exception("GDQ not returning 200")
output = r.json()
with open(CACHE_LOCATION, 'w') as f:
f.write(r.text)
return transform_runs(output)
try:
logger.warn("Re-fetching cache")
r = requests.get('https://gamesdonequick.com/tracker/search/?type=run&event=20', timeout=3)
if r.status_code != 200:
raise GDQException("GDQ not returning 200")
output = r.json()
with open(CACHE_LOCATION, 'w') as f:
f.write(r.text)
return transform_runs(output)
except (GDQException, requests.exceptions.ReadTimeout):
return transform_runs(load_run_cache())
def transform_runs(raw_data):
"""Transform a JSON list of GDQ runs from their site in to simplified dict for rendering here."""