import requests import logging import json import os from bs4 import BeautifulSoup import csv AUTH_TOKEN = {'Authorization': f'Bearer {os.environ.get("API_TOKEN")}'} DB_URL = 'http://database/api' def param_char(other_params): if other_params: return '&' else: return '?' def get_req_url(endpoint: str, api_ver: int = 1, object_id: str = None, params: list = None): 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: other_params = False for x in params: req_url += f'{param_char(other_params)}{x[0]}={x[1]}' other_params = True return req_url def db_get(endpoint: str, api_ver: int = 1, object_id: int = None, params: list = None, as_csv: bool = False): req_url = get_req_url(endpoint, api_ver=api_ver, object_id=object_id, params=params) resp = requests.get(req_url) if resp.status_code == 200: if as_csv: return csv.reader(map(bytes.decode, resp.content)) return resp.json() else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def get_current(season=None): req_url = f'http://database/api/v1/current' if season: req_url += f'?season={season}' resp = requests.get(req_url) if resp.status_code == 200: return resp.json() else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def patch_current( week=None, freeze=None, season=None, transcount=None, bstatcount=None, pstatcount=None, bet_week=None, trade_deadline=None, pick_trade_start=None, pick_trade_end=None, injury_count=None): req_url = f'http://database/api/v1/current' other_params = False if week: req_url += f'{param_char(other_params)}week={week}' other_params = True if freeze is not None: req_url += f'{param_char(other_params)}freeze={freeze}' other_params = True if season: req_url += f'{param_char(other_params)}season={season}' other_params = True if transcount: req_url += f'{param_char(other_params)}transcount={transcount}' other_params = True if bstatcount: req_url += f'{param_char(other_params)}bstatcount={bstatcount}' other_params = True if pstatcount: req_url += f'{param_char(other_params)}pstatcount={pstatcount}' other_params = True if bet_week: req_url += f'{param_char(other_params)}bet_week={bet_week}' other_params = True if trade_deadline: req_url += f'{param_char(other_params)}trade_deadline={trade_deadline}' other_params = True if pick_trade_start: req_url += f'{param_char(other_params)}pick_trade_start={pick_trade_start}' other_params = True if pick_trade_end: req_url += f'{param_char(other_params)}pick_trade_end={pick_trade_end}' other_params = True if injury_count: req_url += f'{param_char(other_params)}injury_count={injury_count}' other_params = True resp = requests.patch(req_url, data=None, 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_one_team(id_or_abbrev, season=None, is_pd=False, timeout=3): if is_pd: req_url = f'http://pd-database/api/v1/teams/{id_or_abbrev}' else: req_url = f'http://database/api/v1/teams/{id_or_abbrev}' if season: req_url += f'?season={season}' resp = requests.get(req_url, timeout=timeout) if resp.status_code == 200: return resp.json() else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def get_team_by_owner(season, owner_id): resp = requests.get(f'http://database/api/v1/teams?season={season}&owner_id={owner_id}&active_only=True', timeout=3) if resp.status_code == 200: full_resp = resp.json() if len(full_resp['teams']) != 1: raise ValueError(f'One team requested, but {len(full_resp)} were returned') else: return full_resp['teams'][0] else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def post_team(team): req_url = f'http://database/api/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'http://database/api/v1/teams/{team["id"]}/roster/{current_or_next}', 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_record(team, week_num): resp = requests.get(f'http://database/api/v1/teams/{team["id"]}/record/{week_num}', timeout=3) if resp.status_code == 200: return resp.json() else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def patch_team( team, manager=None, gmid=None, gmid2=None, mascot=None, stadium=None, thumbnail=None, color=None, dice_color=None): req_url = f'http://database/api/v1/teams/{team["id"]}' other_params = False if manager: req_url += f'{param_char(other_params)}manager={manager}' other_params = True if gmid: req_url += f'{param_char(other_params)}gmid={gmid}' other_params = True if gmid2: req_url += f'{param_char(other_params)}gmid2={gmid2}' other_params = True if mascot: req_url += f'{param_char(other_params)}mascot={mascot}' other_params = True if stadium: req_url += f'{param_char(other_params)}stadium={stadium}' other_params = True if thumbnail: req_url += f'{param_char(other_params)}thumbnail={thumbnail}' other_params = True if color: req_url += f'{param_char(other_params)}color={color}' other_params = True if dice_color: req_url += f'{param_char(other_params)}dice_color={dice_color}' other_params = True resp = requests.patch(req_url, data=None, 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_schedule( season, team_abbrev1=None, team_abbrev2=None, away_abbrev=None, home_abbrev=None, week_start=None, week_end=None): req_url = f'http://database/api/v1/schedules?season={season}' if team_abbrev1: req_url += f'&team_abbrev1={team_abbrev1}' if team_abbrev2: req_url += f'&team_abbrev2={team_abbrev2}' if away_abbrev: req_url += f'&away_abbrev={away_abbrev}' if home_abbrev: req_url += f'&home_abbrev={home_abbrev}' if week_start: req_url += f'&week_start={week_start}' if week_end: req_url += f'&week_end={week_end}' resp = requests.get(req_url, timeout=3) if resp.status_code == 200: return resp.json() else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def get_one_schedule( season, team_abbrev1=None, team_abbrev2=None, away_abbrev=None, home_abbrev=None, week=None): req_url = f'http://database/api/v1/schedules?season={season}' if team_abbrev1: req_url += f'&team_abbrev1={team_abbrev1}' if team_abbrev2: req_url += f'&team_abbrev2={team_abbrev2}' if away_abbrev: req_url += f'&away_abbrev={away_abbrev}' if home_abbrev: req_url += f'&home_abbrev={home_abbrev}' if week: req_url += f'&week_start={week}' req_url += f'&week_end={week}' resp = requests.get(req_url, timeout=3) if resp.status_code == 200: full_resp = resp.json() if len(full_resp) > 2: raise ValueError(f'One schedule requested, but {len(full_resp)} were returned') elif len(full_resp) == 0: raise ValueError(f'No schedules found') else: key = [*full_resp] return_val = full_resp[key[0]] return return_val else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def post_players(all_players: list): req_url = f'http://database/api/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'http://database/api/v2/players?season={season}' if team_abbrev: req_url += f'&team_abbrev={team_abbrev}' if sort: req_url += f'&sort={sort}' if injured: req_url += f'&injured={injured}' resp = requests.get(req_url, timeout=3) if resp.status_code == 200: return resp.json() else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def get_one_player(id_or_name, season=None): req_url = f'http://database/api/v1/players/{id_or_name}' if season is not None: req_url += f'?season={season}' resp = requests.get(req_url, timeout=3) if resp.status_code == 200: return resp.json() 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'http://database/api/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): req_url = f'http://database/api/v1/standings' other_params = False if season: req_url += f'{param_char(other_params)}season={season}' other_params = True if team_abbrev: req_url += f'{param_char(other_params)}team_abbrev={team_abbrev}' other_params = True if league_abbrev: req_url += f'{param_char(other_params)}league_abbrev={league_abbrev}' other_params = True if division_abbrev: req_url += f'{param_char(other_params)}division_abbrev={division_abbrev}' other_params = True resp = requests.get(req_url, timeout=3) if resp.status_code == 200: return resp.json() else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def post_standings_recalc(season): req_url = f'http://database/api/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'http://database/api/v1/results?season={season}' if team_abbrev: req_url += f'&team_abbrev={team_abbrev}' if week: req_url += f'&week={week}' if away_abbrev: req_url += f'&away_abbrev={away_abbrev}' if home_abbrev: req_url += f'&home_abbrev={home_abbrev}' resp = requests.get(req_url, timeout=3) if resp.status_code == 200: return resp.json() else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def post_result(result): req_url = f'http://database/api/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'http://database/api/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'http://database/api/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): req_url = f'http://database/api/v1/transactions?season={season}' if team_abbrev: req_url += f'&team_abbrev={team_abbrev}' if week_start: req_url += f'&week_start={week_start}' if week_end: req_url += f'&week_end={week_end}' if cancelled: req_url += f'&cancelled={cancelled}' if frozen: req_url += f'&frozen={frozen}' if player_name: req_url += f'&player_name={player_name}' if player_id: req_url += f'&player_id={player_id}' if move_id: req_url += f'&move_id={move_id}' resp = requests.get(req_url, timeout=timeout) if resp.status_code == 200: return resp.json() else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def post_transactions(moves: list): req_url = f'http://database/api/v1/transactions' data = { 'count': len(moves), 'moves': moves } 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 patch_transaction(move_id, frozen=None, cancelled=None): req_url = f'http://database/api/v1/transactions/{move_id}' other_params = False if frozen is not None: req_url += f'{param_char(other_params)}frozen={frozen}' other_params = True if cancelled is not None: req_url += f'{param_char(other_params)}cancelled={cancelled}' 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_one_battingseason(player_id): req_url = f'http://database/api/v1/battingseasons/{player_id}' resp = requests.get(req_url, timeout=3) if resp.status_code == 200: return resp.json() else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def get_one_pitchingseason(player_id): req_url = f'http://database/api/v1/pitchingseasons/{player_id}' resp = requests.get(req_url, timeout=3) if resp.status_code == 200: return resp.json() else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def get_one_fieldingseason(player_id): req_url = f'http://database/api/v1/fieldingseasons/{player_id}' resp = requests.get(req_url, timeout=3) if resp.status_code == 200: return resp.json() else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def get_battingcareer(name_or_id): req_url = f'http://database/api/v1/battingcareer/{name_or_id}' resp = requests.get(req_url, timeout=3) if resp.status_code == 200: return resp.json() elif resp.status_code == 404: return None else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def get_pitchingcareer(name_or_id): req_url = f'http://database/api/v1/pitchingcareer/{name_or_id}' resp = requests.get(req_url, timeout=3) if resp.status_code == 200: return resp.json() elif resp.status_code == 404: return None else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def get_battingstat( season, s_type='regular', team_abbrev=None, player_name=None, player_id=None, week_start=None, week_end=None, game_num=None, position=None, timeout=10): req_url = f'http://database/api/v1/battingstats/s{season}/{s_type}' other_params = False if team_abbrev: req_url += f'{param_char(other_params)}team_abbrev={team_abbrev}' other_params = True if player_name: req_url += f'{param_char(other_params)}player_name={player_name}' other_params = True if player_id: req_url += f'{param_char(other_params)}player_id={player_id}' other_params = True if week_start: req_url += f'{param_char(other_params)}week_start={week_start}' other_params = True if week_end: req_url += f'{param_char(other_params)}week_end={week_end}' other_params = True if game_num: req_url += f'{param_char(other_params)}game_num={game_num}' other_params = True if position: req_url += f'{param_char(other_params)}position={position}' other_params = True resp = requests.get(req_url, timeout=timeout) if resp.status_code == 200: return resp.json() else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def post_battingstats(stats: list): req_url = f'http://database/api/v1/battingstats' data = { 'count': len(stats), 'stats': stats } 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 delete_battingstats(game_obj: dict): req_url = f'http://database/api/v1/battingstats' payload = json.dumps(game_obj) resp = requests.delete(req_url, data=payload, headers=AUTH_TOKEN) if resp.status_code == 200: return True else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def recalc_batting_seasons(season, team_id): req_url = f'http://database/api/v1/battingseasons/recalculate?season={season}&team_id={team_id}' resp = requests.post(req_url, headers=AUTH_TOKEN, timeout=45) if resp.status_code == 200: return True else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def recalc_fielding_seasons(season, team_id): req_url = f'http://database/api/v1/fieldingseasons/recalculate?season={season}&team_id={team_id}' resp = requests.post(req_url, headers=AUTH_TOKEN, timeout=45) if resp.status_code == 200: return True else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def get_pitchingstat( season, s_type='regular', team_abbrev=None, player_name=None, player_id=None, week_start=None, week_end=None, game_num=None, timeout=10): req_url = f'http://database/api/v1/pitchingstats/s{season}/{s_type}' other_params = False if team_abbrev: req_url += f'{param_char(other_params)}team_abbrev={team_abbrev}' other_params = True if player_name: req_url += f'{param_char(other_params)}player_name={player_name}' other_params = True if player_id: req_url += f'{param_char(other_params)}player_id={player_id}' other_params = True if week_start: req_url += f'{param_char(other_params)}week_start={week_start}' other_params = True if game_num: req_url += f'{param_char(other_params)}game_num={game_num}' other_params = True if week_end: req_url += f'{param_char(other_params)}week_end={week_end}' other_params = True resp = requests.get(req_url, timeout=timeout) if resp.status_code == 200: return resp.json() else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def post_pitchingstats(stats: list): req_url = f'http://database/api/v1/pitchingstats' data = { 'count': len(stats), 'stats': stats } 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 delete_pitchingstats(game_obj: dict): req_url = f'http://database/api/v1/pitchingstats' payload = json.dumps(game_obj) resp = requests.delete(req_url, data=payload, headers=AUTH_TOKEN) if resp.status_code == 200: return True else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def recalc_pitching_seasons(season, team_id): req_url = f'http://database/api/v1/pitchingseasons/recalculate?season={season}&team_id={team_id}' resp = requests.post(req_url, headers=AUTH_TOKEN, timeout=45) if resp.status_code == 200: return True else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def get_draftdata(): resp = requests.get(f'http://database/api/v1/draftdata') if resp.status_code == 200: return resp.json() else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def patch_draftdata( currentpick=None, timer=None, pick_deadline=None, result_channel=None, ping_channel=None, pick_minutes=None): req_url = f'http://database/api/v1/draftdata' other_params = False if currentpick is not None: req_url += f'{param_char(other_params)}currentpick={currentpick}' other_params = True if timer is not None: req_url += f'{param_char(other_params)}timer={timer}' other_params = True if pick_deadline is not None: req_url += f'{param_char(other_params)}pick_deadline={pick_deadline}' other_params = True if result_channel is not None: req_url += f'{param_char(other_params)}result_channel={result_channel}' other_params = True if ping_channel is not None: req_url += f'{param_char(other_params)}ping_channel={ping_channel}' other_params = True if pick_minutes is not None: req_url += f'{param_char(other_params)}pick_minutes={pick_minutes}' 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_draftpicks( season, owner_team=None, orig_owner_team=None, round_start=None, round_end=None, team_season=None, overall_start=None, overall_end=None): req_url = f'http://database/api/v1/draftpicks?season={season}' if owner_team: req_url += f'&owner_team_abbrev={owner_team["abbrev"]}' if orig_owner_team: req_url += f'&orig_team_abbrev={orig_owner_team["abbrev"]}' if round_start: req_url += f'&pick_round_start={round_start}' if round_end: req_url += f'&pick_round_end={round_end}' if team_season: req_url += f'&team_season={team_season}' if overall_start: req_url += f'&overall_start={overall_start}' if overall_end: req_url += f'&overall_end={overall_end}' resp = requests.get(req_url, timeout=3) if resp.status_code == 200: return resp.json() else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def get_one_draftpick_search(season, orig_owner_abbrev, round_num: int, team_season: int = None): req_url = f'http://database/api/v1/draftpicks?season={season}' req_url += f'&orig_team_abbrev={orig_owner_abbrev}' req_url += f'&pick_round_start={round_num}' req_url += f'&pick_round_end={round_num}' if team_season is not None: req_url += f'&team_season={team_season}' resp = requests.get(req_url, timeout=3) if resp.status_code == 200: picks = resp.json() if len(picks) == 0: return None elif len(picks) == 1: for x in picks: return picks[x] else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def get_one_draftpick_byid(pick_id: int): req_url = f'http://database/api/v1/draftpicks/{pick_id}' resp = requests.get(req_url, timeout=3) if resp.status_code == 200: return resp.json() else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def get_one_draftpick_byoverall(season: int, overall: int): req_url = f'http://database/api/v1/draftpicks?season={season}&overall={overall}' resp = requests.get(req_url, timeout=3) if resp.status_code == 200: full_resp = resp.json() if len(full_resp) != 1: return None else: key = [*full_resp] return full_resp[key[0]] else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def patch_draftpick( pick_id: int, overall: int = None, round: int = None, orig_owner_id: int = None, owner_id: int = None, season: int = None, player_id: int = None): req_url = f'http://database/api/v1/draftpicks/{pick_id}' other_params = False if overall is not None: req_url += f'{param_char(other_params)}overall={overall}' other_params = True if round is not None: req_url += f'{param_char(other_params)}round={round}' other_params = True if orig_owner_id is not None: req_url += f'{param_char(other_params)}orig_owner_id={orig_owner_id}' other_params = True if owner_id is not None: req_url += f'{param_char(other_params)}owner_id={owner_id}' other_params = True if season is not None: req_url += f'{param_char(other_params)}season={season}' other_params = True if player_id is not None: req_url += f'{param_char(other_params)}player_id={player_id}' 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_awards(player_id=None, player_name=None, season=None): req_url = f'http://database/api/v1/awards' other_params = False if player_id: req_url += f'{param_char(other_params)}player_id={player_id}' other_params = True if player_name: req_url += f'{param_char(other_params)}player_name={player_name}' other_params = True if season: req_url += f'{param_char(other_params)}season={season}' other_params = True resp = requests.get(req_url, timeout=3) if resp.status_code == 200: return resp.json() else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def post_award(award: dict): req_url = f'http://database/api/v1/awards' award_model = { 'name': award['name'], 'season': award['season'], 'timing': award['timing'], } if award['manager1']: award_model['manager1_id'] = award['manager1']['id'] if award['manager2']: award_model['manager2_id'] = award['manager2']['id'] if award['player']: award_model['player_id'] = award['player']['id'] if award['team']: award_model['team_id'] = award['team']['id'] if award['image']: award_model['image'] = award['image'] payload_prep = {'count': 1, 'awards': [award_model]} payload = json.dumps(payload_prep) 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_one_manager(manager_name): req_url = f'http://database/api/v1/managers/{manager_name}' resp = requests.get(req_url, timeout=3) if resp.status_code == 200: return resp.json() else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') async def post_dice(dice_bag: list): req_url = f'http://database/api/v1/dice' dice_model = { 'count': len(dice_bag), 'rolls': dice_bag } payload = json.dumps(dice_model) 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_player_photo(player_name): req_url = f'https://www.thesportsdb.com/api/v1/json/1/searchplayers.php?p={player_name}' try: resp = requests.get(req_url, timeout=.5) except Exception as e: return None if resp.status_code == 200 and resp.json()['player']: if resp.json()['player'][0]['strSport'] == 'Baseball': return resp.json()['player'][0]['strThumb'] return None async def get_player_headshot(player_name): req_url = f'https://www.baseball-reference.com/search/search.fcgi?search={player_name}' try: resp = requests.get(req_url, timeout=1).text except Exception as e: return None soup = BeautifulSoup(resp, 'html.parser') for item in soup.find_all('img'): if 'headshot' in item['src']: return item['src'] return await get_player_photo(player_name) async def post_draft_list(this_list: list): req_url = f'http://database/api/v1/draft-list' list_model = { 'count': len(this_list), 'draft_list': this_list } payload = json.dumps(list_model) 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_draft_list(team): req_url = f'http://database/api/v1/draft-list/{team["id"]}' resp = requests.get(req_url, headers=AUTH_TOKEN, timeout=3) if resp.status_code == 200: return resp.json() else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}')