diff --git a/LeagueData.py b/LeagueData.py index c46fe6b..8b1a053 100644 --- a/LeagueData.py +++ b/LeagueData.py @@ -11,7 +11,8 @@ load_dotenv(dotenv_path=dotenv_path) """ -Class for the data being built around the league ID and year given +This class is used to hold data about the ESPN Fantasy Basketball League +and the teams in the league including their schedules, rosters, and player stats @github coolbrett """ @@ -21,11 +22,40 @@ class LeagueData: def __init__(self, league_id: int, year: int, espn_s2: str = None, swid: str = None): """LeagueData holds data about the FBB League""" - if espn_s2 != None and swid != None: - self.league = League(league_id=league_id, year=year, espn_s2=espn_s2, swid=swid) + if espn_s2 is not None and swid is not None: + try: + self.league = League(league_id=league_id, year=year, espn_s2=espn_s2, swid=swid) + except: + self.league = League(league_id=league_id, year=year) else: self.league = League(league_id=league_id, year=year) - + + self.__create_active_years_list() + + def __create_active_years_list(self): + """Creates a list of active years for the league""" + self.active_years = [] + for year in range(2003, 2024): + if self.__did_league_exist__(self.league.league_id, year): + self.active_years.append(year) + print(f"Active years: {self.active_years}") + + def __did_league_exist__(self, league_id: int, year: int) -> bool: + """Checks if league existed for a given year""" + try: + League(league_id=league_id, year=year) + return True + except: + return False + + def __did_league_exist__(self, league_id: int, year: int) -> bool: + """Checks if league existed for a given year""" + try: + League(league_id=league_id, year=year) + return True + except: + return False + def find_current_week(self): """The ESPN API being used doesn't keep track of the current week in the fantasy year it is, so this finds the current week and returns it""" @@ -112,7 +142,7 @@ def __get_list_team_abbreviations_for_past_three_weeks(self, list_to_populate): def __report_three_weeks_list(self, three_weeks_list): - """Helper method to build report for three weeks statd""" + """Helper method to build report for three weeks stats""" temp = "" count = 1 temp = "`# Team".ljust(12) + "3WT`" @@ -251,25 +281,15 @@ def get_top_half_percentage_for_each_team(self, stat: str = None) -> dict: returns a dictionary with the keys as team_id's and the values as percentage of players they have on the top half of the list """ - rostered_players = [] - if stat == "avg": - rostered_players = self.get_list_of_all_players_rostered(stat=stat) - else: - rostered_players = self.get_list_of_all_players_rostered() - - rostered_players = rostered_players[0:int(len(rostered_players)/2)] - top_half_player_percentages_by_team = dict() - perc = float(1/len(rostered_players)) - - for roster_player in rostered_players: - for team in self.league.teams: - for team_player in team.roster: - if team_player.playerId == roster_player.playerId: - if team.team_id in top_half_player_percentages_by_team.keys(): - top_half_player_percentages_by_team[team.team_id] += perc - else: - top_half_player_percentages_by_team.__setitem__(team.team_id, perc) - + rostered_players = self.get_list_of_all_players_rostered(stat=stat) if stat == "avg" else self.get_list_of_all_players_rostered() + rostered_players = rostered_players[:len(rostered_players)//2] + top_half_player_percentages_by_team = {} + + for team in self.league.teams: + team_players = set(player.playerId for player in team.roster) + top_half_players = [player for player in rostered_players if player.playerId in team_players] + top_half_player_percentages_by_team[team.team_id] = len(top_half_players) / len(rostered_players) + return top_half_player_percentages_by_team def get_record_vs_all_teams(self) -> dict: diff --git a/bot.py b/bot.py index 4aa3be9..a3d41ae 100644 --- a/bot.py +++ b/bot.py @@ -130,7 +130,12 @@ async def draft_recap(interaction: discord.Interaction, year: int = None, round: embed = discord.Embed( title=str((league_data.league.year - 1)) + "-" + str(league_data.league.year) + " Draft Recap") - draft_recap = league_data.get_draft_recap() + + try: + draft_recap = league_data.get_draft_recap() + except espn_api.exceptions.ESPNInvalidLeague: + await interaction.followup.send(f"Your league did not exist in {league_data.league.year}, try a more recent year :)") + return if round is None: for round_num, list_of_picks in draft_recap.items(): @@ -485,7 +490,7 @@ async def on_application_command_error(context: discord.ApplicationContext, erro print(str(error.original)) await context.interaction.response.send_message("Your league has not been setup yet, or the credentials given are invalid. Use `/setup` to configure your league.") - if isinstance(error, espn_api.requests.espn_requests.ESPNInvalidLeague): + if isinstance(error.original, espn_api.requests.espn_requests.ESPNInvalidLeague): await context.interaction.response.send_message("League credentials do not match any leagues on ESPN. Re-run /setup with correct credentials.") @bot.event diff --git a/todo.md b/todo.md index 8bbdfce..fae722b 100644 --- a/todo.md +++ b/todo.md @@ -1,6 +1,7 @@ BUGS ----- - draft-recap breaks when user inputs a year where league did not exist + - need to get how many years a league has been a thing sometime before commands get called, maybe when LeagueData object builds - scoreboard current year different week just reports current week scoreboard - box-score breaks if given abbreviation that doesn't exist - commands report inaccurately if fantasy league starts on any other week than week 1