diff --git a/espn_api/basketball/activity.py b/espn_api/basketball/activity.py index 439a6175..5f0877a8 100644 --- a/espn_api/basketball/activity.py +++ b/espn_api/basketball/activity.py @@ -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 '' diff --git a/espn_api/basketball/constant.py b/espn_api/basketball/constant.py index e5c8c823..173699f7 100644 --- a/espn_api/basketball/constant.py +++ b/espn_api/basketball/constant.py @@ -129,6 +129,7 @@ 180: 'WAIVER ADDED', 179: 'DROPPED', 181: 'DROPPED', + 188: 'MOVED', 239: 'DROPPED', 244: 'TRADED', 'FA': 178, diff --git a/espn_api/basketball/league.py b/espn_api/basketball/league.py index 59cacf13..cf35d9d6 100644 --- a/espn_api/basketball/league.py +++ b/espn_api/basketball/league.py @@ -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 = { @@ -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