Skip to content

Commit

Permalink
Updating error handling in yahoo api module
Browse files Browse the repository at this point in the history
  • Loading branch information
DMcP89 committed Mar 5, 2024
1 parent fe820f9 commit dd4758f
Showing 1 changed file with 67 additions and 22 deletions.
89 changes: 67 additions & 22 deletions harambot/yahoo_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,43 @@ def __init__(self, oauth, league_id, league_type):
self.league_type = league_type

def league(self):
# TODO: Handle oauth failures
if not self.oauth.token_is_valid():
self.oauth.refresh_access_token()
# TODO: Handle yahoo api failures
gm = game.Game(self.oauth, self.league_type)
league = gm.to_league("{}.l.{}".format(gm.game_id(), self.league_id))
self.scoring_type = league.settings()["scoring_type"]
return league

try:
if not self.oauth.token_is_valid():
self.oauth.refresh_access_token()
except Exception:
logger.exception(
"Error while refreshing access token for league: {}".format(
self.league_id
)
)
return None

try:
gm = game.Game(self.oauth, self.league_type)
league = gm.to_league(
"{}.l.{}".format(gm.game_id(), self.league_id)
)
self.scoring_type = league.settings()["scoring_type"]
return league
except Exception:
logger.exception(
"Error while fetching league details for league {}".format(
self.league_id
)
)
return None

def get_teams(self):
# TODO: Handle yahoo api failures
return self.league().teams()
try:
return self.league().teams()
except Exception:
logger.exception(
"Error while fetching teams for league {}".format(
self.league_id
)
)
return None

def get_standings(self):
try:
Expand All @@ -63,11 +88,20 @@ def get_standings(self):

@cached(cache)
def get_roster(self, team_name):
# TODO: Handle yahoo api failures
team_details = self.league().get_team(team_name)
if team_details:
return team_details[team_name].roster(self.league().current_week())
else:
try:
team_details = self.league().get_team(team_name)
if team_details:
return team_details[team_name].roster(
self.league().current_week()
)
else:
return None
except Exception:
logger.exception(
"Error while fetching roster for team: {} in league {}".format(
team_name, self.league_id
)
)
return None

@cached(cache)
Expand Down Expand Up @@ -186,13 +220,24 @@ def get_latest_trade(self):
return accepted_trades[0]
return
except Exception:
logger.exception("Error while fetching latest trade")
logger.exception(
"Error fetching latest trades for league: {}".format(
self.league_id
)
)

def get_latest_waiver_transactions(self):
ts = datetime.now() - timedelta(days=1)
# TODO: Handle yahoo api failures
transactions = self.league().transactions("add,drop", "")
filtered_transactions = [
t for t in transactions if int(t["timestamp"]) > ts.timestamp()
]
return filtered_transactions
try:
transactions = self.league().transactions("add,drop", "")
filtered_transactions = [
t for t in transactions if int(t["timestamp"]) > ts.timestamp()
]
return filtered_transactions
except Exception:
logger.exception(
"Error fetching latest waivers for league: {}".format(
self.league_id
)
)
return None

0 comments on commit dd4758f

Please sign in to comment.