From caae450fc08d6c8db7dcea6fc98cfd2446606434 Mon Sep 17 00:00:00 2001 From: bnm91 Date: Tue, 24 Oct 2023 15:50:40 -0700 Subject: [PATCH 1/5] adding owners to Teams --- espn_api/base_league.py | 18 +++++++++++++++++- espn_api/baseball/team.py | 3 ++- espn_api/basketball/team.py | 3 ++- espn_api/football/team.py | 3 ++- espn_api/hockey/team.py | 3 ++- espn_api/wbasketball/team.py | 3 ++- 6 files changed, 27 insertions(+), 6 deletions(-) diff --git a/espn_api/base_league.py b/espn_api/base_league.py index 241fe115..50e65a56 100644 --- a/espn_api/base_league.py +++ b/espn_api/base_league.py @@ -12,6 +12,7 @@ def __init__(self, league_id: int, year: int, sport: str, espn_s2=None, swid=Non self.league_id = league_id self.year = year self.teams = [] + self.members = [] self.draft = [] self.player_map = {} @@ -38,6 +39,7 @@ def _fetch_league(self, SettingsClass = BaseSettings): else: self.current_week = self.scoringPeriodId if self.scoringPeriodId <= data['status']['finalScoringPeriod'] else data['status']['finalScoringPeriod'] self.settings = SettingsClass(data['settings']) + self.members = data['members'] return data def _fetch_teams(self, data, TeamClass, pro_schedule = None): @@ -51,9 +53,12 @@ def _fetch_teams(self, data, TeamClass, pro_schedule = None): for team in data['teams']: team_roster[team['id']] = team.get('roster', {}) + team_owners = self._get_team_owners(data) + for team in teams: roster = team_roster[team['id']] - self.teams.append(TeamClass(team, roster=roster, schedule=schedule, year=seasonId, pro_schedule=pro_schedule)) + owners = team_owners[team['id']] + self.teams.append(TeamClass(team, roster=roster, schedule=schedule, year=seasonId, pro_schedule=pro_schedule, owners=owners)) # sort by team ID self.teams = sorted(self.teams, key=lambda x: x.team_id, reverse=False) @@ -92,6 +97,17 @@ def _get_all_pro_schedule(self): pro_team_schedule[team['id']] = pro_game return pro_team_schedule + def _get_team_owners(self, data) -> dict: + team_owners = {} + for team in data['teams']: + team_owners[team['id']] = [] + for member in self.members: + for owner in team['owners']: + if owner == member['id']: + team_owners[team['id']].append(member) + + return team_owners + def standings(self) -> List: standings = sorted(self.teams, key=lambda x: x.final_standing if x.final_standing != 0 else x.standing, reverse=False) return standings \ No newline at end of file diff --git a/espn_api/baseball/team.py b/espn_api/baseball/team.py index 6fae0e75..f9506fb6 100644 --- a/espn_api/baseball/team.py +++ b/espn_api/baseball/team.py @@ -5,7 +5,7 @@ class Team(object): '''Teams are part of the league''' - def __init__(self, data, roster, schedule, year, **kwargs): + def __init__(self, data, roster, schedule, year, owners, **kwargs): self.team_id = data['id'] self.team_abbrev = data['abbrev'] if year < 2023: @@ -27,6 +27,7 @@ def __init__(self, data, roster, schedule, year, **kwargs): self._fetch_roster(roster) self._fetch_schedule(schedule) + self.owners = owners def __repr__(self): return f'Team({self.team_name})' diff --git a/espn_api/basketball/team.py b/espn_api/basketball/team.py index 1523ff6b..f28a4785 100644 --- a/espn_api/basketball/team.py +++ b/espn_api/basketball/team.py @@ -4,7 +4,7 @@ class Team(object): '''Teams are part of the league''' - def __init__(self, data, roster, schedule, year, **kwargs): + def __init__(self, data, roster, schedule, year, owners, **kwargs): self.team_id = data['id'] self.team_abbrev = data['abbrev'] if year < 2023: @@ -30,6 +30,7 @@ def __init__(self, data, roster, schedule, year, **kwargs): self._fetch_roster(roster, year, kwargs.get('pro_schedule')) self._fetch_schedule(schedule) + self.owners = owners def __repr__(self): return f'Team({self.team_name})' diff --git a/espn_api/football/team.py b/espn_api/football/team.py index d4b945bf..33397094 100644 --- a/espn_api/football/team.py +++ b/espn_api/football/team.py @@ -2,7 +2,7 @@ class Team(object): '''Teams are part of the league''' - def __init__(self, data, roster, schedule, year, **kwargs): + def __init__(self, data, roster, schedule, year, owners, **kwargs): self.team_id = data['id'] self.team_abbrev = data['abbrev'] if year < 2023: @@ -37,6 +37,7 @@ def __init__(self, data, roster, schedule, year, **kwargs): self.mov = [] self._fetch_schedule(schedule) self._fetch_roster(roster, year) + self.owners = owners def __repr__(self): return 'Team(%s)' % (self.team_name, ) diff --git a/espn_api/hockey/team.py b/espn_api/hockey/team.py index 81dcd348..1c0e7536 100644 --- a/espn_api/hockey/team.py +++ b/espn_api/hockey/team.py @@ -6,7 +6,7 @@ class Team(object): '''Teams are part of the league''' - def __init__(self, data, roster, schedule, year, **kwargs): + def __init__(self, data, roster, schedule, year, owners, **kwargs): self.team_id = data['id'] self.team_abbrev = data['abbrev'] if year < 2023: @@ -34,6 +34,7 @@ def __init__(self, data, roster, schedule, year, **kwargs): self._fetch_roster(roster) self._fetch_schedule(schedule) + self.owners = owners def __repr__(self): return 'Team(%s)' % (self.team_name,) diff --git a/espn_api/wbasketball/team.py b/espn_api/wbasketball/team.py index 0c0edcba..55310a67 100644 --- a/espn_api/wbasketball/team.py +++ b/espn_api/wbasketball/team.py @@ -4,7 +4,7 @@ class Team(object): '''Teams are part of the league''' - def __init__(self, data, roster, schedule, year, **kwargs): + def __init__(self, data, roster, schedule, year, owners, **kwargs): self.team_id = data['id'] self.team_abbrev = data['abbrev'] if year < 2023: @@ -31,6 +31,7 @@ def __init__(self, data, roster, schedule, year, **kwargs): self._fetch_roster(roster, year) self._fetch_schedule(schedule) + self.owners = owners def __repr__(self): return f'Team({self.team_name})' From fa6bb2cc58f0dbbfc65f7da478260e43a431c64d Mon Sep 17 00:00:00 2001 From: bnm91 Date: Wed, 25 Oct 2023 16:33:59 -0700 Subject: [PATCH 2/5] simplify owners update to be id only --- espn_api/base_league.py | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/espn_api/base_league.py b/espn_api/base_league.py index 50e65a56..f0aa18db 100644 --- a/espn_api/base_league.py +++ b/espn_api/base_league.py @@ -50,10 +50,10 @@ def _fetch_teams(self, data, TeamClass, pro_schedule = None): seasonId = data['seasonId'] team_roster = {} + team_owners = {} for team in data['teams']: team_roster[team['id']] = team.get('roster', {}) - - team_owners = self._get_team_owners(data) + team_owners[team['id']] = team.get('owners', []) for team in teams: roster = team_roster[team['id']] @@ -97,17 +97,6 @@ def _get_all_pro_schedule(self): pro_team_schedule[team['id']] = pro_game return pro_team_schedule - def _get_team_owners(self, data) -> dict: - team_owners = {} - for team in data['teams']: - team_owners[team['id']] = [] - for member in self.members: - for owner in team['owners']: - if owner == member['id']: - team_owners[team['id']].append(member) - - return team_owners - def standings(self) -> List: standings = sorted(self.teams, key=lambda x: x.final_standing if x.final_standing != 0 else x.standing, reverse=False) return standings \ No newline at end of file From 1910e151d319d6620b17130ecc3c111016001664 Mon Sep 17 00:00:00 2001 From: bnm91 Date: Wed, 25 Oct 2023 16:34:15 -0700 Subject: [PATCH 3/5] fixing unit test to reflect new Team constructor --- tests/hockey/unit/test_team.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/hockey/unit/test_team.py b/tests/hockey/unit/test_team.py index e03e0a35..7ce90898 100644 --- a/tests/hockey/unit/test_team.py +++ b/tests/hockey/unit/test_team.py @@ -15,13 +15,14 @@ def setUp(self) -> None: self.team = self.data['teams'][3] self.team_roster = self.team['roster'] + self.owners = self.team['owners'] def test_team(self): - team = Team(self.team, roster= self.team_roster, schedule= self.schedule, year= self.year) + team = Team(self.team, roster= self.team_roster, schedule= self.schedule, year= self.year, owners=self.owners) self.assertEqual(team.team_abbrev, 'ESPC') def test_team_roster_df(self): - team = Team(self.team, roster= self.team_roster, schedule= self.schedule, year= self.year) + team = Team(self.team, roster= self.team_roster, schedule= self.schedule, year= self.year, owners=self.owners) self.assertEqual(len(team.roster), 25) self.assertEqual(team.roster[0].name, 'Thomas Chabot') From 0aef069e920132ef4fb86e71321a85157127592a Mon Sep 17 00:00:00 2001 From: bnm91 Date: Thu, 26 Oct 2023 10:25:19 -0700 Subject: [PATCH 4/5] moving logic to Team --- espn_api/base_league.py | 5 +---- espn_api/baseball/team.py | 4 ++-- espn_api/basketball/team.py | 4 ++-- espn_api/football/team.py | 4 ++-- espn_api/hockey/team.py | 4 ++-- espn_api/wbasketball/team.py | 4 ++-- tests/hockey/unit/test_team.py | 5 ++--- 7 files changed, 13 insertions(+), 17 deletions(-) diff --git a/espn_api/base_league.py b/espn_api/base_league.py index f0aa18db..ece426a5 100644 --- a/espn_api/base_league.py +++ b/espn_api/base_league.py @@ -50,15 +50,12 @@ def _fetch_teams(self, data, TeamClass, pro_schedule = None): seasonId = data['seasonId'] team_roster = {} - team_owners = {} for team in data['teams']: team_roster[team['id']] = team.get('roster', {}) - team_owners[team['id']] = team.get('owners', []) for team in teams: roster = team_roster[team['id']] - owners = team_owners[team['id']] - self.teams.append(TeamClass(team, roster=roster, schedule=schedule, year=seasonId, pro_schedule=pro_schedule, owners=owners)) + self.teams.append(TeamClass(team, roster=roster, schedule=schedule, year=seasonId, pro_schedule=pro_schedule)) # sort by team ID self.teams = sorted(self.teams, key=lambda x: x.team_id, reverse=False) diff --git a/espn_api/baseball/team.py b/espn_api/baseball/team.py index f9506fb6..acfbeb99 100644 --- a/espn_api/baseball/team.py +++ b/espn_api/baseball/team.py @@ -5,7 +5,7 @@ class Team(object): '''Teams are part of the league''' - def __init__(self, data, roster, schedule, year, owners, **kwargs): + def __init__(self, data, roster, schedule, year, **kwargs): self.team_id = data['id'] self.team_abbrev = data['abbrev'] if year < 2023: @@ -27,7 +27,7 @@ def __init__(self, data, roster, schedule, year, owners, **kwargs): self._fetch_roster(roster) self._fetch_schedule(schedule) - self.owners = owners + self.owners = data.get('owners', []) def __repr__(self): return f'Team({self.team_name})' diff --git a/espn_api/basketball/team.py b/espn_api/basketball/team.py index f28a4785..19a91c15 100644 --- a/espn_api/basketball/team.py +++ b/espn_api/basketball/team.py @@ -4,7 +4,7 @@ class Team(object): '''Teams are part of the league''' - def __init__(self, data, roster, schedule, year, owners, **kwargs): + def __init__(self, data, roster, schedule, year, **kwargs): self.team_id = data['id'] self.team_abbrev = data['abbrev'] if year < 2023: @@ -30,7 +30,7 @@ def __init__(self, data, roster, schedule, year, owners, **kwargs): self._fetch_roster(roster, year, kwargs.get('pro_schedule')) self._fetch_schedule(schedule) - self.owners = owners + self.owners = data.get('owners', []) def __repr__(self): return f'Team({self.team_name})' diff --git a/espn_api/football/team.py b/espn_api/football/team.py index 33397094..50d51b3e 100644 --- a/espn_api/football/team.py +++ b/espn_api/football/team.py @@ -2,7 +2,7 @@ class Team(object): '''Teams are part of the league''' - def __init__(self, data, roster, schedule, year, owners, **kwargs): + def __init__(self, data, roster, schedule, year, **kwargs): self.team_id = data['id'] self.team_abbrev = data['abbrev'] if year < 2023: @@ -37,7 +37,7 @@ def __init__(self, data, roster, schedule, year, owners, **kwargs): self.mov = [] self._fetch_schedule(schedule) self._fetch_roster(roster, year) - self.owners = owners + self.owners = data.get('owners', []) def __repr__(self): return 'Team(%s)' % (self.team_name, ) diff --git a/espn_api/hockey/team.py b/espn_api/hockey/team.py index 1c0e7536..550a1b59 100644 --- a/espn_api/hockey/team.py +++ b/espn_api/hockey/team.py @@ -6,7 +6,7 @@ class Team(object): '''Teams are part of the league''' - def __init__(self, data, roster, schedule, year, owners, **kwargs): + def __init__(self, data, roster, schedule, year, **kwargs): self.team_id = data['id'] self.team_abbrev = data['abbrev'] if year < 2023: @@ -34,7 +34,7 @@ def __init__(self, data, roster, schedule, year, owners, **kwargs): self._fetch_roster(roster) self._fetch_schedule(schedule) - self.owners = owners + self.owners = data.get('owners', []) def __repr__(self): return 'Team(%s)' % (self.team_name,) diff --git a/espn_api/wbasketball/team.py b/espn_api/wbasketball/team.py index 55310a67..af9ddd6c 100644 --- a/espn_api/wbasketball/team.py +++ b/espn_api/wbasketball/team.py @@ -4,7 +4,7 @@ class Team(object): '''Teams are part of the league''' - def __init__(self, data, roster, schedule, year, owners, **kwargs): + def __init__(self, data, roster, schedule, year, **kwargs): self.team_id = data['id'] self.team_abbrev = data['abbrev'] if year < 2023: @@ -31,7 +31,7 @@ def __init__(self, data, roster, schedule, year, owners, **kwargs): self._fetch_roster(roster, year) self._fetch_schedule(schedule) - self.owners = owners + self.owners = data.get('owners', []) def __repr__(self): return f'Team({self.team_name})' diff --git a/tests/hockey/unit/test_team.py b/tests/hockey/unit/test_team.py index 7ce90898..e03e0a35 100644 --- a/tests/hockey/unit/test_team.py +++ b/tests/hockey/unit/test_team.py @@ -15,14 +15,13 @@ def setUp(self) -> None: self.team = self.data['teams'][3] self.team_roster = self.team['roster'] - self.owners = self.team['owners'] def test_team(self): - team = Team(self.team, roster= self.team_roster, schedule= self.schedule, year= self.year, owners=self.owners) + team = Team(self.team, roster= self.team_roster, schedule= self.schedule, year= self.year) self.assertEqual(team.team_abbrev, 'ESPC') def test_team_roster_df(self): - team = Team(self.team, roster= self.team_roster, schedule= self.schedule, year= self.year, owners=self.owners) + team = Team(self.team, roster= self.team_roster, schedule= self.schedule, year= self.year) self.assertEqual(len(team.roster), 25) self.assertEqual(team.roster[0].name, 'Thomas Chabot') From ed238bc7af22698b5141a4d16cd73553f40e1280 Mon Sep 17 00:00:00 2001 From: bnm91 Date: Thu, 26 Oct 2023 10:27:10 -0700 Subject: [PATCH 5/5] safety --- espn_api/base_league.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/espn_api/base_league.py b/espn_api/base_league.py index ece426a5..7aea1477 100644 --- a/espn_api/base_league.py +++ b/espn_api/base_league.py @@ -39,7 +39,7 @@ def _fetch_league(self, SettingsClass = BaseSettings): else: self.current_week = self.scoringPeriodId if self.scoringPeriodId <= data['status']['finalScoringPeriod'] else data['status']['finalScoringPeriod'] self.settings = SettingsClass(data['settings']) - self.members = data['members'] + self.members = data.get('members', []) return data def _fetch_teams(self, data, TeamClass, pro_schedule = None):