Skip to content

Commit

Permalink
Merge pull request #137 from cwendt94/RecentActivityPlayerObject
Browse files Browse the repository at this point in the history
Add player object to recent activity
  • Loading branch information
cwendt94 authored Sep 30, 2020
2 parents f5c9b06 + b43ae49 commit e474742
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
15 changes: 10 additions & 5 deletions espn_api/football/activity.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from .constant import ACTIVITY_MAP

class Activity(object):
def __init__(self, data, player_map, get_team_data):
self.actions = [] # List of tuples (Team, action, player)
def __init__(self, data, player_map, get_team_data, player_info):
self.actions = [] # List of tuples (Team, action, Player)
self.date = data['date']
for msg in data['messages']:
team = ''
action = 'UNKNOWN'
player = ''
player = None
msg_id = msg['messageTypeId']
if msg_id == 244:
team = get_team_data(msg['from'])
Expand All @@ -17,8 +17,13 @@ def __init__(self, data, player_map, get_team_data):
team = get_team_data(msg['to'])
if msg_id in ACTIVITY_MAP:
action = ACTIVITY_MAP[msg_id]
if msg['targetId'] in player_map:
player = player_map[msg['targetId']]
if team:
for team_player in team.roster:
if team_player.playerId == msg['targetId']:
player = team_player
break
if not player:
player = player_info(playerId=msg['targetId'])
self.actions.append((team, action, player))

def __repr__(self):
Expand Down
4 changes: 2 additions & 2 deletions espn_api/football/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def recent_activity(self, size: int = 25, msg_type: str = None) -> List[Activity
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]
activity = [Activity(topic, self.player_map, self.get_team_data, self.player_info) for topic in data]

return activity

Expand Down Expand Up @@ -270,7 +270,7 @@ def player_info(self, name: str = None, playerId: int = None):
if playerId is None or isinstance(playerId, str):
return None
params = { 'view': 'kona_playercard' }
filters = {'players':{'filterIds':{'value':[playerId]}, 'filterStatsForTopScoringPeriodIds':{'value':16, "additionalValue":["002020", "102020"]}}}
filters = {'players':{'filterIds':{'value':[playerId]}, 'filterStatsForTopScoringPeriodIds':{'value':16, "additionalValue":["00{}".format(self.year), "10{}".format(self.year)]}}}
headers = {'x-fantasy-filter': json.dumps(filters)}

data = self.espn_request.league_get(params=params, headers=headers)
Expand Down
4 changes: 2 additions & 2 deletions tests/football/unit/test_league.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,11 @@ def test_recent_activity(self, m):
with open('tests/football/unit/data/league_recent_activity_2019.json') as f:
data = json.loads(f.read())
m.get(self.espn_endpoint + '/communication/?view=kona_league_communication', status_code=200, json=data)
m.get(self.espn_endpoint + '?view=kona_playercard', status_code=200, json=self.player_card_data)

activity = league.recent_activity()
self.assertEqual(repr(activity[0].actions[0][0]), 'Team(Perscription Mixon)')
self.assertEqual(len(repr(activity)), 2369)
self.assertEqual(len(repr(activity)), 2765)

@mock.patch.object(League, '_fetch_league')
def test_cookie_set(self, mock_fetch_league):
Expand All @@ -249,7 +250,6 @@ def test_player_info(self, m):
self.mock_setUp(m)
m.get(self.espn_endpoint + '?view=kona_playercard', status_code=200, json=self.player_card_data)


league = League(self.league_id, self.season)
league.year = 2019
# Invalid name
Expand Down

0 comments on commit e474742

Please sign in to comment.