Skip to content

Commit

Permalink
Merge pull request #155 from cwendt94/baskeballFreeAgents
Browse files Browse the repository at this point in the history
Basketball Free Agents
  • Loading branch information
cwendt94 authored Dec 10, 2020
2 parents cb66e7d + df6df5e commit f652a04
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
18 changes: 18 additions & 0 deletions espn_api/basketball/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@
11: 'UT',
12: 'BE',
13: 'IR',
14: '',
15: 'Rookie',
# reverse
'PG': 0,
'SG': 1,
'SF': 2,
'PF': 3,
'C': 4,
'G': 5,
'F': 6,
'SG/SF': 7,
'G/F': 8,
'PF/C': 9,
'F/C': 10,
'UT': 11,
'BE': 12,
'IR': 13,
'Rookie': 15
}

PRO_TEAM_MAP = {
Expand Down
28 changes: 28 additions & 0 deletions espn_api/basketball/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,31 @@ def recent_activity(self, size: int = 25, msg_type: str = None) -> List[Activity
activity = [Activity(topic, self.player_map, self.get_team_data) for topic in data]

return activity

def free_agents(self, week: int=None, size: int=50, position: str=None, position_id: int=None) -> List[Player]:
'''Returns a List of Free Agents for a Given Week\n
Should only be used with most recent season'''

if self.year < 2019:
raise Exception('Cant use free agents before 2019')
if not week:
week = self.current_week

slot_filter = []
if position and position in POSITION_MAP:
slot_filter = [POSITION_MAP[position]]
if position_id:
slot_filter.append(position_id)


params = {
'view': 'kona_player_info',
'scoringPeriodId': week,
}
filters = {"players":{"filterStatus":{"value":["FREEAGENT","WAIVERS"]},"filterSlotIds":{"value":slot_filter},"limit":size,"sortPercOwned":{"sortPriority":1,"sortAsc":False},"sortDraftRanks":{"sortPriority":100,"sortAsc":True,"value":"STANDARD"}}}
headers = {'x-fantasy-filter': json.dumps(filters)}

data = self.espn_request.league_get(params=params, headers=headers)
players = data['players']

return [Player(player) for player in players]
22 changes: 10 additions & 12 deletions espn_api/basketball/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,16 @@ def __init__(self, data):
# 1020XX is projected season stats for 20XX
# I think 0120XX, 0220XX, and 0320XX are last 7, last 15, and last 30
# but I haven't figured out which yet (too season in season!)

if 'stats' in data['playerPoolEntry']['player'].keys():
for split in data['playerPoolEntry']['player']['stats']:

if split['stats']:
self.stats[split['id']] = {}
if 'averageStats' in split.keys():
self.stats[split['id']]['avg'] = {STATS_MAP[i]: split['averageStats'][i] for i in split['averageStats'].keys() if STATS_MAP[i] != ''}
self.stats[split['id']]['total'] = {STATS_MAP[i]: split['stats'][i] for i in split['stats'].keys() if STATS_MAP[i] != ''}
else:
self.stats[split['id']]['avg'] = None
self.stats[split['id']]['total'] = None
player = data['playerPoolEntry']['player'] if 'playerPoolEntry' in data else data['player']
for split in player.get('stats', []):
if split['stats']:
self.stats[split['id']] = {}
if 'averageStats' in split.keys():
self.stats[split['id']]['avg'] = {STATS_MAP[i]: split['averageStats'][i] for i in split['averageStats'].keys() if STATS_MAP[i] != ''}
self.stats[split['id']]['total'] = {STATS_MAP[i]: split['stats'][i] for i in split['stats'].keys() if STATS_MAP[i] != ''}
else:
self.stats[split['id']]['avg'] = None
self.stats[split['id']]['total'] = None

def __repr__(self):
return 'Player(%s)' % (self.name, )
Expand Down
8 changes: 7 additions & 1 deletion tests/basketball/integration/test_league.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ def test_league_scoreboard(self):

self.assertEqual(scores[0].home_final_score, 4240.0)
self.assertEqual(scores[0].away_final_score, 2965.0)

def test_league_free_agents(self):
league = League(411647, 2019)
free_agents = league.free_agents()

self.assertNotEqual(len(free_agents), 0)

def test_past_league(self):
league = League(411647, 2017)
Expand All @@ -26,4 +32,4 @@ def test_past_league_scoreboard(self):
scores = league.scoreboard()

self.assertTrue(scores[0].home_final_score > 0)
self.assertTrue(scores[0].away_final_score > 0)
self.assertTrue(scores[0].away_final_score > 0)

0 comments on commit f652a04

Please sign in to comment.