Skip to content

Commit

Permalink
Merge pull request #446 from cwendt94/bb-boxscore-fullSchedule
Browse files Browse the repository at this point in the history
Basketball Box Score Pass Full Schedule
  • Loading branch information
cwendt94 authored Mar 26, 2023
2 parents 41780f4 + 5694656 commit 6e30d60
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
12 changes: 7 additions & 5 deletions espn_api/basketball/box_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

class BoxPlayer(Player):
'''player with extra data from a matchup'''
def __init__(self, data, pro_schedule, year):
super(BoxPlayer, self).__init__(data, year)
def __init__(self, data, pro_schedule, year, scoring_period):
super(BoxPlayer, self).__init__(data, year, pro_schedule)
self.slot_position = 'FA'
self.pro_opponent = "None" # professional team playing against
self.game_played = 100 # 0-100 for percent of game played
Expand All @@ -16,9 +16,11 @@ def __init__(self, data, pro_schedule, year):
self.slot_position = POSITION_MAP[data['lineupSlotId']]

player = data['playerPoolEntry']['player'] if 'playerPoolEntry' in data else data['player']
if player['proTeamId'] in pro_schedule:
(opp_id, date) = pro_schedule[player['proTeamId']]
self.game_played = 100 if datetime.now() > datetime.fromtimestamp(date/1000.0) + timedelta(hours=3) else 0
pro_id = player['proTeamId']
if pro_id in pro_schedule and str(scoring_period) in pro_schedule[pro_id]:
game = pro_schedule[pro_id][str(scoring_period)][0]
opp_id = game['awayProTeamId'] if game['awayProTeamId'] != player['proTeamId'] else game['homeProTeamId']
self.game_played = 100 if datetime.now() > datetime.fromtimestamp(game['date']/1000.0) + timedelta(hours=3) else 0
self.pro_opponent = PRO_TEAM_MAP[opp_id]

player_stats = player.get('stats', [])
Expand Down
13 changes: 7 additions & 6 deletions espn_api/basketball/box_score.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

class BoxScore(ABC):
''' '''
def __init__(self, data):
def __init__(self, data, scoring_period):
self.winner = data.get('winner', 'UNDECIDED')
self.home_team = data.get('home', {}).get('teamId', 0)
self.away_team = data.get('away', {}).get('teamId', 0)
self.scoring_period = scoring_period

def __repr__(self):
away_team = self.away_team or "BYE"
Expand All @@ -21,13 +22,13 @@ def _get_player_lineup(self, team, data, pro_schedule, by_matchup, year):

roster_key = 'rosterForMatchupPeriod' if by_matchup else 'rosterForCurrentScoringPeriod'
roster = data[team].get(roster_key, {})
lineup = [BoxPlayer(player, pro_schedule, year) for player in roster.get('entries', [])]
lineup = [BoxPlayer(player, pro_schedule, year, self.scoring_period) for player in roster.get('entries', [])]

return lineup

class H2HPointsBoxScore(BoxScore):
def __init__(self, data, pro_schedule, by_matchup, year):
super().__init__(data)
def __init__(self, data, pro_schedule, by_matchup, year, scoring_period = 0):
super().__init__(data, scoring_period)

(self.home_score, self.home_projected, self.home_lineup) = self._get_team_data('home', data, pro_schedule, by_matchup, year)

Expand All @@ -50,8 +51,8 @@ def _get_team_data(self, team, data, pro_schedule, by_matchup, year):
return (team_score, team_projected, lineup)

class H2HCategoryBoxScore(BoxScore):
def __init__(self, data, pro_schedule, by_matchup, year):
super().__init__(data)
def __init__(self, data, pro_schedule, by_matchup, year, scoring_period = 0):
super().__init__(data, scoring_period)

(self.home_wins, self.home_ties, self.home_losses, self.home_stats, self.home_lineup) = self._get_team_data('home', data, pro_schedule, by_matchup, year)

Expand Down
4 changes: 2 additions & 2 deletions espn_api/basketball/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ def box_scores(self, matchup_period: int = None, scoring_period: int = None, mat
data = self.espn_request.league_get(params=params, headers=headers)

schedule = data['schedule']
pro_schedule = self._get_pro_schedule(scoring_id)
box_data = [self.BoxScoreClass(matchup, pro_schedule, matchup_total, self.year) for matchup in schedule]
pro_schedule = self._get_all_pro_schedule()
box_data = [self.BoxScoreClass(matchup, pro_schedule, matchup_total, self.year, scoring_id) for matchup in schedule]

for team in self.teams:
for matchup in box_data:
Expand Down

0 comments on commit 6e30d60

Please sign in to comment.