From 622f49920b8fa123944f741a4deb309125a3b940 Mon Sep 17 00:00:00 2001 From: Cal Corum Date: Mon, 17 Apr 2023 22:40:53 -0500 Subject: [PATCH] Move dice to own instance --- cogs/dice.py | 2 +- cogs/players.py | 260 ++++++++++++++++++++++++++++++++++-------------- db_calls.py | 101 +++++++++---------- helpers.py | 14 ++- 4 files changed, 247 insertions(+), 130 deletions(-) diff --git a/cogs/dice.py b/cogs/dice.py index 0087806..866ab00 100644 --- a/cogs/dice.py +++ b/cogs/dice.py @@ -24,7 +24,7 @@ class Dice(commands.Cog): try: team_abbrev = re.split('-', channel.name) if len(team_abbrev[0]) <= 4 and team_abbrev not in ['the', 'city']: - team = await get_one_team(team_abbrev[0], timeout=0.5) + team = await get_one_team(team_abbrev[0], timeout=1) else: team = None except (ValueError, AttributeError, requests.ReadTimeout) as e: diff --git a/cogs/players.py b/cogs/players.py index 735859f..90e9dfa 100644 --- a/cogs/players.py +++ b/cogs/players.py @@ -6,6 +6,7 @@ import re from discord import app_commands from discord.ext import tasks +from discord.app_commands import Choice from db_calls_gameplay import get_one_game from helpers import * @@ -94,6 +95,31 @@ class Players(commands.Cog): async def slash_error(self, ctx, error): await ctx.send(f'{error}') + async def get_dice_embed(self, channel, title, message): + try: + team_abbrev = re.split('-', channel.name) + if len(team_abbrev[0]) <= 4 and team_abbrev not in ['the', 'city']: + team = await get_one_team(team_abbrev[0], timeout=1) + else: + team = None + except (ValueError, AttributeError, requests.ReadTimeout) as e: + logging.info(f'{type(e)}: {e}') + team = None + + if team: + embed = discord.Embed( + color=int(team["dice_color"], 16) if team["dice_color"] else int(team["color"], 16) + ) + else: + embed = discord.Embed( + color=int('0x000000', 16) + ) + + if title and message: + embed.add_field(name=title, value=message) + + return embed + @tasks.loop(count=1) async def build_master_player_list(self): guild = self.bot.get_guild(int(os.environ.get('GUILD_ID'))) @@ -934,6 +960,7 @@ class Players(commands.Cog): @app_commands.describe(player_name='Name of injured player') @app_commands.checks.has_any_role(SBA_PLAYERS_ROLE_NAME) async def clear_injury_slash(self, interaction: discord.Interaction, player_name: str): + await interaction.response.defer() current = await get_current() player = await get_one_player(player_name) @@ -950,14 +977,22 @@ class Players(commands.Cog): return old_injury = player['il_return'] - await interaction.response.send_message(f'{player["name"]}\'s return was set for {player["il_return"]}.') + await interaction.edit_original_response( + content=f'{player["name"]}\'s return was set for {player["il_return"]}.' + ) view = Confirm(responders=[interaction.user]) - question = await interaction.channel.send('Is he eligible to play again?', view=view) + await interaction.edit_original_response( + content='Is he eligible to play again?', + view=view + ) await view.wait() if view.value: player['il_return'] = None - await question.edit(content='Okay. Working on it...', view=None) + await interaction.edit_original_response( + content='Okay. Working on it...', + view=None + ) if await patch_player(player['id'], il_return=False): await patch_current(injury_count=current['injury_count'] + 1) embed = get_team_embed(f'Injury Update', team=team) @@ -969,85 +1004,19 @@ class Players(commands.Cog): await log_injury( current, {'player': player, 'type': 'clear', 'current_game': None, 'injury_length': None} ) - await question.edit(content=random_conf_gif(), view=None) + await interaction.edit_original_response( + content=random_conf_gif(), + view=None + ) await send_to_channel(self.bot, 'sba-network-news', content=None, embed=embed) await self.update_injuries(interaction) else: await interaction.response.message_update('Well that didn\'t work.') else: - await question.edit(content='You keep thinking on it.', view=None) - - # @commands.command(name='result', help='Log result') - # @commands.has_any_role(SBA_PLAYERS_ROLE_NAME) - # async def result_command( - # self, ctx, away_abbrev: str, away_score: int, home_abbrev: str, home_score: int, *week: int): - # # current = await get_current() - # # away_team = await get_one_team(away_abbrev) - # # home_team = await get_one_team(home_abbrev) - # # this_week = week[0] if week else current['week'] - # # - # # this_matchup = await get_schedule(current['season'], week_start=this_week, week_end=this_week, - # # away_abbrev=away_team["abbrev"], home_abbrev=home_team["abbrev"]) - # # these_results = await get_results(current['season'], week=this_week, away_abbrev=away_team["abbrev"], - # # home_abbrev=home_team["abbrev"]) - # # - # # # Check if scores match a weekly matchup - # # game_count = 0 - # # legal = False - # # for x in this_matchup: - # # if this_matchup[x]['awayteam'] == away_team and this_matchup[x]['hometeam'] == home_team: - # # legal = True - # # game_count = this_matchup[x]['gamecount'] - # # if not legal: - # # raise ValueError(f'I do not see a matchup between {away_team["abbrev"]} and {home_team["abbrev"]} ' - # # f'in week {this_week}') - # # - # # # Check if author is gm of these teams or the bot owner - # # if ctx.author.id not in [away_team['gmid'], away_team['gmid2'], home_team['gmid'], home_team['gmid2']] \ - # # and ctx.author.id != self.bot.owner_id: - # # await ctx.message.add_reaction('❌') - # # raise PermissionError('Only GMs of these teams can enter results') - # # - # # if len(these_results) >= game_count: - # # raise ValueError('It looks like that series is complete') - # # - # # prompt = f'Should I log {away_team["abbrev"]} {await team_emoji(ctx, away_team)} {away_score} @ ' \ - # # f'{home_score} {await team_emoji(ctx, home_team)} {home_team["abbrev"]} ' \ - # # f'as w{this_week}g{len(these_results) + 1}?' - # # this_q = Question(self.bot, ctx.channel, prompt, 'yesno', 45) - # # resp = await this_q.ask([ctx.author]) - # # - # # if not resp: - # # await ctx.send('No worries, I\'ll be here when you\'re ready.') - # # return - # # else: - # # result = { - # # 'week': this_week, - # # 'game': len(these_results) + 1, - # # 'away_team_id': away_team['id'], - # # 'home_team_id': home_team['id'], - # # 'away_score': away_score, - # # 'home_score': home_score, - # # 'season': current['season'] - # # } - # # if await post_result(result): - # # await ctx.message.add_reaction('✅') - # # await ctx.send(random_conf_gif()) - # # update = await ctx.send('I\'m tallying standings now...') - # # if await post_standings_recalc(current['season']): - # # await update.delete() - # await ctx.send(f'You no longer have to submit scores manually. They will be added when you run `/sba-submit`') - - # @commands.command(name='drive', help='Season 5 Drive') - # async def drive_command(self, ctx): - # await ctx.send('Here is a link to the Season 5 drive:\n' - # 'https://drive.google.com/drive/folders/1W8B54gZsDXXT3coKVtnGYNiYnUAPcwNB?usp=sharing') - - # @commands.command(name='rules', aliases=['rulesref', 'rulesreference', 'reference'], help='Rules reference') - # async def rules_command(self, ctx): - # await ctx.send('Here is the online rules reference: \n\n' - # 'Here is a link to the google doc:\n' - # 'https://docs.google.com/document/d/1yGZcHy9zN2MUi4hnce12dAzlFpIApbn7zR24vCkPl1o') + await interaction.edit_original_response( + content='You keep thinking on it.', + view=None + ) @commands.command(name='links', aliases=['scorecards', 'scorecard', 'link', 'resources', 'resource'], help='Links for league resources') @@ -3025,6 +2994,143 @@ class Players(commands.Cog): score_text = this_scorecard.get_value('A1') await interaction.response.send_message(score_text) + @app_commands.command(name='injury', description='Make an injury check; rating = left of "p", games = right') + @app_commands.guilds(discord.Object(id=os.environ.get('GUILD_ID'))) + @app_commands.choices(games=[ + Choice(name='86', value=86), + Choice(name='80', value=80), + Choice(name='70', value=70), + Choice(name='60', value=60), + Choice(name='50', value=50), + Choice(name='40', value=40), + Choice(name='30', value=30), + Choice(name='20', value=20), + ], rating=[ + Choice(name='1', value=1), + Choice(name='2', value=2), + Choice(name='3', value=3), + Choice(name='4', value=4), + Choice(name='5', value=5), + Choice(name='6', value=6), + ]) + async def injury_roll_slash(self, interaction: discord.Interaction, rating: Choice[int], games: Choice[int]): + team = None + await interaction.response.defer() + + d_six_one = random.randint(1, 6) + d_six_two = random.randint(1, 6) + d_six_three = random.randint(1, 6) + injury_roll = d_six_one + d_six_two + d_six_three + + inj_data = { + 'one': { + 'p86': ['OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'REM', 'REM', 1, 1, 2, 2, 3, 3, 4, 5], + 'p80': [2, 2, 'OK', 'REM', 1, 2, 3, 3, 4, 4, 5, 5, 6, 8, 12, 16], + 'p70': ['OK', 'OK', 'REM', 1, 2, 3, 4, 5, 5, 6, 6, 8, 12, 16, 20, 20], + 'p60': ['OK', 'REM', 1, 2, 3, 4, 5, 6, 6, 8, 12, 12, 16, 20, 20, 'OK'], + 'p50': ['OK', 1, 2, 3, 4, 5, 6, 8, 8, 12, 12, 16, 20, 30, 'REM', 'OK'], + 'p40': ['OK', 5, 1, 3, 5, 6, 8, 12, 12, 16, 20, 30, 4, 2, 'REM', 'OK'], + 'p30': ['OK', 1, 2, 5, 6, 8, 12, 16, 20, 30, 12, 8, 4, 3, 'REM', 'OK'], + 'p20': ['OK', 1, 2, 5, 6, 12, 12, 30, 20, 16, 16, 8, 4, 3, 'REM', 'OK'] + }, + 'two': { + 'p86': [4, 3, 2, 2, 1, 1, 'REM', 'OK', 'REM', 'OK', 2, 1, 2, 2, 3, 5], + 'p80': [12, 6, 4, 2, 2, 'OK', 1, 'OK', 'REM', 1, 'REM', 2, 3, 5, 8, 16], + 'p70': [1, 3, 4, 6, 2, 2, 'OK', 1, 3, 'REM', 4, 5, 8, 12, 16, 3], + 'p60': [5, 'OK', 'OK', 'REM', 1, 2, 5, 3, 4, 6, 4, 8, 12, 16, 16, 'OK'], + 'p50': ['OK', 'OK', 'REM', 1, 2, 3, 4, 5, 6, 5, 8, 12, 16, 20, 20, 'OK'], + 'p40': ['OK', 'REM', 1, 2, 3, 4, 5, 6, 8, 6, 12, 16, 20, 30, 'REM', 'OK'], + 'p30': ['OK', 1, 2, 4, 5, 6, 8, 6, 12, 16, 8, 20, 30, 3, 5, 'REM'], + 'p20': ['OK', 1, 4, 5, 6, 6, 8, 8, 16, 12, 20, 30, 12, 3, 2, 'REM'] + }, + 'three': { + 'p86': [], + 'p80': ['OK', 'OK', 'REM', 1, 3, 'OK', 'REM', 1, 2, 1, 2, 3, 4, 5, 6, 'REM'], + 'p70': ['OK', 6, 'OK', 'REM', 1, 2, 2, 3, 4, 5, 1, 3, 6, 8, 12, 'REM'], + 'p60': ['OK', 'OK', 'REM', 1, 2, 3, 4, 5, 6, 4, 5, 8, 12, 12, 16, 'REM'], + 'p50': ['OK', 1, 1, 2, 3, 4, 5, 6, 8, 6, 8, 12, 12, 16, 5, 'REM'], + 'p40': ['OK', 1, 2, 3, 4, 6, 5, 8, 6, 8, 12, 12, 16, 20, 1, 'REM'], + 'p30': ['OK', 2, 3, 4, 5, 8, 6, 8, 12, 12, 16, 6, 20, 30, 1, 'REM'], + 'p20': ['OK', 1, 2, 4, 5, 12, 12, 8, 6, 16, 8, 20, 30, 3, 5, 'REM'] + }, + 'four': { + 'p86': [], + 'p80': [], + 'p70': ['OK', 'OK', 'REM', 3, 3, 'OK', 'REM', 1, 2, 1, 4, 5, 6, 8, 12, 'REM'], + 'p60': ['OK', 8, 4, 'OK', 'REM', 1, 2, 4, 5, 3, 6, 3, 8, 12, 16, 'REM'], + 'p50': ['OK', 'OK', 'REM', 1, 2, 3, 4, 5, 6, 4, 5, 8, 12, 12, 16, 'REM'], + 'p40': ['OK', 1, 1, 2, 3, 4, 5, 6, 8, 6, 8, 12, 12, 16, 5, 'REM'], + 'p30': ['OK', 'REM', 2, 3, 4, 5, 6, 6, 8, 8, 16, 12, 12, 1, 20, 'REM'], + 'p20': ['OK', 1, 2, 3, 4, 6, 5, 8, 6, 8, 16, 12, 12, 20, 1, 'REM'] + }, + 'five': { + 'p86': [], + 'p80': [], + 'p70': [], + 'p60': ['OK', 'OK', 'REM', 1, 1, 'OK', 'REM', 3, 2, 4, 5, 6, 8, 6, 12, 16], + 'p50': ['OK', 8, 8, 'OK', 1, 3, 2, 4, 5, 6, 'REM', 3, 12, 8, 16, 1], + 'p40': ['OK', 'OK', 'REM', 5, 1, 2, 6, 4, 8, 3, 5, 12, 6, 8, 16, 'REM'], + 'p30': ['OK', 'REM', 2, 3, 5, 4, 6, 5, 8, 6, 12, 8, 3, 1, 16, 'REM'], + 'p20': ['OK', 'REM', 2, 3, 5, 4, 6, 5, 8, 6, 12, 8, 12, 1, 16, 'REM'] + }, + 'six': { + 'p86': [], + 'p80': [], + 'p70': [], + 'p60': [], + 'p50': ['OK', 8, 3, 'OK', 1, 3, 2, 4, 5, 6, 'REM', 8, 12, 3, 16, 1], + 'p40': ['OK', 'OK', 'REM', 5, 1, 3, 8, 4, 6, 2, 5, 12, 3, 4, 16, 'REM'], + 'p30': ['OK', 'REM', 4, 5, 2, 3, 6, 4, 8, 5, 12, 6, 3, 1, 16, 'REM'], + 'p20': ['OK', 'REM', 2, 3, 5, 4, 6, 5, 8, 6, 12, 8, 12, 1, 16, 'REM'] + } + } + p_ratings = ['one', 'two', 'three', 'four', 'five', 'six'] + + injury_string = f'```md\n# {injury_roll}\n' \ + f'Details:[3d6 ({d_six_one} {d_six_two} {d_six_three})]\n```\n' + + logging.info(f'injury rating: {rating.value}p{games.value}') + + injury_list = inj_data[p_ratings[rating.value - 1]][f'p{games.value}'] + injury_result = injury_list[injury_roll - 3] + logging.info(f'injury rating: {rating.value}p{games.value} / array: {injury_list}[{injury_roll - 2}] / result: {injury_result}') + + if isinstance(injury_result, int): + await interaction.edit_original_response( + content=random_gif(random_from_list(['salute', 'press f', 'pay respects'])) + ) + injury_string += f'With a roll of {injury_roll}, the injury length is **{injury_result} ' \ + f'game{"s" if injury_result > 1 else ""}**.' + elif injury_result == 'REM': + await interaction.edit_original_response( + content=random_gif(random_from_list(['could be worse', 'not too bad'])) + ) + injury_string += f'With a roll of {injury_roll}, the injury length is **REMAINDER OF GAME** for batters ' \ + f'or **FATIGUED** for pitchers' + else: + await interaction.edit_original_response( + content=random_gif(random_from_list(['it is fine', 'nothing to see here', 'i wasn\'t worried'])) + ) + injury_string += f'With a roll of {injury_roll}, the player is **OKAY** - no injury!' + + embed = await self.get_dice_embed( + interaction.channel.name, + f'Injury roll for {interaction.user.name}', + injury_string + ) + embed.set_footer(text='For pitchers, add their current rest to the injury') + + await interaction.channel.send(content=None, embed=embed) + + # this_roll = { + # 'season': self.current['season'], + # 'week': self.current['week'], + # 'team_id': team["id"] if team else None, + # 'roller': interaction.user.id, + # 'threedsix': d_six_one + d_six_two + d_six_three + # } + # self.rolls.append(this_roll) + async def setup(bot): await bot.add_cog(Players(bot)) diff --git a/db_calls.py b/db_calls.py index d70fbd0..511bdcc 100644 --- a/db_calls.py +++ b/db_calls.py @@ -8,6 +8,7 @@ import csv AUTH_TOKEN = {'Authorization': f'Bearer {os.environ.get("API_TOKEN")}'} DB_URL = 'http://database/api' +# DB_URL = 'https://sombaseball.ddns.net/api' master_debug = True @@ -65,7 +66,7 @@ def db_get(endpoint: str, api_ver: int = 1, object_id: int = None, params: list async def get_current(season=None): - req_url = f'http://database/api/v1/current' + req_url = f'{DB_URL}/v1/current' if season: req_url += f'?season={season}' @@ -80,7 +81,7 @@ async def get_current(season=None): 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' + req_url = f'{DB_URL}/v1/current' other_params = False if week: req_url += f'{param_char(other_params)}week={week}' @@ -128,7 +129,7 @@ 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}' + req_url = f'{DB_URL}/v1/teams/{id_or_abbrev}' if season: req_url += f'?season={season}' @@ -142,7 +143,7 @@ async def get_one_team(id_or_abbrev, season=None, is_pd=False, timeout=3): 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) + resp = requests.get(f'{DB_URL}/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: @@ -155,7 +156,7 @@ async def get_team_by_owner(season, owner_id): async def post_team(team): - req_url = f'http://database/api/v1/teams' + req_url = f'{DB_URL}/v1/teams' payload = json.dumps(team) resp = requests.post(req_url, payload, headers=AUTH_TOKEN, timeout=3) @@ -167,7 +168,7 @@ async def post_team(team): 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) + resp = requests.get(f'{DB_URL}/v1/teams/{team["id"]}/roster/{current_or_next}', timeout=3) if resp.status_code == 200: return resp.json() else: @@ -176,7 +177,7 @@ async def get_team_roster(team, current_or_next): async def get_team_record(team, week_num): - resp = requests.get(f'http://database/api/v1/teams/{team["id"]}/record/{week_num}', timeout=3) + resp = requests.get(f'{DB_URL}/v1/teams/{team["id"]}/record/{week_num}', timeout=3) if resp.status_code == 200: return resp.json() else: @@ -187,7 +188,7 @@ async def get_team_record(team, week_num): 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"]}' + req_url = f'{DB_URL}/v1/teams/{team["id"]}' other_params = False if manager: req_url += f'{param_char(other_params)}manager={manager}' @@ -225,7 +226,7 @@ async def patch_team( 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}' + req_url = f'{DB_URL}/v1/schedules?season={season}' if team_abbrev1: req_url += f'&team_abbrev1={team_abbrev1}' if team_abbrev2: @@ -249,7 +250,7 @@ async def get_schedule( 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}' + req_url = f'{DB_URL}/v1/schedules?season={season}' if team_abbrev1: req_url += f'&team_abbrev1={team_abbrev1}' if team_abbrev2: @@ -279,7 +280,7 @@ async def get_one_schedule( async def post_players(all_players: list): - req_url = f'http://database/api/v1/players' + req_url = f'{DB_URL}/v1/players' data = { 'players': all_players } @@ -294,7 +295,7 @@ async def post_players(all_players: list): async def get_players(season, team_abbrev=None, sort=None, injured=None): - req_url = f'http://database/api/v2/players?season={season}' + req_url = f'{DB_URL}/v2/players?season={season}' if team_abbrev: req_url += f'&team_abbrev={team_abbrev}' if sort: @@ -311,7 +312,7 @@ async def get_players(season, team_abbrev=None, sort=None, injured=None): async def get_one_player(id_or_name, season=None): - req_url = f'http://database/api/v1/players/{id_or_name}' + req_url = f'{DB_URL}/v1/players/{id_or_name}' if season is not None: req_url += f'?season={season}' @@ -351,7 +352,7 @@ async def patch_player( :param il_return: :return: """ - req_url = f'http://database/api/v2/players/{pid}' + 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}' @@ -468,7 +469,7 @@ async def patch_player( async def get_standings(season=None, team_abbrev=None, league_abbrev=None, division_abbrev=None): - req_url = f'http://database/api/v1/standings' + req_url = f'{DB_URL}/v1/standings' other_params = False if season: req_url += f'{param_char(other_params)}season={season}' @@ -492,7 +493,7 @@ async def get_standings(season=None, team_abbrev=None, league_abbrev=None, divis async def post_standings_recalc(season): - req_url = f'http://database/api/v1/standings/s{season}/recalculate' + 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: @@ -504,7 +505,7 @@ async def post_standings_recalc(season): 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}' + req_url = f'{DB_URL}/v1/results?season={season}' if team_abbrev: req_url += f'&team_abbrev={team_abbrev}' if week: @@ -523,7 +524,7 @@ async def get_results(season, team_abbrev=None, week=None, away_abbrev=None, hom async def post_result(result): - req_url = f'http://database/api/v1/results' + req_url = f'{DB_URL}/v1/results' payload = json.dumps(result) resp = requests.post(req_url, payload, headers=AUTH_TOKEN, timeout=3) @@ -535,7 +536,7 @@ async def post_result(result): async def patch_result(result_id, away_score=None, home_score=None): - req_url = f'http://database/api/v1/results/{result_id}' + 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}' @@ -553,7 +554,7 @@ async def patch_result(result_id, away_score=None, home_score=None): async def delete_result(result_id): - req_url = f'http://database/api/v1/results/{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: @@ -566,7 +567,7 @@ async def delete_result(result_id): 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}' + req_url = f'{DB_URL}/v1/transactions?season={season}' if team_abbrev: req_url += f'&team_abbrev={team_abbrev}' if week_start: @@ -593,7 +594,7 @@ async def get_transactions( async def post_transactions(moves: list): - req_url = f'http://database/api/v1/transactions' + req_url = f'{DB_URL}/v1/transactions' data = { 'count': len(moves), 'moves': moves @@ -609,7 +610,7 @@ async def post_transactions(moves: list): async def patch_transaction(move_id, frozen=None, cancelled=None): - req_url = f'http://database/api/v1/transactions/{move_id}' + req_url = f'{DB_URL}/v1/transactions/{move_id}' other_params = False if frozen is not None: req_url += f'{param_char(other_params)}frozen={frozen}' @@ -627,7 +628,7 @@ async def patch_transaction(move_id, frozen=None, cancelled=None): async def get_one_battingseason(player_id): - req_url = f'http://database/api/v1/battingseasons/{player_id}' + req_url = f'{DB_URL}/v1/battingseasons/{player_id}' resp = requests.get(req_url, timeout=3) if resp.status_code == 200: @@ -638,7 +639,7 @@ async def get_one_battingseason(player_id): async def get_one_pitchingseason(player_id): - req_url = f'http://database/api/v1/pitchingseasons/{player_id}' + req_url = f'{DB_URL}/v1/pitchingseasons/{player_id}' resp = requests.get(req_url, timeout=3) if resp.status_code == 200: @@ -649,7 +650,7 @@ async def get_one_pitchingseason(player_id): async def get_one_fieldingseason(player_id): - req_url = f'http://database/api/v1/fieldingseasons/{player_id}' + req_url = f'{DB_URL}/v1/fieldingseasons/{player_id}' resp = requests.get(req_url, timeout=3) if resp.status_code == 200: @@ -660,7 +661,7 @@ async def get_one_fieldingseason(player_id): async def get_battingcareer(name_or_id): - req_url = f'http://database/api/v1/battingcareer/{name_or_id}' + req_url = f'{DB_URL}/v1/battingcareer/{name_or_id}' resp = requests.get(req_url, timeout=3) if resp.status_code == 200: @@ -673,7 +674,7 @@ async def get_battingcareer(name_or_id): async def get_pitchingcareer(name_or_id): - req_url = f'http://database/api/v1/pitchingcareer/{name_or_id}' + req_url = f'{DB_URL}/v1/pitchingcareer/{name_or_id}' resp = requests.get(req_url, timeout=3) if resp.status_code == 200: @@ -688,7 +689,7 @@ async def get_pitchingcareer(name_or_id): 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}' + req_url = f'{DB_URL}/v1/battingstats/s{season}/{s_type}' other_params = False if team_abbrev: req_url += f'{param_char(other_params)}team_abbrev={team_abbrev}' @@ -721,7 +722,7 @@ async def get_battingstat( async def post_battingstats(stats: list): - req_url = f'http://database/api/v1/battingstats' + req_url = f'{DB_URL}/v1/battingstats' data = { 'count': len(stats), 'stats': stats @@ -737,7 +738,7 @@ async def post_battingstats(stats: list): async def delete_battingstats(game_obj: dict): - req_url = f'http://database/api/v1/battingstats' + req_url = f'{DB_URL}/v1/battingstats' payload = json.dumps(game_obj) resp = requests.delete(req_url, data=payload, headers=AUTH_TOKEN) @@ -749,7 +750,7 @@ async def delete_battingstats(game_obj: dict): async def recalc_batting_seasons(season, team_id): - req_url = f'http://database/api/v1/battingseasons/recalculate?season={season}&team_id={team_id}' + req_url = f'{DB_URL}/v1/battingseasons/recalculate?season={season}&team_id={team_id}' resp = requests.post(req_url, headers=AUTH_TOKEN, timeout=45) if resp.status_code == 200: @@ -760,7 +761,7 @@ async def recalc_batting_seasons(season, team_id): async def recalc_fielding_seasons(season, team_id): - req_url = f'http://database/api/v1/fieldingseasons/recalculate?season={season}&team_id={team_id}' + req_url = f'{DB_URL}/v1/fieldingseasons/recalculate?season={season}&team_id={team_id}' resp = requests.post(req_url, headers=AUTH_TOKEN, timeout=45) if resp.status_code == 200: @@ -773,7 +774,7 @@ async def recalc_fielding_seasons(season, team_id): 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}' + req_url = f'{DB_URL}/v1/pitchingstats/s{season}/{s_type}' other_params = False if team_abbrev: req_url += f'{param_char(other_params)}team_abbrev={team_abbrev}' @@ -803,7 +804,7 @@ async def get_pitchingstat( async def post_pitchingstats(stats: list): - req_url = f'http://database/api/v1/pitchingstats' + req_url = f'{DB_URL}/v1/pitchingstats' data = { 'count': len(stats), 'stats': stats @@ -819,7 +820,7 @@ async def post_pitchingstats(stats: list): async def delete_pitchingstats(game_obj: dict): - req_url = f'http://database/api/v1/pitchingstats' + req_url = f'{DB_URL}/v1/pitchingstats' payload = json.dumps(game_obj) resp = requests.delete(req_url, data=payload, headers=AUTH_TOKEN) @@ -831,7 +832,7 @@ async def delete_pitchingstats(game_obj: dict): async def recalc_pitching_seasons(season, team_id): - req_url = f'http://database/api/v1/pitchingseasons/recalculate?season={season}&team_id={team_id}' + req_url = f'{DB_URL}/v1/pitchingseasons/recalculate?season={season}&team_id={team_id}' resp = requests.post(req_url, headers=AUTH_TOKEN, timeout=45) if resp.status_code == 200: @@ -842,7 +843,7 @@ async def recalc_pitching_seasons(season, team_id): async def get_draftdata(): - resp = requests.get(f'http://database/api/v1/draftdata') + resp = requests.get(f'{DB_URL}/v1/draftdata') if resp.status_code == 200: return resp.json() else: @@ -852,7 +853,7 @@ async def get_draftdata(): 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' + req_url = f'{DB_URL}/v1/draftdata' other_params = False if currentpick is not None: req_url += f'{param_char(other_params)}currentpick={currentpick}' @@ -884,7 +885,7 @@ async def patch_draftdata( 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}' + req_url = f'{DB_URL}/v1/draftpicks?season={season}' if owner_team: req_url += f'&owner_team_abbrev={owner_team["abbrev"]}' if orig_owner_team: @@ -909,7 +910,7 @@ async def get_draftpicks( 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'{DB_URL}/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}' @@ -930,7 +931,7 @@ async def get_one_draftpick_search(season, orig_owner_abbrev, round_num: int, te async def get_one_draftpick_byid(pick_id: int): - req_url = f'http://database/api/v1/draftpicks/{pick_id}' + req_url = f'{DB_URL}/v1/draftpicks/{pick_id}' resp = requests.get(req_url, timeout=3) if resp.status_code == 200: @@ -941,7 +942,7 @@ async def get_one_draftpick_byid(pick_id: int): async def get_one_draftpick_byoverall(season: int, overall: int): - req_url = f'http://database/api/v1/draftpicks?season={season}&overall={overall}' + req_url = f'{DB_URL}/v1/draftpicks?season={season}&overall={overall}' resp = requests.get(req_url, timeout=3) if resp.status_code == 200: @@ -959,7 +960,7 @@ async def get_one_draftpick_byoverall(season: int, overall: int): 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}' + req_url = f'{DB_URL}/v1/draftpicks/{pick_id}' other_params = False if overall is not None: req_url += f'{param_char(other_params)}overall={overall}' @@ -989,7 +990,7 @@ async def patch_draftpick( async def get_awards(player_id=None, player_name=None, season=None): - req_url = f'http://database/api/v1/awards' + req_url = f'{DB_URL}/v1/awards' other_params = False if player_id: req_url += f'{param_char(other_params)}player_id={player_id}' @@ -1010,7 +1011,7 @@ async def get_awards(player_id=None, player_name=None, season=None): async def post_award(award: dict): - req_url = f'http://database/api/v1/awards' + req_url = f'{DB_URL}/v1/awards' award_model = { 'name': award['name'], 'season': award['season'], @@ -1039,7 +1040,7 @@ async def post_award(award: dict): async def get_one_manager(manager_name): - req_url = f'http://database/api/v1/managers/{manager_name}' + req_url = f'{DB_URL}/v1/managers/{manager_name}' resp = requests.get(req_url, timeout=3) if resp.status_code == 200: @@ -1050,7 +1051,7 @@ async def get_one_manager(manager_name): async def post_dice(dice_bag: list): - req_url = f'http://database/api/v1/dice' + req_url = f'{DB_URL}/v1/dice' dice_model = { 'count': len(dice_bag), 'rolls': dice_bag @@ -1094,7 +1095,7 @@ async def get_player_headshot(player_name): async def post_draft_list(this_list: list): - req_url = f'http://database/api/v1/draft-list' + req_url = f'{DB_URL}/v1/draft-list' list_model = { 'count': len(this_list), 'draft_list': this_list @@ -1111,7 +1112,7 @@ async def post_draft_list(this_list: list): async def get_draft_list(team): - req_url = f'http://database/api/v1/draft-list/{team["id"]}' + req_url = f'{DB_URL}/v1/draft-list/{team["id"]}' resp = requests.get(req_url, headers=AUTH_TOKEN, timeout=3) if resp.status_code == 200: diff --git a/helpers.py b/helpers.py index 22c5210..726fd75 100644 --- a/helpers.py +++ b/helpers.py @@ -1058,8 +1058,8 @@ def get_pos_abbrev(pos_name): raise KeyError(f'{pos_name} is not a recognized position name') -def new_rand_conf_gif(): - req_url = 'https://api.giphy.com/v1/gifs/translate?s=all done&api_key=H86xibttEuUcslgmMM6uu74IgLEZ7UOD' +def random_gif(search_term: str): + req_url = f'https://api.giphy.com/v1/gifs/translate?s={search_term}&api_key=H86xibttEuUcslgmMM6uu74IgLEZ7UOD' resp = requests.get(req_url, timeout=3) if resp.status_code == 200: @@ -1071,3 +1071,13 @@ def new_rand_conf_gif(): else: logging.warning(resp.text) raise ValueError(f'DB: {resp.text}') + + +def new_rand_conf_gif(): + return random_gif('all done') + + +def random_from_list(data_list: list): + item = data_list[random.randint(0, len(data_list) - 1)] + logging.info(f'random_from_list: {item}') + return item