move site api for hanab.live into own file

This commit is contained in:
Maximilian Keßler 2023-03-02 13:11:06 +01:00
parent bbbdae0611
commit cae4a44703
Signed by: max
GPG key ID: BCC5A619923C0BA5
2 changed files with 33 additions and 34 deletions

View file

@ -1,40 +1,8 @@
import requests
import requests_cache
import json
from site_api import get, api, replay
from sat import COLORS, solve
from database import Game, store, load, commit
session = requests_cache.CachedSession('hanab.live')
def get(url):
# print("sending request for " + url)
response = session.get("https://hanab.live/" + url)
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)
r = api("variants/0")
print(json.dumps(r, indent=4))
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))
def known_solvable(seed):
link = "seed/" + seed
r = api(link)
@ -61,7 +29,6 @@ def solvable(replay):
return solve(deck_str, len(replay["players"]))
num_entries = 0
for i in range(0,10000):
r = api("variants/0?page=" + str(i))

32
site_api.py Normal file
View file

@ -0,0 +1,32 @@
import json
import requests
import requests_cache
## Cache all requests to site to reduce traffic and latency
session = requests_cache.CachedSession('hanab.live')
def get(url):
# print("sending request for " + url)
response = session.get("https://hanab.live/" + url)
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))