some fixes

This commit is contained in:
joe 2021-01-04 19:19:12 -08:00
parent 56a1b06d28
commit ba70ac4f31
2 changed files with 38 additions and 7 deletions

View File

@ -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

View File

@ -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