diff --git a/espn_api/base_league.py b/espn_api/base_league.py index 241fe115..78607f62 100644 --- a/espn_api/base_league.py +++ b/espn_api/base_league.py @@ -44,6 +44,7 @@ def _fetch_teams(self, data, TeamClass, pro_schedule = None): '''Fetch teams in league''' self.teams = [] teams = data['teams'] + members = data['members'] schedule = data['schedule'] seasonId = data['seasonId'] @@ -53,7 +54,14 @@ def _fetch_teams(self, data, TeamClass, pro_schedule = None): for team in teams: roster = team_roster[team['id']] - self.teams.append(TeamClass(team, roster=roster, schedule=schedule, year=seasonId, pro_schedule=pro_schedule)) + for member in members: + # For league that is not full the team will not have a owner field + if 'owners' not in team or not team['owners']: + member = None + break + elif member['id'] == team['owners'][0]: + break + self.teams.append(TeamClass(team, roster=roster, member=member, 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 6fae0e75..83738a4e 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, member, roster, schedule, year, **kwargs): self.team_id = data['id'] self.team_abbrev = data['abbrev'] if year < 2023: @@ -17,11 +17,16 @@ def __init__(self, data, roster, schedule, year, **kwargs): self.wins = data['record']['overall']['wins'] self.losses = data['record']['overall']['losses'] self.ties = data['record']['overall']['ties'] + self.owner = 'None' self.logo_url = '' self.standing = data['playoffSeed'] self.final_standing = data['rankCalculatedFinal'] self.roster = [] self.schedule = [] + + if member: + self.owner = "%s %s" % (member['firstName'], + member['lastName']) if 'logo' in data: self.logo_url = data['logo'] diff --git a/espn_api/basketball/team.py b/espn_api/basketball/team.py index 1523ff6b..591fec53 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, member, roster, schedule, year, **kwargs): self.team_id = data['id'] self.team_abbrev = data['abbrev'] if year < 2023: @@ -15,7 +15,8 @@ def __init__(self, data, roster, schedule, year, **kwargs): self.division_name = '' # set by caller self.wins = data['record']['overall']['wins'] self.losses = data['record']['overall']['losses'] - self.ties = data['record']['overall']['ties'] + self.ties = data['record']['overall']['ties'] + self.owner = 'None' self.logo_url = '' self.stats = None self.standing = data['playoffSeed'] @@ -25,6 +26,8 @@ def __init__(self, data, roster, schedule, year, **kwargs): if 'valuesByStat' in data: self.stats = {STATS_MAP.get(i, i): j for i, j in data['valuesByStat'].items()} + if member: + self.owner = f"{member['firstName']} {member['lastName']}" if 'logo' in data: self.logo_url = data['logo'] diff --git a/espn_api/football/team.py b/espn_api/football/team.py index d4b945bf..76fbb5df 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, member, roster, schedule, year, **kwargs): self.team_id = data['id'] self.team_abbrev = data['abbrev'] if year < 2023: @@ -22,6 +22,10 @@ def __init__(self, data, roster, schedule, year, **kwargs): self.trades = data.get('transactionCounter', {}).get('trades', 0) self.playoff_pct = data.get('currentSimulationResults', {}).get('playoffPct', 0) * 100 self.draft_projected_rank = data.get('draftDayProjectedRank', 0) + self.owner = 'None' + if member: + self.owner = "%s %s" % (member['firstName'], + member['lastName']) self.streak_length = data['record']['overall']['streakLength'] self.streak_type = data['record']['overall']['streakType'] self.standing = data['playoffSeed'] diff --git a/espn_api/hockey/__init__.py b/espn_api/hockey/__init__.py index 74026ae2..8680d062 100644 --- a/espn_api/hockey/__init__.py +++ b/espn_api/hockey/__init__.py @@ -8,6 +8,7 @@ ] from .league import League +from .member import Member from .player import Player from .record import Record from .team import Team diff --git a/espn_api/hockey/member.py b/espn_api/hockey/member.py new file mode 100644 index 00000000..f879e48c --- /dev/null +++ b/espn_api/hockey/member.py @@ -0,0 +1,8 @@ +class Member(object): + + def __init__(self, data): + self.id = data['id'] + self.display_name = data['displayName'] + self.first_name = data['firstName'] + self.last_name = data['lastName'] + self.full_name = self.first_name + ' ' + self.last_name \ No newline at end of file diff --git a/espn_api/hockey/team.py b/espn_api/hockey/team.py index 81dcd348..05a30772 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, member, roster, schedule, year, **kwargs): self.team_id = data['id'] self.team_abbrev = data['abbrev'] if year < 2023: @@ -29,6 +29,9 @@ def __init__(self, data, roster, schedule, year, **kwargs): if 'valuesByStat' in data: self.stats = {STATS_MAP[i]: j for i, j in data['valuesByStat'].items()} + if member: + self.owner = "%s %s" % (member['firstName'], + member['lastName']) if 'logo' in data: self.logo_url = data['logo'] diff --git a/espn_api/wbasketball/team.py b/espn_api/wbasketball/team.py index 0c0edcba..e5f75875 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, member, roster, schedule, year, **kwargs): self.team_id = data['id'] self.team_abbrev = data['abbrev'] if year < 2023: @@ -26,6 +26,8 @@ def __init__(self, data, roster, schedule, year, **kwargs): if 'valuesByStat' in data: self.stats = {STATS_MAP[i]: j for i, j in data['valuesByStat'].items()} + if member: + self.owner = f"{member['firstName']} {member['lastName']}" if 'logo' in data: self.logo_url = data['logo'] diff --git a/tests/hockey/unit/test_team.py b/tests/hockey/unit/test_team.py index e03e0a35..71423221 100644 --- a/tests/hockey/unit/test_team.py +++ b/tests/hockey/unit/test_team.py @@ -17,11 +17,11 @@ def setUp(self) -> None: self.team_roster = self.team['roster'] 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, member= None, 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) + team = Team(self.team, roster= self.team_roster, member= None, schedule= self.schedule, year= self.year) self.assertEqual(len(team.roster), 25) self.assertEqual(team.roster[0].name, 'Thomas Chabot')