diff --git a/harambot/yahoo_api.py b/harambot/yahoo_api.py index 1301162..8e46108 100644 --- a/harambot/yahoo_api.py +++ b/harambot/yahoo_api.py @@ -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: @@ -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) @@ -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