Updated players cog for new API

This commit is contained in:
Cal Corum 2023-07-03 15:41:36 -05:00
parent a94f24d433
commit af5ca46925
3 changed files with 1146 additions and 1765 deletions

File diff suppressed because it is too large Load Diff

View File

@ -25,7 +25,7 @@ def get_req_url(endpoint: str, api_ver: int = 3, object_id: int = None, params:
return endpoint
req_url = f'{DB_URL}/v{api_ver}/{endpoint}{"/" if object_id is not None else ""}{object_id if object_id is not None else ""}'
if params:
if params is not None:
other_params = False
for x in params:
req_url += f'{param_char(other_params)}{x[0]}={x[1]}'
@ -74,15 +74,16 @@ async def db_get(endpoint: str, api_ver: int = 3, object_id: int = None, params:
raise ValueError(f'DB: {resp.text}')
async def db_patch(endpoint: str, object_id: int, params: list, api_ver: int = 3, timeout: int = 3):
async def db_patch(
endpoint: str, object_id: int, params: list, api_ver: int = 3, timeout: int = 3, payload: dict = None):
req_url = get_req_url(endpoint, api_ver=api_ver, object_id=object_id, params=params)
log_string = f'patch:\n{endpoint} {params}'
log_string = f'patch:\n{endpoint}/{object_id} {params}'
logging.info(log_string) if master_debug else logging.debug(log_string)
retries = 0
while True:
try:
resp = requests.patch(req_url, headers=AUTH_TOKEN, timeout=timeout)
resp = requests.patch(req_url, json=payload, headers=AUTH_TOKEN, timeout=timeout)
break
except requests.Timeout as e:
logging.error(f'Patch Timeout: {req_url} / retries: {retries} / timeout: {timeout}')
@ -167,6 +168,39 @@ async def db_delete(endpoint: str, object_id: int, api_ver: int = 3, timeout=3):
raise ValueError(f'DB: {resp.text}')
async def get_team_by_abbrev(team_abbrev: str, season: int):
t_query = await db_get('teams', params=[('season', season), ('team_abbrev', team_abbrev)])
if t_query['count'] == 0:
return None
else:
return t_query['teams'][0]
async def get_team_by_owner(season, owner_id):
params = [('active_only', True), ('season', season), ('owner_id', owner_id)]
resp = await db_get('teams', params=params)
if resp['count'] > 0:
return resp['teams'][0]
else:
return None
async def get_player_by_name(season: int, player_name: str):
p_query = await db_get('players', params=[('name', player_name), ('season', season)])
if p_query['count'] == 0:
return None
return p_query['players'][0]
async def patch_player(this_player: dict):
this_player['team_id'] = this_player['team']['id']
return await db_patch('players', object_id=this_player['id'], params=[], payload=this_player)
###
# TO BE DEPRECATED FUNCTIONS
###
async def get_current(season=None):
if season is not None:
params = [('season', season)]
@ -219,27 +253,6 @@ async def get_one_team(id_or_abbrev, season=None, is_pd=False, timeout=3):
return await db_get('teams', object_id=id_or_abbrev, params=params)
async def get_team_by_owner(season, owner_id):
params = [('active_only', True), ('season', season), ('owner_id', owner_id)]
resp = await db_get('teams', params=params)
if resp['count'] > 0:
return resp['teams'][0]
else:
return None
async def post_team(team):
req_url = f'{DB_URL}/v1/teams'
payload = json.dumps(team)
resp = requests.post(req_url, payload, headers=AUTH_TOKEN, timeout=3)
if resp.status_code == 200:
return resp.json()
else:
logging.warning(resp.text)
raise ValueError(f'DB: {resp.text}')
async def get_team_roster(team, current_or_next):
resp = requests.get(f'{DB_URL}/v1/teams/{team["id"]}/roster/{current_or_next}', timeout=3)
if resp.status_code == 200:
@ -342,21 +355,6 @@ async def get_one_schedule(
return return_val
async def post_players(all_players: list):
req_url = f'{DB_URL}/v1/players'
data = {
'players': all_players
}
payload = json.dumps(data)
resp = requests.post(req_url, payload, headers=AUTH_TOKEN, timeout=3)
if resp.status_code == 200:
return True
else:
logging.warning(resp.text)
raise ValueError(f'DB: {resp.text}')
async def get_players(season, team_abbrev=None, sort=None, injured=None):
req_url = f'{DB_URL}/v2/players?season={season}'
if team_abbrev:
@ -377,148 +375,148 @@ async def get_one_player(id_or_name, season=None):
return await db_get(req_url)
async def patch_player(
pid, name=None, wara=None, image=None, image2=None, team_id=None, season=None, pitcher_injury=None, pos_1=None,
pos_2=None, pos_3=None, pos_4=None, pos_5=None, pos_6=None, pos_7=None, pos_8=None, last_game=None,
last_game2=None, il_return=None, demotion_week=None, headshot=None, vanity_card=None, injury_rating=None):
"""
For values being patched to "None", set to False
:param pid:
:param name:
:param wara:
:param image:
:param image2:
:param team_id:
:param season:
:param pitcher_injury:
:param pos_1:
:param pos_2:
:param pos_3:
:param pos_4:
:param pos_5:
:param pos_6:
:param pos_7:
:param pos_8:
:param last_game:
:param last_game2:
:param il_return:
:return:
"""
req_url = f'{DB_URL}/v2/players/{pid}'
other_params = False
if name is not None:
req_url += f'{param_char(other_params)}name={name}'
other_params = True
if wara is not None:
req_url += f'{param_char(other_params)}wara={wara}'
other_params = True
if image is not None:
req_url += f'{param_char(other_params)}image={image}'
other_params = True
if image2 is not None:
if not image2:
req_url += f'{param_char(other_params)}image2=False'
else:
req_url += f'{param_char(other_params)}image2={image2}'
other_params = True
if team_id is not None:
req_url += f'{param_char(other_params)}team_id={team_id}'
other_params = True
if season is not None:
req_url += f'{param_char(other_params)}season={season}'
other_params = True
if pitcher_injury is not None:
if not pitcher_injury:
req_url += f'{param_char(other_params)}pitcher_injury=False'
else:
req_url += f'{param_char(other_params)}pitcher_injury={pitcher_injury}'
other_params = True
if pos_1 is not None:
req_url += f'{param_char(other_params)}pos_1={pos_1}'
other_params = True
if pos_2 is not None:
if not pos_2:
req_url += f'{param_char(other_params)}pos_2=False'
else:
req_url += f'{param_char(other_params)}pos_2={pos_2}'
other_params = True
if pos_3 is not None:
if not pos_3:
req_url += f'{param_char(other_params)}pos_3=False'
else:
req_url += f'{param_char(other_params)}pos_3={pos_3}'
other_params = True
if pos_4 is not None:
if not pos_4:
req_url += f'{param_char(other_params)}pos_4=False'
else:
req_url += f'{param_char(other_params)}pos_4={pos_4}'
other_params = True
if pos_5 is not None:
if not pos_5:
req_url += f'{param_char(other_params)}pos_5=False'
else:
req_url += f'{param_char(other_params)}pos_5={pos_5}'
other_params = True
if pos_6 is not None:
if not pos_6:
req_url += f'{param_char(other_params)}pos_6=False'
else:
req_url += f'{param_char(other_params)}pos_6={pos_6}'
other_params = True
if pos_7 is not None:
if not pos_7:
req_url += f'{param_char(other_params)}pos_7=False'
else:
req_url += f'{param_char(other_params)}pos_7={pos_7}'
other_params = True
if pos_8 is not None:
if not pos_8:
req_url += f'{param_char(other_params)}pos_8=False'
else:
req_url += f'{param_char(other_params)}pos_8={pos_8}'
other_params = True
if last_game is not None:
if not last_game:
req_url += f'{param_char(other_params)}last_game=False'
else:
req_url += f'{param_char(other_params)}last_game={last_game}'
other_params = True
if last_game2 is not None:
req_url += f'{param_char(other_params)}last_game2={last_game2}'
other_params = True
if il_return is not None:
if not il_return:
req_url += f'{param_char(other_params)}il_return=false'
else:
req_url += f'{param_char(other_params)}il_return={il_return}'
other_params = True
if demotion_week is not None:
req_url += f'{param_char(other_params)}demotion_week={demotion_week}'
other_params = True
if headshot is not None:
if not headshot:
req_url += f'{param_char(other_params)}headshot=false'
else:
req_url += f'{param_char(other_params)}headshot={headshot}'
other_params = True
if vanity_card is not None:
if not vanity_card:
req_url += f'{param_char(other_params)}vanity_card=false'
else:
req_url += f'{param_char(other_params)}vanity_card={vanity_card}'
other_params = True
if injury_rating is not None:
req_url += f'{param_char(other_params)}injury_rating={injury_rating}'
other_params = True
resp = requests.patch(req_url, headers=AUTH_TOKEN, timeout=3)
if resp.status_code == 200:
return True
else:
logging.warning(resp.text)
raise ValueError(f'DB: {resp.text}')
# async def patch_player(
# pid, name=None, wara=None, image=None, image2=None, team_id=None, season=None, pitcher_injury=None, pos_1=None,
# pos_2=None, pos_3=None, pos_4=None, pos_5=None, pos_6=None, pos_7=None, pos_8=None, last_game=None,
# last_game2=None, il_return=None, demotion_week=None, headshot=None, vanity_card=None, injury_rating=None):
# """
# For values being patched to "None", set to False
#
# :param pid:
# :param name:
# :param wara:
# :param image:
# :param image2:
# :param team_id:
# :param season:
# :param pitcher_injury:
# :param pos_1:
# :param pos_2:
# :param pos_3:
# :param pos_4:
# :param pos_5:
# :param pos_6:
# :param pos_7:
# :param pos_8:
# :param last_game:
# :param last_game2:
# :param il_return:
# :return:
# """
# req_url = f'{DB_URL}/v2/players/{pid}'
# other_params = False
# if name is not None:
# req_url += f'{param_char(other_params)}name={name}'
# other_params = True
# if wara is not None:
# req_url += f'{param_char(other_params)}wara={wara}'
# other_params = True
# if image is not None:
# req_url += f'{param_char(other_params)}image={image}'
# other_params = True
# if image2 is not None:
# if not image2:
# req_url += f'{param_char(other_params)}image2=False'
# else:
# req_url += f'{param_char(other_params)}image2={image2}'
# other_params = True
# if team_id is not None:
# req_url += f'{param_char(other_params)}team_id={team_id}'
# other_params = True
# if season is not None:
# req_url += f'{param_char(other_params)}season={season}'
# other_params = True
# if pitcher_injury is not None:
# if not pitcher_injury:
# req_url += f'{param_char(other_params)}pitcher_injury=False'
# else:
# req_url += f'{param_char(other_params)}pitcher_injury={pitcher_injury}'
# other_params = True
# if pos_1 is not None:
# req_url += f'{param_char(other_params)}pos_1={pos_1}'
# other_params = True
# if pos_2 is not None:
# if not pos_2:
# req_url += f'{param_char(other_params)}pos_2=False'
# else:
# req_url += f'{param_char(other_params)}pos_2={pos_2}'
# other_params = True
# if pos_3 is not None:
# if not pos_3:
# req_url += f'{param_char(other_params)}pos_3=False'
# else:
# req_url += f'{param_char(other_params)}pos_3={pos_3}'
# other_params = True
# if pos_4 is not None:
# if not pos_4:
# req_url += f'{param_char(other_params)}pos_4=False'
# else:
# req_url += f'{param_char(other_params)}pos_4={pos_4}'
# other_params = True
# if pos_5 is not None:
# if not pos_5:
# req_url += f'{param_char(other_params)}pos_5=False'
# else:
# req_url += f'{param_char(other_params)}pos_5={pos_5}'
# other_params = True
# if pos_6 is not None:
# if not pos_6:
# req_url += f'{param_char(other_params)}pos_6=False'
# else:
# req_url += f'{param_char(other_params)}pos_6={pos_6}'
# other_params = True
# if pos_7 is not None:
# if not pos_7:
# req_url += f'{param_char(other_params)}pos_7=False'
# else:
# req_url += f'{param_char(other_params)}pos_7={pos_7}'
# other_params = True
# if pos_8 is not None:
# if not pos_8:
# req_url += f'{param_char(other_params)}pos_8=False'
# else:
# req_url += f'{param_char(other_params)}pos_8={pos_8}'
# other_params = True
# if last_game is not None:
# if not last_game:
# req_url += f'{param_char(other_params)}last_game=False'
# else:
# req_url += f'{param_char(other_params)}last_game={last_game}'
# other_params = True
# if last_game2 is not None:
# req_url += f'{param_char(other_params)}last_game2={last_game2}'
# other_params = True
# if il_return is not None:
# if not il_return:
# req_url += f'{param_char(other_params)}il_return=false'
# else:
# req_url += f'{param_char(other_params)}il_return={il_return}'
# other_params = True
# if demotion_week is not None:
# req_url += f'{param_char(other_params)}demotion_week={demotion_week}'
# other_params = True
# if headshot is not None:
# if not headshot:
# req_url += f'{param_char(other_params)}headshot=false'
# else:
# req_url += f'{param_char(other_params)}headshot={headshot}'
# other_params = True
# if vanity_card is not None:
# if not vanity_card:
# req_url += f'{param_char(other_params)}vanity_card=false'
# else:
# req_url += f'{param_char(other_params)}vanity_card={vanity_card}'
# other_params = True
# if injury_rating is not None:
# req_url += f'{param_char(other_params)}injury_rating={injury_rating}'
# other_params = True
#
# resp = requests.patch(req_url, headers=AUTH_TOKEN, timeout=3)
# if resp.status_code == 200:
# return True
# else:
# logging.warning(resp.text)
# raise ValueError(f'DB: {resp.text}')
async def get_standings(season=None, team_abbrev=None, league_abbrev=None, division_abbrev=None):
@ -540,18 +538,6 @@ async def get_standings(season=None, team_abbrev=None, league_abbrev=None, divis
return await db_get(req_url)
async def post_standings_recalc(season):
req_url = f'{DB_URL}/v1/standings/s{season}/recalculate'
resp = requests.post(req_url, headers=AUTH_TOKEN, timeout=15)
if resp.status_code == 200:
print(f'Done calculating')
return resp.json()
else:
logging.warning(resp.text)
raise ValueError(f'DB: {resp.text}')
async def get_results(season, team_abbrev=None, week=None, away_abbrev=None, home_abbrev=None):
req_url = f'{DB_URL}/v1/results?season={season}'
if team_abbrev:
@ -566,47 +552,6 @@ async def get_results(season, team_abbrev=None, week=None, away_abbrev=None, hom
return await db_get(req_url)
async def post_result(result):
req_url = f'{DB_URL}/v1/results'
payload = json.dumps(result)
resp = requests.post(req_url, payload, headers=AUTH_TOKEN, timeout=3)
if resp.status_code == 200:
return True
else:
logging.warning(resp.text)
raise ValueError(f'DB: {resp.text}')
async def patch_result(result_id, away_score=None, home_score=None):
req_url = f'{DB_URL}/v1/results/{result_id}'
other_params = False
if away_score:
req_url += f'{param_char(other_params)}away_score={away_score}'
other_params = True
if home_score:
req_url += f'{param_char(other_params)}home_score={home_score}'
other_params = True
resp = requests.patch(req_url, headers=AUTH_TOKEN, timeout=3)
if resp.status_code == 200:
return True
else:
logging.warning(resp.text)
raise ValueError(f'DB: {resp.text}')
async def delete_result(result_id):
req_url = f'{DB_URL}/v1/results/{result_id}'
resp = requests.delete(req_url, headers=AUTH_TOKEN, timeout=3)
if resp.status_code == 200:
return True
else:
logging.warning(resp.text)
raise ValueError(f'DB: {resp.text}')
async def get_transactions(
season, team_abbrev=None, week_start=None, week_end=None, cancelled=None, frozen=None,
player_name=None, player_id=None, move_id=None, timeout=3):

View File

@ -2,7 +2,8 @@ import datetime
import pygsheets
from db_calls import *
from db_calls import db_get, db_patch, db_post, db_delete, get_player_headshot
# from db_calls import *
import asyncio
import logging
@ -642,7 +643,7 @@ async def team_emoji(ctx, team):
return emoji
async def fuzzy_player_search(ctx, channel, bot, name, master_list):
async def fuzzy_player_search(ctx, channel, bot, name, master_list, author = None):
"""
Takes a name to search and returns the name of the best match
@ -681,7 +682,7 @@ async def fuzzy_player_search(ctx, channel, bot, name, master_list):
count += 1
embed.set_footer(text='These are the closest matches. Spell better if they\'re not who you want.')
this_q = Question(bot, channel, None, 'int', 45, embed=embed)
resp = await this_q.ask([ctx.author])
resp = await this_q.ask([author])
if not resp:
return None