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

feat: Pull Draft/Pick to Base League and Add for All Sports #531

Merged
merged 4 commits into from
Feb 26, 2024
Merged
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
29 changes: 29 additions & 0 deletions espn_api/base_league.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import List, Tuple

from .base_settings import BaseSettings
from .base_pick import BasePick
from .utils.logger import Logger
from .requests.espn_requests import EspnFantasyRequests

Expand Down Expand Up @@ -45,6 +46,28 @@ def _fetch_league(self, SettingsClass = BaseSettings):
self.members = data.get('members', [])
return data

def _fetch_draft(self):
'''Creates list of Pick objects from the leagues draft'''
data = self.espn_request.get_league_draft()

# League has not drafted yet
if not data.get('draftDetail', {}).get('drafted'):
return

picks = data.get('draftDetail', {}).get('picks', [])
for pick in picks:
team = self.get_team_data(pick.get('teamId'))
playerId = pick.get('playerId')
playerName = ''
if playerId in self.player_map:
playerName = self.player_map[playerId]
round_num = pick.get('roundId')
round_pick = pick.get('roundPickNumber')
bid_amount = pick.get('bidAmount')
keeper_status = pick.get('keeper')
nominatingTeam = self.get_team_data(pick.get('nominatingTeamId'))
self.draft.append(BasePick(team, playerId, playerName, round_num, round_pick, bid_amount, keeper_status, nominatingTeam))

def _fetch_teams(self, data, TeamClass, pro_schedule = None):
'''Fetch teams in league'''
self.teams = []
Expand Down Expand Up @@ -102,3 +125,9 @@ def _get_all_pro_schedule(self):
def standings(self) -> List:
standings = sorted(self.teams, key=lambda x: x.final_standing if x.final_standing != 0 else x.standing, reverse=False)
return standings

def get_team_data(self, team_id: int) -> List:
for team in self.teams:
if team_id == team.team_id:
return team
return None
4 changes: 2 additions & 2 deletions espn_api/basketball/pick.py → espn_api/base_pick.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

class Pick(object):
class BasePick(object):
''' Pick represents a pick in draft '''
def __init__(self, team, playerId, playerName, round_num, round_pick, bid_amount, keeper_status, nominatingTeam):
self.team = team
Expand All @@ -15,4 +15,4 @@ def __repr__(self):
return 'Pick(R:%s P:%s, %s, %s)' % (self.round_num, self.round_pick, self.playerName, self.team)

def auction_repr(self):
return ', '.join(map(str, [self.team.owner, self.playerId, self.playerName, self.bid_amount, self.keeper_status]))
return ', '.join(map(str, [self.team, self.playerId, self.playerName, self.bid_amount, self.keeper_status]))
12 changes: 2 additions & 10 deletions espn_api/baseball/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ def __init__(self, league_id: int, year: int, espn_s2=None, swid=None, fetch_lea
self._box_score_class = None

if fetch_league:
data = self._fetch_league()
self.scoring_type = data['settings']['scoringSettings']['scoringType']
self._fetch_teams(data)

self.fetch_league()
if self._box_score_class is None:
self._box_score_class = self._set_scoring_class(self.scoring_type)

Expand All @@ -39,6 +36,7 @@ def fetch_league(self):
self.scoring_type = data['settings']['scoringSettings']['scoringType']
self._fetch_teams(data)
self._box_score_class = self._set_scoring_class(self.scoring_type)
super()._fetch_draft()

def _fetch_league(self):
data = super()._fetch_league()
Expand Down Expand Up @@ -84,12 +82,6 @@ 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, offset: int = 0) -> List[Activity]:
'''Returns a list of recent league activities (Add, Drop, Trade)'''
if self.year < 2019:
Expand Down
31 changes: 1 addition & 30 deletions espn_api/basketball/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from .team import Team
from .player import Player
from .matchup import Matchup
from .pick import Pick
from .box_score import get_box_scoring_type_class, BoxScore
from .constant import PRO_TEAM_MAP
from .activity import Activity
Expand All @@ -22,7 +21,7 @@ def __init__(self, league_id: int, year: int, espn_s2=None, swid=None, fetch_lea
def fetch_league(self):
data = self._fetch_league()
self._fetch_teams(data)
self._fetch_draft()
super()._fetch_draft()

self.BoxScoreClass = get_box_scoring_type_class(self.settings.scoring_type)

Expand Down Expand Up @@ -60,28 +59,6 @@ def _fetch_teams(self, data):
if matchup.home_team == opponent.team_id:
matchup.home_team = opponent

def _fetch_draft(self):
'''Creates list of Pick objects from the leagues draft'''
data = self.espn_request.get_league_draft()

# League has not drafted yet
if not data['draftDetail']['drafted']:
return

picks = data['draftDetail']['picks']
for pick in picks:
team = self.get_team_data(pick['teamId'])
playerId = pick['playerId']
playerName = ''
if playerId in self.player_map:
playerName = self.player_map[playerId]
round_num = pick['roundId']
round_pick = pick['roundPickNumber']
bid_amount = pick['bidAmount']
keeper_status = pick['keeper']
nominatingTeam = self.get_team_data(pick['nominatingTeamId'])
self.draft.append(Pick(team, playerId, playerName, round_num, round_pick, bid_amount, keeper_status, nominatingTeam))

def standings(self) -> List[Team]:
standings = sorted(self.teams, key=lambda x: x.final_standing if x.final_standing != 0 else x.standing, reverse=False)
return standings
Expand All @@ -107,12 +84,6 @@ 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, offset: int = 0) -> List[Activity]:
'''Returns a list of recent league activities (Add, Drop, Trade)'''
if self.year < 2019:
Expand Down
2 changes: 0 additions & 2 deletions espn_api/football/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
'Team',
'Matchup',
'Player',
'Pick',
'BoxPlayer'
]

