Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FB Add Pro Schedule to Player Class #579

Merged
merged 3 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion espn_api/base_league.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def _get_pro_schedule(self, scoringPeriodId: int = None):
def _get_all_pro_schedule(self):
data = self.espn_request.get_pro_schedule()

pro_teams = data['settings']['proTeams']
pro_teams = data.get('settings', {}).get('proTeams', {})
pro_team_schedule = {}

for team in pro_teams:
Expand Down
9 changes: 5 additions & 4 deletions espn_api/football/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def _fetch_league(self):

def _fetch_teams(self, data):
'''Fetch teams in league'''
super()._fetch_teams(data, TeamClass=Team)
pro_schedule = self._get_all_pro_schedule()
super()._fetch_teams(data, TeamClass=Team, pro_schedule=pro_schedule)

# replace opponentIds in schedule with team instances
for team in self.teams:
Expand Down Expand Up @@ -380,11 +381,11 @@ def player_info(self, name: str = None, playerId: Union[int, list] = None) -> Un
playerId = [playerId]

data = self.espn_request.get_player_card(playerId, self.finalScoringPeriod)

pro_schedule = self._get_all_pro_schedule()
if len(data['players']) == 1:
return Player(data['players'][0], self.year)
return Player(data['players'][0], self.year, pro_schedule)
if len(data['players']) > 1:
return [Player(player, self.year) for player in data['players']]
return [Player(player, self.year, pro_schedule) for player in data['players']]

def message_board(self, msg_types: List[str] = None):
''' Returns a list of league messages'''
Expand Down
12 changes: 11 additions & 1 deletion espn_api/football/player.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from .constant import POSITION_MAP, PRO_TEAM_MAP, PLAYER_STATS_MAP
from .utils import json_parsing
from datetime import datetime

class Player(object):
'''Player are part of team'''
def __init__(self, data, year):
def __init__(self, data, year, pro_team_schedule = None):
self.name = json_parsing(data, 'fullName')
self.playerId = json_parsing(data, 'id')
self.posRank = json_parsing(data, 'positionalRanking')
Expand All @@ -14,13 +15,22 @@ def __init__(self, data, year):
self.onTeamId = json_parsing(data, 'onTeamId')
self.lineupSlot = POSITION_MAP.get(data.get('lineupSlotId'), '')
self.stats = {}
self.schedule = {}

# Get players main position
for pos in json_parsing(data, 'eligibleSlots'):
if (pos != 25 and '/' not in POSITION_MAP[pos]) or '/' in self.name:
self.position = POSITION_MAP[pos]
break

if pro_team_schedule:
pro_team_id = json_parsing(data, 'proTeamId')
pro_team = pro_team_schedule.get(pro_team_id, {})
for key in pro_team:
game = pro_team[key][0]
team = game['awayProTeamId'] if game['awayProTeamId'] != pro_team_id else game['homeProTeamId']
self.schedule[key] = { 'team': PRO_TEAM_MAP[team], 'date': datetime.fromtimestamp(game['date']/1000.0) }

# set each scoring period stat
player = data['playerPoolEntry']['player'] if 'playerPoolEntry' in data else data['player']
self.injuryStatus = player.get('injuryStatus', self.injuryStatus)
Expand Down
6 changes: 3 additions & 3 deletions espn_api/football/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ def __init__(self, data, roster, schedule, year, **kwargs):
self.outcomes = []
self.mov = []
self._fetch_schedule(schedule)
self._fetch_roster(roster, year)
self._fetch_roster(roster, year, kwargs.get('pro_schedule'))
self.owners = kwargs.get('owners', [])

def __repr__(self):
return 'Team(%s)' % (self.team_name, )

def _fetch_roster(self, data, year):
def _fetch_roster(self, data, year, pro_schedule = None):
'''Fetch teams roster'''
self.roster.clear()
roster = data.get('entries', [])

for player in roster:
self.roster.append(Player(player, year))
self.roster.append(Player(player, year, pro_schedule))

def _fetch_schedule(self, data):
'''Fetch schedule and scores for team'''
Expand Down
Loading
Loading