kappa123/kappa123/server/runs.py

48 lines
1.3 KiB
Python

# kappa123/server/runs.py
import os
import requests
import simplejson as json
import arrow
CACHE_LOCATION='/tmp/runs.json'
def inject_run_class(runs, selected_runs):
output = list()
for run in runs:
# munge run
output.append(run)
return output
def fetch_or_cache_runs():
if os.path.isfile(CACHE_LOCATION):
with open(CACHE_LOCATION, 'r') as f:
string = f.read()
return transform_runs(json.loads(string))
else:
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)
def transform_runs(raw_data):
"""Transform a JSON list of GDQ runs from their site in to simplified dict for rendering here."""
output = list()
for run in raw_data:
run = run['fields']
d = {
'time': arrow.get(run['starttime']).to('America/Chicago').format('MMM DD HH:mm:ss'),
'estimate': run['run_time'],
'name': run['name'],
'category': run['category'],
'runners': run['deprecated_runners'], #lol
'order': run['order']
}
output.append(d)
return output