diff --git a/espn_api/football/box_score.py b/espn_api/football/box_score.py index e5e4384d..521e4226 100644 --- a/espn_api/football/box_score.py +++ b/espn_api/football/box_score.py @@ -6,32 +6,38 @@ def __init__(self, data, pro_schedule, positional_rankings, week, year): self.matchup_type = data.get('playoffTierType', 'NONE') self.is_playoff = self.matchup_type != 'NONE' - self.home_team = data['home']['teamId'] - self.home_projected = -1 # week is over/not set - if 'totalPointsLive' in data['home']: - self.home_score = round(data['home']['totalPointsLive'], 2) - self.home_projected = round(data['home'].get('totalProjectedPointsLive', -1), 2) - else: - self.home_score = round(data['home']['rosterForCurrentScoringPeriod']['appliedStatTotal'], 2) - home_roster = data['home']['rosterForCurrentScoringPeriod']['entries'] - self.home_lineup = [BoxPlayer(player, pro_schedule, positional_rankings, week, year) for player in home_roster] + (self.home_team, self.home_score, self.home_projected, self.home_lineup) = self._get_team_data('home', data, pro_schedule, positional_rankings, week, year) + self.home_projected = self._get_projected_score(self.home_projected, self.home_lineup) - # For Leagues with bye weeks - self.away_team = 0 - self.away_score = 0 - self.away_lineup = [] - self.away_projected = -1 # week is over/not set - if 'away' in data: - self.away_team = data['away']['teamId'] - if 'totalPointsLive' in data['away']: - self.away_score = round(data['away']['totalPointsLive'], 2) - self.away_projected = round(data['away'].get('totalProjectedPointsLive', -1), 2) - else: - self.away_score = round(data['away']['rosterForCurrentScoringPeriod']['appliedStatTotal'], 2) - away_roster = data['away']['rosterForCurrentScoringPeriod']['entries'] - self.away_lineup = [BoxPlayer(player, pro_schedule, positional_rankings, week, year) for player in away_roster] + (self.away_team, self.away_score, self.away_projected, self.away_lineup) = self._get_team_data('away', data, pro_schedule, positional_rankings, week, year) + self.away_projected = self._get_projected_score(self.away_projected, self.away_lineup) def __repr__(self): away_team = self.away_team or "BYE" home_team = self.home_team or "BYE" return f'Box Score({away_team} at {home_team})' + + def _get_projected_score(self, projected_score, lineup): + if projected_score != -1: + return projected_score + projected_score = 0 + for player in lineup: + if player.slot_position != 'BE' and player.slot_position != 'IR': + projected_score += player.projected_points + return projected_score + + def _get_team_data(self, team, data, pro_schedule, positional_rankings, week, year): + if team not in data: + return (0, 0, -1, []) + + team_id = data[team]['teamId'] + team_projected = -1 + if 'totalPointsLive' in data[team]: + team_score = round(data[team]['totalPointsLive'], 2) + team_projected = round(data[team].get('totalProjectedPointsLive', -1), 2) + else: + team_score = round(data[team]['rosterForCurrentScoringPeriod']['appliedStatTotal'], 2) + team_roster = data[team]['rosterForCurrentScoringPeriod']['entries'] + team_lineup = [BoxPlayer(player, pro_schedule, positional_rankings, week, year) for player in team_roster] + + return (team_id, team_score, team_projected, team_lineup) \ No newline at end of file