Py-Hanabi/site_api.py

37 lines
905 B
Python
Raw Normal View History

import json
import requests
import requests_cache
2023-05-13 23:25:15 +02:00
# Cache all requests to site to reduce traffic and latency
session = requests_cache.CachedSession('hanab.live')
2023-05-13 23:25:15 +02:00
def get(url):
2023-05-13 23:25:15 +02:00
# print("sending request for " + url)
query = "https://hanab.live/" + url
response = session.get(query)
if not response:
raise RuntimeError("Failed to get request {} from hanab.live".format(query))
if not response.status_code == 200:
return None
if "application/json" in response.headers['content-type']:
return json.loads(response.text)
def api(url):
link = "api/v1/" + url
if "?" in url:
link += "&"
else:
link += "?"
link += "size=100"
return get(link)
def replay(seed):
r = api("seed/" + str(seed))
try:
game_id = r['rows'][0]['id']
except TypeError:
return None
return get("export/" + str(game_id))