Skip to content

Commit

Permalink
Starting to refactor yahoo cog and yahoo api
Browse files Browse the repository at this point in the history
  • Loading branch information
DMcP89 committed Aug 28, 2024
1 parent 2936108 commit 8758b2f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
24 changes: 15 additions & 9 deletions harambot/cogs/yahoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import logging
import urllib3
import functools
import yahoo_oauth
import time


from discord.ext import commands
from discord import app_commands
Expand All @@ -13,12 +14,10 @@

from harambot.yahoo_api import Yahoo
from harambot.database.models import Guild
from harambot.config import settings
from harambot import utils

logging.setLoggerClass(logging.Logger)
yahoo_oauth.logger = logging.getLogger("yahoo_oauth")
logging.getLogger("yahoo_oauth").setLevel(settings.LOGLEVEL)
logging.getLogger("yahoo_oauth").setLevel("INFO")

logger = logging.getLogger("discord.harambot.cogs.yahoo")

Expand All @@ -31,7 +30,6 @@ class YahooCog(commands.Cog):

def __init__(self, bot, KEY, SECRET):
self.bot = bot
self.http = urllib3.PoolManager()
self.KEY = KEY
self.SECRET = SECRET
self.yahoo_api = None
Expand All @@ -41,6 +39,8 @@ def set_yahoo(f):
async def wrapper(
self, interaction: discord.Interaction, *args, **kwargs
):
logger.info("SET YAHOO WRAPPER Interaction: {}".format(interaction.id))
await interaction.response.defer()
guild = Guild.get_or_none(
Guild.guild_id == str(interaction.guild_id)
)
Expand Down Expand Up @@ -101,8 +101,11 @@ async def roster_autocomplete(
interaction: discord.Interaction,
current: str,
) -> List[app_commands.Choice[str]]:

await interaction.response.defer()
logger.info("AUTOCOMPLETE Interaction {}".format(interaction.id))
logger.info("Interaction Details: {}".format(interaction))
teams = self.yahoo_api.get_teams()
time.sleep(3)
if teams:
options = list(
map(
Expand All @@ -119,13 +122,15 @@ async def roster_autocomplete(
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("COMMAND Interaction {}".format(interaction.id))
logger.info("Is Expired? {}".format(interaction.is_expired()))
logger.info(
"Command:Roster called in %i with team_name:%s",
interaction.guild_id,
team_name,
)
await interaction.response.defer()
embed = discord.Embed(
title="{}'s Roster".format(team_name),
description="",
Expand All @@ -139,9 +144,9 @@ async def roster(self, interaction: discord.Interaction, team_name: str):
value=player["name"],
inline=False,
)
await interaction.response.send_message(embed=embed)
await interaction.followup.send(embed=embed)
else:
await interaction.response.send_message(self.error_message)
await interaction.followup.send(self.error_message)

@app_commands.command(
name="trade",
Expand Down Expand Up @@ -240,6 +245,7 @@ async def stats_autocomplete(
current: str,
) -> List[app_commands.Choice[str]]:
players = self.yahoo_api.get_players(current)
time.sleep(3)
if players:
options = list(
map(
Expand Down
40 changes: 39 additions & 1 deletion harambot/yahoo_api.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,56 @@
import logging
import os
import objectpath

import functools

from yahoo_fantasy_api import game
from cachetools import cached, TTLCache

from harambot.models import Guild

logger = logging.getLogger("discord.harambot.yahoo_api")


dir_path = os.path.dirname(os.path.realpath(__file__))
cache = TTLCache(maxsize=1024, ttl=600)

def oauth(f):
@functools.wraps(f)
async def wrapper(
self, *args, **kwargs
):
logger.info("SET YAHOO WRAPPER Interaction: {}".format(guild_id))
guild = Guild.get_or_none(
Guild.guild_id == str(guild_id)
)
if guild is None:
logger.error(
"Guild with id %i does not exist in the database",
guild_id,
)
# Will need to figure out how this should be handled
#return await interaction.response.send_message(
# "I'm not set up for this server yet please run /config"
#)
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


class Yahoo:

Expand Down

0 comments on commit 8758b2f

Please sign in to comment.