From ba70ac4f313c5cc85b18071ac4ac791f4fe3fd22 Mon Sep 17 00:00:00 2001 From: joe Date: Mon, 4 Jan 2021 19:19:12 -0800 Subject: [PATCH] some fixes --- onomancer.py | 30 ++++++++++++++++++++++++++---- the_prestige.py | 15 ++++++++++++--- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/onomancer.py b/onomancer.py index 2031ec9..278d128 100644 --- a/onomancer.py +++ b/onomancer.py @@ -1,6 +1,8 @@ #interfaces with onomancer import requests, json, urllib +from requests.adapters import HTTPAdapter +from requests.packages.urllib3.util.retry import Retry import database as db @@ -9,13 +11,27 @@ name_stats_hook = "getOrGenerateStats?name=" collection_hook = "getCollection?token=" names_hook = "getNames" + +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 + def get_stats(name): player = db.get_stats(name) if player is not None: return player #returns json_string #yell at onomancer if not in cache or too old - response = requests.get(onomancer_url + name_stats_hook + urllib.parse.quote_plus(name)) + response = _retry_session().get(onomancer_url + name_stats_hook + urllib.parse.quote_plus(name)) if response.status_code == 200: stats = json.dumps(response.json()) db.cache_stats(name, stats) @@ -31,7 +47,7 @@ def get_scream(username): return scream def get_collection(collection_url): - response = requests.get(onomancer_url + collection_hook + urllib.parse.quote(collection_url)) + response = _retry_session().get(onomancer_url + collection_hook + urllib.parse.quote(collection_url)) if response.status_code == 200: for player in response.json()['lineup'] + response.json()['rotation']: db.cache_stats(player['name'], json.dumps(player)) @@ -44,7 +60,7 @@ 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. """ - response = requests.get( + response = _retry_session().get( onomancer_url + names_hook, params={ 'limit': limit, @@ -53,4 +69,10 @@ def get_names(limit=20, threshold=1): 'random': 1, }, ) - return {p['name']: p for p in response.json()} + 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 diff --git a/the_prestige.py b/the_prestige.py index 654614d..5f0e424 100644 --- a/the_prestige.py +++ b/the_prestige.py @@ -603,7 +603,7 @@ top of the list with each mention, teamname, and slogan on a new line (shift+ent - 20 players will be available for draft at a time, and the pool will refresh automatically when it becomes small. - Each participant will be asked to draft 12 hitters then finally one pitcher. - The draft will start only once every participant has given a 👍 to begin. - - use the command `m;draft` on your turn to draft someone + - use the command `d`, `draft`, or `m;draft` on your turn to draft someone """ async def execute(self, msg, command): @@ -615,11 +615,18 @@ top of the list with each mention, teamname, and slogan on a new line (shift+ent raise ValueError('Invalid length') for i in range(0, len(content), 3): - handle = content[i].strip() + handle_token = content[i].strip() + for mention in mentions: + if mention in handle_token: + handle = mention + break + else: + await msg.channel.send(f"I don't recognize {handle_token}") + return team_name = content[i + 1].strip() if games.get_team(team_name): await msg.channel.send(f'Sorry {handle}, {team_name} already exists') - raise ValueError('Existing team') + return slogan = content[i + 2].strip() draft.add_participant(handle, team_name, slogan) @@ -681,6 +688,8 @@ top of the list with each mention, teamname, and slogan on a new line (shift+ent def check(m): if m.channel != channel: return False + if m.content.startswith('d') or m.content.startswith('draft'): + return True for prefix in config()['prefix']: if m.content.startswith(prefix + 'draft'): return True