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