Skip to content

Commit

Permalink
Merge pull request #119 from jwill24/basketball_recentActivity
Browse files Browse the repository at this point in the history
Basketball recent activity
  • Loading branch information
cwendt94 authored Sep 16, 2020
2 parents 189373e + 824c832 commit ba91e35
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
30 changes: 30 additions & 0 deletions espn_api/basketball/activity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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)
self.date = data['date']
for msg in data['messages']:
team = ''
action = 'UNKNOWN'
player = ''
msg_id = msg['messageTypeId']
if msg_id == 244:
team = get_team_data(msg['from'])
elif msg_id == 239:
team = get_team_data(msg['for'])
else:
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']]
self.actions.append((team, action, player))

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





12 changes: 12 additions & 0 deletions espn_api/basketball/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@
}


#ACTIVITY_MAP = {
#}

ACTIVITY_MAP = {
178: 'FA ADDED',
180: 'WAVIER ADDED',
179: 'DROPPED',
181: 'DROPPED',
239: 'DROPPED',
244: 'TRADED',
'FA': 178,
'WAVIER': 180,
'TRADED': 244
}

29 changes: 28 additions & 1 deletion espn_api/basketball/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from .player import Player
from .matchup import Matchup
from .constant import PRO_TEAM_MAP
from.activity import Activity
from .constant import POSITION_MAP, ACTIVITY_MAP

class League(BaseLeague):
'''Creates a League instance for Public/Private ESPN league'''
Expand All @@ -21,7 +23,7 @@ def __init__(self, league_id: int, year: int, espn_s2=None, swid=None, username=
def _fetch_league(self):
data = super()._fetch_league()
self.start_date = datetime.datetime.fromtimestamp(min([i[1][1]/1000 for i in self._get_pro_schedule(1).items()])).date()

self._fetch_players()
return(data)


Expand Down Expand Up @@ -66,3 +68,28 @@ def scoreboard(self, matchupPeriod: int = None) -> List[Matchup]:

return matchups

def get_team_data(self, team_id: int) -> Team:
for team in self.teams:
if team_id == team.team_id:
return team
return None

def recent_activity(self, size: int = 25, msg_type: str = None) -> 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]
if msg_type in ACTIVITY_MAP:
msg_types = [ACTIVITY_MAP[msg_type]]
params = {
'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},"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

0 comments on commit ba91e35

Please sign in to comment.