Skip to content

Commit

Permalink
Basketball Add Moved as an Optional Output of Activity
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnDiebold committed Sep 28, 2024
1 parent 8e131e7 commit 1e04475
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
25 changes: 20 additions & 5 deletions espn_api/basketball/activity.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
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 = ''

Check warning on line 11 in espn_api/basketball/activity.py

View check run for this annotation

Codecov / codecov/patch

espn_api/basketball/activity.py#L11

Added line #L11 was not covered by tests
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:
if include_moved:
if msg['to'] in POSITION_MAP:
position = POSITION_MAP[msg['to']]

Check warning on line 20 in espn_api/basketball/activity.py

View check run for this annotation

Codecov / codecov/patch

espn_api/basketball/activity.py#L17-L20

Added lines #L17 - L20 were not covered by tests
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]

Check warning on line 25 in espn_api/basketball/activity.py

View check run for this annotation

Codecov / codecov/patch

espn_api/basketball/activity.py#L24-L25

Added lines #L24 - L25 were not covered by tests
else:
if msg_id != 188:
action = ACTIVITY_MAP[msg_id]

Check warning on line 28 in espn_api/basketball/activity.py

View check run for this annotation

Codecov / codecov/patch

espn_api/basketball/activity.py#L27-L28

Added lines #L27 - L28 were not covered by tests
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))

Check warning on line 32 in espn_api/basketball/activity.py

View check run for this annotation

Codecov / codecov/patch

espn_api/basketball/activity.py#L31-L32

Added lines #L31 - L32 were not covered by tests

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) + ')'

Check warning on line 38 in espn_api/basketball/activity.py

View check run for this annotation

Codecov / codecov/patch

espn_api/basketball/activity.py#L35-L38

Added lines #L35 - L38 were not covered by tests
else:
return ''

Check warning on line 40 in espn_api/basketball/activity.py

View check run for this annotation

Codecov / codecov/patch

espn_api/basketball/activity.py#L40

Added line #L40 was not covered by tests



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]

Check warning on line 92 in espn_api/basketball/league.py

View check run for this annotation

Codecov / codecov/patch

espn_api/basketball/league.py#L92

Added line #L92 was not covered by tests
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 = [act for act in (Activity(topic, self.player_map, self.get_team_data, include_moved=include_moved) for topic in data)if act and str(act).strip()]

Check warning on line 103 in espn_api/basketball/league.py

View check run for this annotation

Codecov / codecov/patch

espn_api/basketball/league.py#L103

Added line #L103 was not covered by tests

return activity

Expand Down

0 comments on commit 1e04475

Please sign in to comment.