Skip to content

Commit

Permalink
Merge pull request #108 from cwendt94/PlayerCardApi
Browse files Browse the repository at this point in the history
Player card api
  • Loading branch information
cwendt94 authored Sep 5, 2020
2 parents 2921198 + 236bdc9 commit 4aae66d
Show file tree
Hide file tree
Showing 9 changed files with 1,012 additions and 21 deletions.
2 changes: 2 additions & 0 deletions espn_api/base_league.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ def _fetch_players(self):
data = self.espn_request.get_pro_players()
# Map all player id's to player name
for player in data:
# two way map to find playerId's by name
self.player_map[player['id']] = player['fullName']
self.player_map[player['fullName']] = player['id']

def _get_pro_schedule(self, scoringPeriodId: int = None):
data = self.espn_request.get_pro_schedule()
Expand Down
18 changes: 5 additions & 13 deletions espn_api/football/box_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ class BoxPlayer(Player):
def __init__(self, data, pro_schedule, positional_rankings, week):
super(BoxPlayer, self).__init__(data)
self.slot_position = 'FA'
self.points = 0
self.projected_points = 0
self.pro_opponent = "None" # professional team playing against
self.pro_pos_rank = 0 # rank of professional team against player position
self.game_played = 100 # 0-100 for percent of game played
Expand All @@ -25,17 +23,11 @@ def __init__(self, data, pro_schedule, positional_rankings, week):
self.pro_opponent = PRO_TEAM_MAP[opp_id]
self.pro_pos_rank = positional_rankings[posId][str(opp_id)] if str(opp_id) in positional_rankings[posId] else 0


player_stats = player.get('stats')
for stats in player_stats:
stats_breakdown = stats.get('appliedStats') if stats.get('appliedStats') else stats.get('stats', {})
points = round(stats.get('appliedTotal', 0), 2)
if stats.get('statSourceId') == 0 and stats.get('scoringPeriodId') == week:
self.points = points
self.points_breakdown = {PLAYER_STATS_MAP.get(int(k), k):v for (k,v) in stats_breakdown.items()}
elif stats.get('statSourceId') == 1 and stats.get('scoringPeriodId') == week:
self.projected_points = points
self.projected_breakdown = {PLAYER_STATS_MAP.get(int(k), k):v for (k,v) in stats_breakdown.items()}
stats = self.stats.get(week)
self.points = stats.get('points', 0)
self.points_breakdown = stats.get('breakdown', 0)
self.projected_points = stats.get('projected_points', 0)
self.projected_breakdown = stats.get('projected_breakdown', 0)



Expand Down
20 changes: 17 additions & 3 deletions espn_api/football/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,10 @@ def recent_activity(self, size: int = 25, msg_type: str = None) -> List[Activity
'view': 'kona_league_communication'
}

filters = {"topics":{"filterType":{"value":["ACTIVITY_TRANSACTIONS"]},"limit":size,"limitPerMessageSet":{"value":25},"offset":0,"sortMessageDate":{"sortPriority":1,"sortAsc":False},"sortFor":{"sortPriority":2,"sortAsc":False},"filterDateRange":{"value":1564689600000,"additionalValue":1583110842000},"filterIncludeMessageTypeIds":{"value":msg_types}}}
filters = {"topics":{"filterType":{"value":["ACTIVITY_TRANSACTIONS"]},"limit":size,"limitPerMessageSet":{"value":25},"offset":0,"sortMessageDate":{"sortPriority":1,"sortAsc":False},"sortFor":{"sortPriority":2,"sortAsc":False},"filterIncludeMessageTypeIds":{"value":msg_types}}}
headers = {'x-fantasy-filter': json.dumps(filters)}
data = self.espn_request.league_get(extend='/communication/', params=params, headers=headers)

data = data['topics']

activity = [Activity(topic, self.player_map, self.get_team_data) for topic in data]

return activity
Expand Down Expand Up @@ -259,3 +257,19 @@ def free_agents(self, week: int=None, size: int=50, position: str=None) -> List[
positional_rankings = self._get_positional_ratings(week)

return [BoxPlayer(player, pro_schedule, positional_rankings, week) for player in players]

def player_info(self, name: str):
''' Returns Player class if name found '''
playerId = self.player_map.get(name)

if playerId is None:
return None
params = { 'view': 'kona_playercard' }
filters = {'players':{'filterIds':{'value':[playerId]}, 'filterStatsForTopScoringPeriodIds':{'value':16}}}
headers = {'x-fantasy-filter': json.dumps(filters)}

data = self.espn_request.league_get(params=params, headers=headers)

player = data['players'][0]
return Player(player)

20 changes: 19 additions & 1 deletion espn_api/football/player.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .constant import POSITION_MAP, PRO_TEAM_MAP
from .constant import POSITION_MAP, PRO_TEAM_MAP, PLAYER_STATS_MAP
from .utils import json_parsing

class Player(object):
Expand All @@ -10,12 +10,30 @@ def __init__(self, data):
self.eligibleSlots = [POSITION_MAP[pos] for pos in json_parsing(data, 'eligibleSlots')]
self.acquisitionType = json_parsing(data, 'acquisitionType')
self.proTeam = PRO_TEAM_MAP[json_parsing(data, 'proTeamId')]
self.stats = {}

# Get players main position
for pos in json_parsing(data, 'eligibleSlots'):
if (pos != 25 and '/' not in POSITION_MAP[pos]) or '/' in self.name:
self.position = POSITION_MAP[pos]
break

# set each scoring period statsd
player = data['playerPoolEntry']['player'] if 'playerPoolEntry' in data else data['player']
player_stats = player.get('stats')
for stats in player_stats:
stats_breakdown = stats.get('appliedStats') if stats.get('appliedStats') else stats.get('stats', {})
breakdown = {PLAYER_STATS_MAP.get(int(k), k):v for (k,v) in stats_breakdown.items()}
points = round(stats.get('appliedTotal', 0), 2)
scoring_period = stats.get('scoringPeriodId')
stat_source = stats.get('statSourceId')
(points_type, breakdown_type) = ('points', 'breakdown') if stat_source == 0 else ('projected_points', 'projected_breakdown')
if self.stats.get(scoring_period):
self.stats[scoring_period][points_type] = points
self.stats[scoring_period][breakdown_type] = breakdown
else:
self.stats[scoring_period] = {points_type: points, breakdown_type: breakdown}

def __repr__(self):
return 'Player(%s)' % (self.name, )

1 change: 1 addition & 0 deletions tests/football/integration/test_league.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ def test_box_scores(self):

self.assertEqual(repr(box_scores[1].away_team), 'Team(TEAM BERRY)')
self.assertEqual(repr(box_scores[1].away_lineup[1]), 'Player(Odell Beckham Jr., points:29, projected:16)')
self.assertEqual(repr(box_scores[1]), 'Box Score(Team(TEAM BERRY) at Team(TEAM HOLLAND))')
Loading

0 comments on commit 4aae66d

Please sign in to comment.