Skip to content

Commit

Permalink
Merge pull request #581 from JohnDiebold/basketball-moved-activity
Browse files Browse the repository at this point in the history
Basketball Add Moved as an Optional Output of Activity
  • Loading branch information
cwendt94 authored Oct 15, 2024
2 parents 8e131e7 + 993d24b commit 1c62e80
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
22 changes: 17 additions & 5 deletions espn_api/basketball/activity.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
from .constant import ACTIVITY_MAP
from .constant import ACTIVITY_MAP, POSITION_MAP

class Activity(object):
def __init__(self, data, player_map, get_team_data):
def __init__(self, data, player_map, get_team_data, include_moved=False):
self.actions = [] # List of tuples (Team, action, player)
self.date = data['date']
for msg in data['messages']:
team = ''
action = 'UNKNOWN'
player = ''
position = ''
msg_id = msg['messageTypeId']
if msg_id == 244:
team = get_team_data(msg['from'])
elif msg_id == 239:
team = get_team_data(msg['for'])
elif msg_id == 188 and include_moved and msg['to'] in POSITION_MAP:
position = POSITION_MAP[msg['to']]
else:
team = get_team_data(msg['to'])
if msg_id in ACTIVITY_MAP:
action = ACTIVITY_MAP[msg_id]
if include_moved:
action = ACTIVITY_MAP[msg_id]
elif msg_id != 188:
action = ACTIVITY_MAP[msg_id]
if msg['targetId'] in player_map:
player = player_map[msg['targetId']]
self.actions.append((team, action, player))
if action != 'UNKNOWN':
self.actions.append((team, action, player, position))

def __repr__(self):
return 'Activity(' + ' '.join("(%s,%s,%s)" % tup for tup in self.actions) + ')'
def format_action(tup):
return '(%s)' % ','.join(str(x) for x in tup if x)
if self.actions:
return 'Activity(' + ' '.join(format_action(tup) for tup in self.actions) + ')'
else:
return ''



Expand Down
1 change: 1 addition & 0 deletions espn_api/basketball/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
180: 'WAIVER ADDED',
179: 'DROPPED',
181: 'DROPPED',
188: 'MOVED',
239: 'DROPPED',
244: 'TRADED',
'FA': 178,
Expand Down
6 changes: 3 additions & 3 deletions espn_api/basketball/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ def scoreboard(self, matchupPeriod: int = None) -> List[Matchup]:

return matchups

def recent_activity(self, size: int = 25, msg_type: str = None, offset: int = 0) -> List[Activity]:
def recent_activity(self, size: int = 25, msg_type: str = None, offset: int = 0, include_moved=False) -> List[Activity]:
'''Returns a list of recent league activities (Add, Drop, Trade)'''
if self.year < 2019:
raise Exception('Cant use recent activity before 2019')

msg_types = [178,180,179,239,181,244]
msg_types = [178,180,179,239,181,244,188]
if msg_type in ACTIVITY_MAP:
msg_types = [ACTIVITY_MAP[msg_type]]
params = {
Expand All @@ -100,7 +100,7 @@ def recent_activity(self, size: int = 25, msg_type: str = None, offset: int = 0)
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, include_moved=include_moved) for topic in data]

return activity

Expand Down

0 comments on commit 1c62e80

Please sign in to comment.