move site api for hanab.live into own file
This commit is contained in:
parent
bbbdae0611
commit
cae4a44703
2 changed files with 33 additions and 34 deletions
|
@ -1,40 +1,8 @@
|
||||||
import requests
|
|
||||||
import requests_cache
|
|
||||||
import json
|
import json
|
||||||
|
from site_api import get, api, replay
|
||||||
from sat import COLORS, solve
|
from sat import COLORS, solve
|
||||||
from database import Game, store, load, commit
|
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):
|
def known_solvable(seed):
|
||||||
link = "seed/" + seed
|
link = "seed/" + seed
|
||||||
r = api(link)
|
r = api(link)
|
||||||
|
@ -61,7 +29,6 @@ def solvable(replay):
|
||||||
return solve(deck_str, len(replay["players"]))
|
return solve(deck_str, len(replay["players"]))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
num_entries = 0
|
num_entries = 0
|
||||||
for i in range(0,10000):
|
for i in range(0,10000):
|
||||||
r = api("variants/0?page=" + str(i))
|
r = api("variants/0?page=" + str(i))
|
||||||
|
|
32
site_api.py
Normal file
32
site_api.py
Normal 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))
|
Loading…
Reference in a new issue