from .league import League
from .team import Team
from .matchup import Matchup
from .player import Player
from .pick import Pick
from .box_player import BoxPlayer
30 changes: 1 addition & 29 deletions espn_api/football/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from ..base_league import BaseLeague
from .team import Team
from .matchup import Matchup
from .pick import Pick
from .box_score import BoxScore
from .box_player import BoxPlayer
from .player import Player
Expand Down Expand Up @@ -41,7 +40,7 @@ def _fetch_league(self):
self.nfl_week = data['status']['latestScoringPeriod']
self._fetch_players()
self._fetch_teams(data)
self._fetch_draft()
super()._fetch_draft()

def _fetch_teams(self, data):
'''Fetch teams in league'''
Expand All @@ -61,28 +60,6 @@ def _fetch_teams(self, data):
mov = team.scores[week] - opponent.scores[week]
team.mov.append(mov)

def _fetch_draft(self):
'''Creates list of Pick objects from the leagues draft'''
data = self.espn_request.get_league_draft()

# League has not drafted yet
if not data['draftDetail']['drafted']:
return

picks = data['draftDetail']['picks']
for pick in picks:
team = self.get_team_data(pick['teamId'])
playerId = pick['playerId']
playerName = ''
if playerId in self.player_map:
playerName = self.player_map[playerId]
round_num = pick['roundId']
round_pick = pick['roundPickNumber']
bid_amount = pick['bidAmount']
keeper_status = pick['keeper']
nominatingTeam = self.get_team_data(pick['nominatingTeamId'])
self.draft.append(Pick(team, playerId, playerName, round_num, round_pick, bid_amount, keeper_status, nominatingTeam))

def _get_positional_ratings(self, week: int):
params = {
'view': 'mPositionalRatings',
Expand Down Expand Up @@ -249,11 +226,6 @@ def least_scored_week(self) -> Tuple[Team, int]:
least_tup = sorted(least_scored_tup, key=lambda tup: int(tup[1]), reverse=False)
return least_tup[0]

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, offset: int = 0) -> List[Activity]:
'''Returns a list of recent league activities (Add, Drop, Trade)'''
Expand Down
18 changes: 0 additions & 18 deletions espn_api/football/pick.py

This file was deleted.

6 changes: 1 addition & 5 deletions espn_api/hockey/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __init__(self, league_id: int, year: int, espn_s2=None, swid=None, fetch_lea
def fetch_league(self):
data = self._fetch_league()
self._fetch_teams(data)
super()._fetch_draft()

def _fetch_league(self):
data = super()._fetch_league()
Expand Down Expand Up @@ -84,11 +85,6 @@ 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, offset: int = 0) -> List[Activity]:
'''Returns a list of recent league activities (Add, Drop, Trade)'''
Expand Down
6 changes: 1 addition & 5 deletions espn_api/wbasketball/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(self, league_id: int, year: int, espn_s2=None, swid=None, fetch_lea
def fetch_league(self):
data = self._fetch_league()
self._fetch_teams(data)
super()._fetch_draft()

def _fetch_league(self):
data = super()._fetch_league()
Expand Down Expand Up @@ -84,11 +85,6 @@ 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, offset: int = 0) -> List[Activity]:
'''Returns a list of recent league activities (Add, Drop, Trade)'''
Expand Down
2 changes: 1 addition & 1 deletion tests/football/unit/test_league.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ def test_draft(self, m):

first_pick = league.draft[0]
third_pick = league.draft[2]
self.assertEqual(repr(first_pick), 'Pick(Le\'Veon Bell, Team(Rollin\' With Mahomies))')
self.assertEqual(repr(first_pick), 'Pick(R:1 P:1, Le\'Veon Bell, Team(Rollin\' With Mahomies))')
self.assertEqual(third_pick.round_num, 1)
self.assertEqual(third_pick.round_pick, 3)
self.assertEqual(third_pick.auction_repr(), 'Team(Goin\' HAM Newton), 13934, Antonio Brown, 0, False')
Expand Down
2 changes: 1 addition & 1 deletion tests/football/unit/test_past_league.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_draft(self, m):

first_pick = league.draft[0]
third_pick = league.draft[2]
self.assertEqual(repr(first_pick), 'Pick(Eddie Lacy, Team(Show Me Your TD\'s))')
self.assertEqual(repr(first_pick), 'Pick(R:1 P:1, Eddie Lacy, Team(Show Me Your TD\'s))')
self.assertEqual(third_pick.round_num, 1)
self.assertEqual(third_pick.round_pick, 3)

Expand Down
Loading
Loading