From 84460b60c1ec58252b923c9fa4bd463cef594040 Mon Sep 17 00:00:00 2001 From: DMcP89 Date: Thu, 25 Jan 2024 16:15:06 -0500 Subject: [PATCH 1/5] Fixing caching in yahoo_api --- harambot/yahoo_api.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/harambot/yahoo_api.py b/harambot/yahoo_api.py index bcee3c8..aaa0d96 100644 --- a/harambot/yahoo_api.py +++ b/harambot/yahoo_api.py @@ -14,6 +14,7 @@ dir_path = os.path.dirname(os.path.realpath(__file__)) +cache = TTLCache(maxsize=1024, ttl=600) class Yahoo: @@ -26,7 +27,6 @@ def __init__(self, oauth, league_id, league_type): self.league_id = league_id self.league_type = league_type - @cached(cache=TTLCache(maxsize=1024, ttl=600)) def league(self): if not self.oauth.token_is_valid(): self.oauth.refresh_access_token() @@ -59,7 +59,7 @@ def get_standings(self): ) return None - @cached(cache=TTLCache(maxsize=1024, ttl=600)) + @cached(cache) def get_roster(self, team_name): team_details = self.league().get_team(team_name) if team_details: @@ -67,7 +67,7 @@ def get_roster(self, team_name): else: return None - @cached(cache=TTLCache(maxsize=1024, ttl=600)) + @cached(cache) def get_player_details(self, player_name): try: player = self.league().player_details(player_name)[0] @@ -82,7 +82,7 @@ def get_player_details(self, player_name): ) return None - @cached(cache=TTLCache(maxsize=1024, ttl=600)) + @cached(cache) def get_player_owner(self, player_id): try: player_ownership = self.league().ownership([player_id])[ @@ -107,7 +107,6 @@ def get_player_owner(self, player_id): ) return None - @cached(cache=TTLCache(maxsize=1024, ttl=600)) def get_matchups(self): try: matchups = objectpath.Tree(self.league().matchups()).execute( @@ -137,6 +136,7 @@ def get_matchups(self): ) ) + @cached(cache) def get_matchup_details(self, team): team_name = team[0][2]["name"] team_details = "" @@ -175,7 +175,6 @@ def get_matchup_details(self, team): ) return {"name": team_name, "text": team_details} - @cached(cache=TTLCache(maxsize=1024, ttl=600)) def get_latest_trade(self): try: for key, values in self.league().teams().items(): @@ -193,7 +192,6 @@ def get_latest_trade(self): except Exception: logger.exception("Error while fetching latest trade") - @cached(cache=TTLCache(maxsize=1024, ttl=600)) def get_latest_waiver_transactions(self): ts = datetime.now() - timedelta(days=1) transactions = self.league().transactions("add,drop", "") From a1b5c464f547ab18199820e07d2fada7201a8274 Mon Sep 17 00:00:00 2001 From: DMcP89 Date: Thu, 25 Jan 2024 16:16:13 -0500 Subject: [PATCH 2/5] Implementing autocomplete for roster command --- harambot/cogs/yahoo.py | 58 ++++++++++++++++++++++++++---------------- harambot/yahoo_api.py | 4 ++- 2 files changed, 39 insertions(+), 23 deletions(-) diff --git a/harambot/cogs/yahoo.py b/harambot/cogs/yahoo.py index 1505629..ac6df19 100644 --- a/harambot/cogs/yahoo.py +++ b/harambot/cogs/yahoo.py @@ -7,6 +7,7 @@ from discord import app_commands from yahoo_oauth import OAuth2 from playhouse.shortcuts import model_to_dict +from typing import List from harambot.yahoo_api import Yahoo from harambot.database.models import Guild @@ -29,34 +30,29 @@ def __init__(self, bot, KEY, SECRET): self.SECRET = SECRET self.yahoo_api = None - async def cog_before_invoke(self, ctx): - guild = Guild.get(Guild.guild_id == str(ctx.guild.id)) - self.yahoo_api = Yahoo( - OAuth2( - self.KEY, self.SECRET, store_file=False, **model_to_dict(guild) - ), - guild.league_id, - guild.league_type, - ) - return - def set_yahoo(f): @functools.wraps(f) async def wrapper( self, interaction: discord.Interaction, *args, **kwargs ): guild = Guild.get(Guild.guild_id == str(interaction.guild_id)) - self.yahoo_api = Yahoo( - OAuth2( - self.KEY, - self.SECRET, - store_file=False, - **model_to_dict(guild), - ), - guild.league_id, - guild.league_type, - ) - await f(self, interaction, *args, **kwargs) + + if ( + not self.yahoo_api + or self.yahoo_api.league_id != guild.league_id + ): + self.yahoo_api = Yahoo( + OAuth2( + self.KEY, + self.SECRET, + store_file=False, + **model_to_dict(guild), + ), + guild.league_id, + guild.league_type, + ) + + return await f(self, interaction, *args, **kwargs) return wrapper @@ -83,9 +79,27 @@ async def standings(self, interaction: discord.Interaction): else: await interaction.response.send_message(self.error_message) + @set_yahoo + async def roster_autocomplete( + self, + interaction: discord.Interaction, + current: str, + ) -> List[app_commands.Choice[str]]: + teams = self.yahoo_api.get_teams() + options = list( + map( + lambda x: app_commands.Choice( + name=teams[x]["name"], value=teams[x]["name"] + ), + teams, + ) + ) + return options + @app_commands.command( name="roster", description="Returns the roster of the given team" ) + @app_commands.autocomplete(team_name=roster_autocomplete) @set_yahoo async def roster(self, interaction: discord.Interaction, team_name: str): logger.info("roster called") diff --git a/harambot/yahoo_api.py b/harambot/yahoo_api.py index aaa0d96..c9ddc03 100644 --- a/harambot/yahoo_api.py +++ b/harambot/yahoo_api.py @@ -35,7 +35,9 @@ def league(self): self.scoring_type = league.settings()["scoring_type"] return league - @cached(cache=TTLCache(maxsize=1024, ttl=600)) + def get_teams(self): + return self.league().teams() + def get_standings(self): try: standings = [] From f0c88f074cde5261931e7adbeb0bce2e9902acc0 Mon Sep 17 00:00:00 2001 From: DMcP89 Date: Thu, 25 Jan 2024 16:16:45 -0500 Subject: [PATCH 3/5] Cleaning up bot.py --- harambot/bot.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/harambot/bot.py b/harambot/bot.py index 69e97aa..7a86410 100644 --- a/harambot/bot.py +++ b/harambot/bot.py @@ -17,12 +17,11 @@ if "LOGLEVEL" in settings: logger.setLevel(settings.loglevel) else: - logger.setLevel("INFO") + logger.setLevel("DEBUG") + intents = discord.Intents.default() -# intents.members = True -# intents.messages = True -# intents.message_content = True + bot = commands.Bot(command_prefix="$", description="", intents=intents) bot.remove_command("help") From db9fd1a5afb5bd9cb95f2d6cc514f425e9959d26 Mon Sep 17 00:00:00 2001 From: DMcP89 Date: Tue, 30 Jan 2024 15:38:58 -0500 Subject: [PATCH 4/5] Implementing auto complete for stats command --- harambot/cogs/yahoo.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/harambot/cogs/yahoo.py b/harambot/cogs/yahoo.py index ac6df19..a789988 100644 --- a/harambot/cogs/yahoo.py +++ b/harambot/cogs/yahoo.py @@ -210,9 +210,31 @@ async def trade(self, interaction: discord.Interaction): await response_message.add_reaction(yes_emoji) await response_message.add_reaction(no_emoji) + @set_yahoo + async def stats_autocomplete( + self, + interaction: discord.Interaction, + current: str, + ) -> List[app_commands.Choice[str]]: + players = self.yahoo_api.league().player_details(current) + if players: + options = list( + map( + lambda x: app_commands.Choice( + name=x["name"]["full"], + value=x["name"]["full"], + ), + players, + ) + ) + else: + options = [] + return options + @app_commands.command( name="stats", description="Returns the details of the given player" ) + @app_commands.autocomplete(player_name=stats_autocomplete) @set_yahoo async def stats(self, interaction: discord.Interaction, player_name: str): logger.info("player_details called") From ad5ed73238f4b3b4d281c3fc99b4ec71e7bdcb08 Mon Sep 17 00:00:00 2001 From: DMcP89 Date: Thu, 1 Feb 2024 13:57:34 -0500 Subject: [PATCH 5/5] Removing caching from get_matchup_details function --- harambot/yahoo_api.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/harambot/yahoo_api.py b/harambot/yahoo_api.py index c9ddc03..3771e12 100644 --- a/harambot/yahoo_api.py +++ b/harambot/yahoo_api.py @@ -131,14 +131,14 @@ def get_matchups(self): } ) return str(self.league().current_week()), details - except Exception: + except Exception as e: logger.exception( "Error while fetching matchups for league: {}".format( self.league_id - ) + ), + e, ) - @cached(cache) def get_matchup_details(self, team): team_name = team[0][2]["name"] team_details = ""