Skip to content

Commit

Permalink
Merge pull request #282 from cwendt94/BballPlayerPoints
Browse files Browse the repository at this point in the history
Basketball Add Total, Avg and Projected Points to Player Object
  • Loading branch information
cwendt94 authored Nov 11, 2021
2 parents f0818f5 + 1a05b3a commit 6d39daf
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 17 deletions.
4 changes: 2 additions & 2 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):
super(BoxPlayer, self).__init__(data)
def __init__(self, data, pro_schedule, year):
super(BoxPlayer, self).__init__(data, year)
self.slot_position = 'FA'
self.pro_opponent = "None" # professional team playing against
self.game_played = 100 # 0-100 for percent of game played
Expand Down
6 changes: 3 additions & 3 deletions espn_api/basketball/box_score.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class BoxScore(object):
''' '''
def __init__(self, data, pro_schedule, by_matchup):
def __init__(self, data, pro_schedule, by_matchup, year):
self.winner = data['winner']
self.home_team = data['home']['teamId']
self.home_projected = -1 # week is over/not set
Expand All @@ -14,7 +14,7 @@ def __init__(self, data, pro_schedule, by_matchup):
self.home_projected = round(data['home'].get('totalProjectedPointsLive', -1), 2)
else:
self.home_score = round(home_roster.get('appliedStatTotal', 0), 2)
self.home_lineup = [BoxPlayer(player, pro_schedule) for player in home_roster.get('entries', [])]
self.home_lineup = [BoxPlayer(player, pro_schedule, year) for player in home_roster.get('entries', [])]

# For Leagues with bye weeks
self.away_team = 0
Expand All @@ -29,7 +29,7 @@ def __init__(self, data, pro_schedule, by_matchup):
self.away_projected = round(data['away'].get('totalProjectedPointsLive', -1), 2)
else:
self.away_score = round(away_roster.get('appliedStatTotal', 0), 2)
self.away_lineup = [BoxPlayer(player, pro_schedule) for player in away_roster.get('entries', [])]
self.away_lineup = [BoxPlayer(player, pro_schedule, year) for player in away_roster.get('entries', [])]

def __repr__(self):
away_team = self.away_team or "BYE"
Expand Down
8 changes: 8 additions & 0 deletions espn_api/basketball/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@
'45': '',
}

STAT_ID_MAP = {
'00': 'total',
'10': 'projected_total',
'01': 'last_7',
'02': 'last_15',
'03': 'last_30'
}

ACTIVITY_MAP = {
178: 'FA ADDED',
180: 'WAIVER ADDED',
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 @@ -134,7 +134,7 @@ def free_agents(self, week: int=None, size: int=50, position: str=None, position
data = self.espn_request.league_get(params=params, headers=headers)
players = data['players']

return [Player(player) for player in players]
return [Player(player, self.year) for player in players]

def box_scores(self, matchup_period: int = None, scoring_period: int = None, matchup_total: bool = True) -> List[BoxScore]:
'''Returns list of box score for a given matchup or scoring period'''
Expand Down Expand Up @@ -167,7 +167,7 @@ def box_scores(self, matchup_period: int = None, scoring_period: int = None, mat

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

for team in self.teams:
for matchup in box_data:
Expand Down
25 changes: 18 additions & 7 deletions espn_api/basketball/player.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from .constant import POSITION_MAP, PRO_TEAM_MAP, STATS_MAP
from .constant import POSITION_MAP, PRO_TEAM_MAP, STATS_MAP, STAT_ID_MAP
from espn_api.utils.utils import json_parsing

class Player(object):
'''Player are part of team'''
def __init__(self, data):
def __init__(self, data, year):
self.name = json_parsing(data, 'fullName')
self.playerId = json_parsing(data, 'id')
self.position = POSITION_MAP[json_parsing(data, 'defaultPositionId') - 1]
Expand All @@ -25,14 +25,25 @@ def __init__(self, data):
self.injured = player.get('injured', False)

for split in player.get('stats', []):
id = self._stat_id_pretty(split['id'])
applied_total = split.get('appliedTotal', 0)
applied_avg = round(split.get('appliedAverage', 0), 2)
self.stats[id] = dict(applied_total=applied_total, applied_avg=applied_avg)
if split['stats']:
self.stats[split['id']] = {}
if 'averageStats' in split.keys():
self.stats[split['id']]['avg'] = {STATS_MAP[i]: split['averageStats'][i] for i in split['averageStats'].keys() if STATS_MAP[i] != ''}
self.stats[split['id']]['total'] = {STATS_MAP[i]: split['stats'][i] for i in split['stats'].keys() if STATS_MAP[i] != ''}
self.stats[id]['avg'] = {STATS_MAP[i]: split['averageStats'][i] for i in split['averageStats'].keys() if STATS_MAP[i] != ''}
self.stats[id]['total'] = {STATS_MAP[i]: split['stats'][i] for i in split['stats'].keys() if STATS_MAP[i] != ''}
else:
self.stats[split['id']]['avg'] = None
self.stats[split['id']]['total'] = None
self.stats[id]['avg'] = None
self.stats[id]['total'] = None
self.total_points = self.stats.get(f'total_{year}', {}).get('applied_total', 0)
self.avg_points = self.stats.get(f'total_{year}', {}).get('applied_avg', 0)
self.projected_total_points= self.stats.get(f'projected_total_{year}', {}).get('applied_total', 0)
self.projected_avg_points = self.stats.get(f'projected_total_{year}', {}).get('applied_avg', 0)

def __repr__(self):
return f'Player({self.name})'

def _stat_id_pretty(self, id: str):
id_type = STAT_ID_MAP.get(id[:2])
return f'{id_type}_{id[2:]}' if id_type else id
6 changes: 3 additions & 3 deletions espn_api/basketball/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ def __init__(self, data, member, roster, schedule, year):
if 'logo' in data:
self.logo_url = data['logo']

self._fetch_roster(roster)
self._fetch_roster(roster, year)
self._fetch_schedule(schedule)

def __repr__(self):
return f'Team({self.team_name})'


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

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


def _fetch_schedule(self, data):
Expand Down
3 changes: 3 additions & 0 deletions espn_api/requests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__all__ = ['EspnFantasyRequests']

from .espn_requests import EspnFantasyRequests

0 comments on commit 6d39daf

Please sign in to comment.