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

Basketball Roto Box Score #438

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
49 changes: 36 additions & 13 deletions espn_api/basketball/box_score.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@ 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_player_lineup(self, team, data, pro_schedule, by_matchup, year):
if team not in data:
return []

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', [])]

return lineup

class H2HPointsBoxScore(BoxScore):
def __init__(self, data, pro_schedule, by_matchup, year):
Expand All @@ -45,7 +35,7 @@ def _get_team_data(self, team, data, pro_schedule, by_matchup, year):
team_projected = round(data[team].get('totalProjectedPointsLive', -1), 2)
else:
team_score = round(team_roster.get('appliedStatTotal', 0), 2)
lineup = self._get_player_lineup(team, data, pro_schedule, by_matchup, year)
lineup = get_player_lineup(data[team], pro_schedule, by_matchup, year)

return (team_score, team_projected, lineup)

Expand All @@ -72,10 +62,43 @@ def _get_team_data(self, team, data, pro_schedule, by_matchup, year):
'result': stat_dict['result']
}

lineup = self._get_player_lineup(team, data, pro_schedule, by_matchup, year)
lineup = get_player_lineup(data[team], pro_schedule, by_matchup, year)

return (team_wins, team_ties, team_losses, team_stats, lineup)

class RotoBoxScore():
def __init__(self, data, pro_schedule, by_matchup, year):
self.teams = [self._get_team_data(team, pro_schedule, by_matchup, year) for team in data.get('teams', [])]

def _get_team_data(self, team_data, pro_schedule, by_matchup, year):
team = team_data.get('teamId', 0)
cumulative_score = team_data.get('cumulativeScore', {})
wins = cumulative_score.get('wins', 0)
ties = cumulative_score.get('ties', 0)
losses = cumulative_score.get('losses', 0)
points = team_data.get('totalPointsLive') if 'totalPointsLive' in team_data else team_data.get('totalPoints', 0)

stats = {}
for stat_key, stat_dict in cumulative_score.get('scoreByStat', {}).items():
stats[STATS_MAP.get(stat_key, stat_key)] = {
'value': stat_dict.get('score'),
'result': stat_dict.get('result'),
'rank': stat_dict.get('rank')
}
lineup = get_player_lineup(team_data, pro_schedule, by_matchup, year)

return { 'team': team, 'wins': wins, 'ties': ties, 'losses': losses, 'points': points, 'stats': stats, 'lineup': lineup }



def get_player_lineup(team_data, pro_schedule, by_matchup, year):
'''Helper function to get teams line up '''
roster_key = 'rosterForMatchupPeriod' if by_matchup else 'rosterForCurrentScoringPeriod'
roster = team_data.get(roster_key, {})
lineup = [BoxPlayer(player, pro_schedule, year) for player in roster.get('entries', [])]

return lineup

# helper function to get correct box score class
ScoringType = {'H2H_POINTS': H2HPointsBoxScore, 'H2H_CATEGORY': H2HCategoryBoxScore, 'H2H_MOST_CATEGORIES': H2HCategoryBoxScore}
ScoringType = {'H2H_POINTS': H2HPointsBoxScore, 'H2H_CATEGORY': H2HCategoryBoxScore, 'H2H_MOST_CATEGORIES': H2HCategoryBoxScore, 'ROTO': RotoBoxScore}
get_box_scoring_type_class = lambda scoring_type: ScoringType.get(scoring_type, H2HPointsBoxScore)
5 changes: 3 additions & 2 deletions espn_api/basketball/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,12 @@ def box_scores(self, matchup_period: int = None, scoring_period: int = None, mat
pro_schedule = self._get_pro_schedule(scoring_id)
box_data = [self.BoxScoreClass(matchup, pro_schedule, matchup_total, self.year) for matchup in schedule]

# TODO add mapping for roto teams
for team in self.teams:
for matchup in box_data:
if matchup.home_team == team.team_id:
if hasattr(matchup, 'home_team') and matchup.home_team == team.team_id:
matchup.home_team = team
elif matchup.away_team == team.team_id:
elif hasattr(matchup, 'away_team') and matchup.away_team == team.team_id:
matchup.away_team = team
return box_data

Expand Down
2 changes: 1 addition & 1 deletion espn_api/basketball/matchup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def _fetch_matchup_info(self, data, team):
team_live_score = None

# if stats are available
if 'cumulativeScore' in data[team].keys() and data[team]['cumulativeScore']['scoreByStat']:
if 'cumulativeScore' in data[team].keys() and data[team]['cumulativeScore'].get('scoreByStat'):

team_live_score = (data[team]['cumulativeScore']['wins'] +
data[team]['cumulativeScore']['ties']/2)
Expand Down
Empty file.
Loading