diff --git a/espn_api/basketball/box_player.py b/espn_api/basketball/box_player.py index 7830ad04..d7c549fe 100644 --- a/espn_api/basketball/box_player.py +++ b/espn_api/basketball/box_player.py @@ -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 diff --git a/espn_api/basketball/box_score.py b/espn_api/basketball/box_score.py index 5f8b959c..e8a77d6c 100644 --- a/espn_api/basketball/box_score.py +++ b/espn_api/basketball/box_score.py @@ -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 @@ -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 @@ -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" diff --git a/espn_api/basketball/constant.py b/espn_api/basketball/constant.py index 9e5a1902..2afdf71d 100644 --- a/espn_api/basketball/constant.py +++ b/espn_api/basketball/constant.py @@ -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', diff --git a/espn_api/basketball/league.py b/espn_api/basketball/league.py index 11cd6b4a..28d71f26 100644 --- a/espn_api/basketball/league.py +++ b/espn_api/basketball/league.py @@ -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''' @@ -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: diff --git a/espn_api/basketball/player.py b/espn_api/basketball/player.py index 3899dd55..e3da0ea9 100644 --- a/espn_api/basketball/player.py +++ b/espn_api/basketball/player.py @@ -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] @@ -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 \ No newline at end of file diff --git a/espn_api/basketball/team.py b/espn_api/basketball/team.py index 1556ae16..42376214 100644 --- a/espn_api/basketball/team.py +++ b/espn_api/basketball/team.py @@ -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): diff --git a/espn_api/requests/__init__.py b/espn_api/requests/__init__.py index e69de29b..64833fa8 100644 --- a/espn_api/requests/__init__.py +++ b/espn_api/requests/__init__.py @@ -0,0 +1,3 @@ +__all__ = ['EspnFantasyRequests'] + +from .espn_requests import EspnFantasyRequests \ No newline at end of file