Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added League Function to get All Players #283

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions espn_api/basketball/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,15 @@ def recent_activity(self, size: int = 25, msg_type: str = None) -> List[Activity

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
def players(self, week: int=None, size: int=50, position: str=None, position_id: int=None, types: list=[]) -> List[Player]:
'''Returns a List of Players\n
Should only be used with most recent season'''

if self.year < 2019:
raise Exception('Cant use free agents before 2019')
for type in types:
if not type in {"FREEAGENT", "WAIVERS", "ONTEAM"}:
raise Exception('Invalid type: {}'.format(type))
if not week:
week = self.current_week

Expand All @@ -128,7 +131,7 @@ def free_agents(self, week: int=None, size: int=50, position: str=None, position
'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"}}}
filters = {"players":{"filterStatus":{"value":types},"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)
Expand Down
17 changes: 15 additions & 2 deletions tests/basketball/integration/test_league.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from unittest import TestCase
import unittest
from espn_api.basketball import League

# Integration test to make sure ESPN's API didn't change
Expand All @@ -16,11 +17,23 @@ 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):
def test_league_players(self):
league = League(411647, 2019)
free_agents = league.free_agents()
# set size=1000 to get all players (doesn't have to be 1000, just needs to be large)
free_agents = league.players(size=1000, types=["FREEAGENT"])
waivers = league.players(size=1000, types=["WAIVERS"])
on_team = league.players(size=1000, types=["ONTEAM"])
all1 = league.players(size=1000, types=["FREEAGENT", "WAIVERS", "ONTEAM"])
all2 = league.players(size=1000)

self.assertNotEqual(len(free_agents), 0)

self.assertNotEqual(len(on_team), 0)

self.assertEqual(len(all1), len(all2))
self.assertNotEqual(len(all2), 0)

self.assertEqual(len(free_agents)+len(waivers)+len(on_team), len(all2))

def test_league_box_scores(self):
league = League(411647, 2019)
Expand Down