2020-12-20 00:08:09 +00:00
|
|
|
#interfaces with onomancer
|
|
|
|
|
2020-12-20 06:31:48 +00:00
|
|
|
import requests, json, urllib
|
2021-01-05 03:19:12 +00:00
|
|
|
from requests.adapters import HTTPAdapter
|
|
|
|
from requests.packages.urllib3.util.retry import Retry
|
2020-12-20 01:26:05 +00:00
|
|
|
import database as db
|
|
|
|
|
2020-12-20 00:08:09 +00:00
|
|
|
|
|
|
|
onomancer_url = "https://onomancer.sibr.dev/api/"
|
2020-12-29 21:10:57 +00:00
|
|
|
name_stats_hook = "getOrGenerateStats?name="
|
2020-12-28 09:09:02 +00:00
|
|
|
collection_hook = "getCollection?token="
|
2021-01-03 23:19:48 +00:00
|
|
|
names_hook = "getNames"
|
2020-12-20 00:08:09 +00:00
|
|
|
|
2021-01-05 03:19:12 +00:00
|
|
|
|
|
|
|
def _retry_session(retries=3, backoff=0.3, status=(500, 501, 502, 503, 504)):
|
|
|
|
session = requests.Session()
|
|
|
|
retry = Retry(
|
|
|
|
total=retries,
|
|
|
|
read=retries,
|
|
|
|
connect=retries,
|
|
|
|
backoff_factor=backoff,
|
|
|
|
status_forcelist=status,
|
|
|
|
)
|
|
|
|
adapter = HTTPAdapter(max_retries=retry)
|
|
|
|
session.mount('https://', adapter)
|
|
|
|
return session
|
|
|
|
|
2020-12-20 12:25:25 +00:00
|
|
|
def get_stats(name):
|
2020-12-23 01:20:58 +00:00
|
|
|
player = db.get_stats(name)
|
|
|
|
if player is not None:
|
|
|
|
return player #returns json_string
|
|
|
|
|
2020-12-20 12:25:25 +00:00
|
|
|
#yell at onomancer if not in cache or too old
|
2021-01-05 03:19:12 +00:00
|
|
|
response = _retry_session().get(onomancer_url + name_stats_hook + urllib.parse.quote_plus(name))
|
2020-12-23 01:20:58 +00:00
|
|
|
if response.status_code == 200:
|
|
|
|
stats = json.dumps(response.json())
|
|
|
|
db.cache_stats(name, stats)
|
|
|
|
return stats
|
2020-12-20 12:25:25 +00:00
|
|
|
|
|
|
|
def get_scream(username):
|
2020-12-20 03:14:45 +00:00
|
|
|
scream = db.get_soulscream(username)
|
|
|
|
if scream is not None:
|
|
|
|
return scream
|
2020-12-20 12:25:25 +00:00
|
|
|
else:
|
2020-12-23 23:02:55 +00:00
|
|
|
scream = json.loads(get_stats(username))["soulscream"]
|
2020-12-20 12:25:25 +00:00
|
|
|
db.cache_soulscream(username, scream)
|
2020-12-28 09:09:02 +00:00
|
|
|
return scream
|
|
|
|
|
|
|
|
def get_collection(collection_url):
|
2021-01-05 03:19:12 +00:00
|
|
|
response = _retry_session().get(onomancer_url + collection_hook + urllib.parse.quote(collection_url))
|
2020-12-28 09:09:02 +00:00
|
|
|
if response.status_code == 200:
|
2020-12-28 15:27:20 +00:00
|
|
|
for player in response.json()['lineup'] + response.json()['rotation']:
|
|
|
|
db.cache_stats(player['name'], json.dumps(player))
|
|
|
|
|
2020-12-29 21:10:57 +00:00
|
|
|
return json.dumps(response.json())
|
2021-01-03 23:19:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_names(limit=20, threshold=1):
|
|
|
|
"""
|
|
|
|
Get `limit` random players that have at least `threshold` upvotes.
|
|
|
|
Returns dictionary keyed by player name of stats.
|
|
|
|
"""
|
2021-01-05 03:19:12 +00:00
|
|
|
response = _retry_session().get(
|
2021-01-03 23:19:48 +00:00
|
|
|
onomancer_url + names_hook,
|
|
|
|
params={
|
|
|
|
'limit': limit,
|
|
|
|
'threshold': threshold,
|
|
|
|
'with_stats': 1,
|
|
|
|
'random': 1,
|
|
|
|
},
|
|
|
|
)
|
2021-01-05 03:19:12 +00:00
|
|
|
response.raise_for_status()
|
|
|
|
res = {}
|
|
|
|
for stats in response.json():
|
|
|
|
name = stats['name']
|
|
|
|
db.cache_stats(name, json.dumps(stats))
|
|
|
|
res[name] = stats
|
|
|
|
return res
|