Skip to content

Commit

Permalink
Merge pull request #65 from xaph/32-fetch-all-players-in-league
Browse files Browse the repository at this point in the history
32 fetch all players in league
  • Loading branch information
mattdodge authored Jan 23, 2024
2 parents a26526b + 0fc5a91 commit f229399
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,16 @@ There is a general hierarchy that head-to-head leagues will follow. This hierarc
for league in ctx.get_leagues('mlb', 2019):
print(f"{league.id} - {league.name} ({league.league_type})")
```


* A **League** will contain multiple **Player** objects.
```python
from yahoofantasy import League

league = League(ctx, '388.l.25000') # Use a manual league ID or get it from league.id above
for player in league.players():
print(f"{player.name.full} - {player.display_position} - {player.editorial_team_abbr}")
```

* A **League** will contain multiple **Team** objects.
```python
from yahoofantasy import League
Expand Down
16 changes: 16 additions & 0 deletions examples/get_league_players.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from yahoofantasy import Context

c = Context()

# Get all basketball leagues belonged to 2023
leagues = c.get_leagues("nba", 2023)
# select the first league to get players from
league = leagues[0]
# Print the name of the league and whether it was private/public
print(league.name + " -- " + league.league_type)

# Iterate through standings and show every team's win/loss record
for player in league.players():
print(f"{player.name.full} - {player.display_position} - {player.editorial_team_abbr}")

print(f"{len(league.players())} players found in the league")
5 changes: 5 additions & 0 deletions examples/readme.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
print(f"{league.id} - {league.name} ({league.league_type})")
print()

print("~~~~~~~~ LEAGUE PLAYERS ~~~~~~~~")
for player in league.players():
print(f"{player.name.full} - {player.display_position} - {player.editorial_team_abbr}")
print()

# Iterate through standings and show every team's win/loss record
print("~~~~~~~~ TEAMS ~~~~~~~~")
for team in league.teams():
Expand Down
17 changes: 16 additions & 1 deletion yahoofantasy/resources/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
from .week import Week
from .draft_result import DraftResult
from .transaction import Transaction
from .player import Player


class League:
def __init__(self, ctx, league_id):
self.ctx = ctx
self.id = league_id
self.players = list()

def get_team(self, team_key):
return next((t for t in self.teams() if t.team_key == team_key), None)
Expand All @@ -28,6 +28,21 @@ def teams(self, persist_ttl=DEFAULT_TTL):
teams.append(t)
return teams

def players(self, persist_ttl=DEFAULT_TTL):
logger.debug("Looking up players")
START = 0
COUNT = 25
data = self.ctx._load_or_fetch(f"players.{self.id}.{START}", f"players;count={COUNT};start={START}", league=self.id)
players = []
while "player" in data["fantasy_content"]["league"]["players"]:
for player in data["fantasy_content"]["league"]["players"]["player"]:
p = Player(self)
from_response_object(p, player)
players.append(p)
START += COUNT
data = self.ctx._load_or_fetch(f"players.{self.id}.{START}", f"players;count={COUNT};start={START}", league=self.id)
return players

def standings(self, persist_ttl=DEFAULT_TTL):
logger.debug("Looking up standings")
data = self.ctx._load_or_fetch(
Expand Down

0 comments on commit f229399

Please sign in to comment.