Skip to content

Commit

Permalink
Merge pull request #461 from spkane31/spk/add-skip-to-api
Browse files Browse the repository at this point in the history
adding an offset parameter to support paging through all activities
  • Loading branch information
cwendt94 authored Sep 5, 2023
2 parents 8ed3355 + c22e9a2 commit 043639b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 34 deletions.
22 changes: 11 additions & 11 deletions espn_api/baseball/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@

class League(BaseLeague):
'''Creates a League instance for Public/Private ESPN league'''

ScoreTypes = {'H2H_CATEGORY': H2HCategoryBoxScore, 'H2H_POINTS': H2HPointsBoxScore}

def __init__(self, league_id: int, year: int, espn_s2=None, swid=None, fetch_league=True, debug=False):
super().__init__(league_id=league_id, year=year, sport='mlb', espn_s2=espn_s2, swid=swid, debug=debug)

self._set_scoring_class = lambda scoring_type: League.ScoreTypes.get(scoring_type, BoxScore)

self.scoring_type = None
self._box_score_class = None

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

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

Expand All @@ -46,7 +46,7 @@ def _fetch_league(self):
return data

def _fetch_teams(self, data):
'''Fetch teams in league'''
'''Fetch teams in league'''
super()._fetch_teams(data, TeamClass=Team)

# replace opponentIds in schedule with team instances
Expand All @@ -58,7 +58,7 @@ def _fetch_teams(self, data):
matchup.away_team = opponent
if matchup.home_team == opponent.team_id:
matchup.home_team = opponent

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 @@ -81,7 +81,7 @@ def scoreboard(self, matchupPeriod: int = None) -> List[Matchup]:
matchup.home_team = team
elif matchup.away_team == team.team_id:
matchup.away_team = team

return matchups

def get_team_data(self, team_id: int) -> Team:
Expand All @@ -90,7 +90,7 @@ def get_team_data(self, team_id: int) -> Team:
return team
return None

def recent_activity(self, size: int = 25, msg_type: str = None) -> List[Activity]:
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:
raise Exception('Cant use recent activity before 2019')
Expand All @@ -102,7 +102,7 @@ def recent_activity(self, size: int = 25, msg_type: str = None) -> List[Activity
'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}}}
filters = {"topics":{"filterType":{"value":["ACTIVITY_TRANSACTIONS"]},"limit":size,"limitPerMessageSet":{"value":25},"offset":offset,"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']
Expand All @@ -118,14 +118,14 @@ def free_agents(self, week: int=None, size: int=50, position: str=None, position
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,
Expand Down
16 changes: 8 additions & 8 deletions espn_api/basketball/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class League(BaseLeague):
'''Creates a League instance for Public/Private ESPN league'''
def __init__(self, league_id: int, year: int, espn_s2=None, swid=None, fetch_league=True, debug=False):
super().__init__(league_id=league_id, year=year, sport='nba', espn_s2=espn_s2, swid=swid, debug=debug)

if fetch_league:
self.fetch_league()

Expand Down Expand Up @@ -47,7 +47,7 @@ def _map_matchup_ids(self, schedule):

def _fetch_teams(self, data):
'''Fetch teams in league'''
pro_schedule = self._get_all_pro_schedule()
pro_schedule = self._get_all_pro_schedule()
super()._fetch_teams(data, TeamClass=Team, pro_schedule=pro_schedule)

# replace opponentIds in schedule with team instances
Expand All @@ -59,7 +59,7 @@ def _fetch_teams(self, data):
matchup.away_team = opponent
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()
Expand Down Expand Up @@ -104,7 +104,7 @@ def scoreboard(self, matchupPeriod: int = None) -> List[Matchup]:
matchup.home_team = team
elif matchup.away_team == team.team_id:
matchup.away_team = team

return matchups

def get_team_data(self, team_id: int) -> Team:
Expand All @@ -113,7 +113,7 @@ def get_team_data(self, team_id: int) -> Team:
return team
return None

def recent_activity(self, size: int = 25, msg_type: str = None) -> List[Activity]:
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:
raise Exception('Cant use recent activity before 2019')
Expand All @@ -125,7 +125,7 @@ def recent_activity(self, size: int = 25, msg_type: str = None) -> List[Activity
'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}}}
filters = {"topics":{"filterType":{"value":["ACTIVITY_TRANSACTIONS"]},"limit":size,"limitPerMessageSet":{"value":25},"offset":offset,"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']
Expand All @@ -141,14 +141,14 @@ def free_agents(self, week: int=None, size: int=50, position: str=None, position
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,
Expand Down
8 changes: 4 additions & 4 deletions espn_api/football/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class League(BaseLeague):
'''Creates a League instance for Public/Private ESPN league'''
def __init__(self, league_id: int, year: int, espn_s2=None, swid=None, fetch_league=True, debug=False):
super().__init__(league_id=league_id, year=year, sport='nfl', espn_s2=espn_s2, swid=swid, debug=debug)

if fetch_league:
self.fetch_league()

Expand Down Expand Up @@ -150,7 +150,7 @@ def get_team_data(self, team_id: int) -> Team:
return team
return None

def recent_activity(self, size: int = 25, msg_type: str = None) -> List[Activity]:
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:
raise Exception('Cant use recent activity before 2019')
Expand All @@ -162,7 +162,7 @@ def recent_activity(self, size: int = 25, msg_type: str = None) -> List[Activity
'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}}}
filters = {"topics":{"filterType":{"value":["ACTIVITY_TRANSACTIONS"]},"limit":size,"limitPerMessageSet":{"value":25},"offset":offset,"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']
Expand Down Expand Up @@ -296,7 +296,7 @@ def player_info(self, name: str = None, playerId: Union[int, list] = None) -> Un
return Player(data['players'][0], self.year)
if len(data['players']) > 1:
return [Player(player, self.year) for player in data['players']]

def message_board(self, msg_types: List[str] = None):
''' Returns a list of league messages'''
data = self.espn_request.get_league_message_board(msg_types)
Expand Down
4 changes: 2 additions & 2 deletions espn_api/hockey/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def get_team_data(self, team_id: int) -> Team:
return team
return None

def recent_activity(self, size: int = 25, msg_type: str = None) -> List[Activity]:
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:
raise Exception('Cant use recent activity before 2019')
Expand All @@ -103,7 +103,7 @@ def recent_activity(self, size: int = 25, msg_type: str = None) -> List[Activity
}

filters = {"topics": {"filterType": {"value": ["ACTIVITY_TRANSACTIONS"]}, "limit": size,
"limitPerMessageSet": {"value": 25}, "offset": 0,
"limitPerMessageSet": {"value": 25}, "offset": offset,
"sortMessageDate": {"sortPriority": 1, "sortAsc": False},
"sortFor": {"sortPriority": 2, "sortAsc": False},
"filterIncludeMessageTypeIds": {"value": msg_types}}}
Expand Down
18 changes: 9 additions & 9 deletions espn_api/wbasketball/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class League(BaseLeague):
'''Creates a League instance for Public/Private ESPN league'''
def __init__(self, league_id: int, year: int, espn_s2=None, swid=None, fetch_league=True, debug=False):
super().__init__(league_id=league_id, year=year, sport='wnba', espn_s2=espn_s2, swid=swid, debug=debug)

if fetch_league:
self._fetch_league()

def fetch_league(self):
data = self._fetch_league()
self._fetch_teams(data)
Expand All @@ -44,7 +44,7 @@ def _map_matchup_ids(self, schedule):


def _fetch_teams(self, data):
'''Fetch teams in league'''
'''Fetch teams in league'''
super()._fetch_teams(data, TeamClass=Team)

# replace opponentIds in schedule with team instances
Expand All @@ -56,7 +56,7 @@ def _fetch_teams(self, data):
matchup.away_team = opponent
if matchup.home_team == opponent.team_id:
matchup.home_team = opponent



def standings(self) -> List[Team]:
Expand All @@ -81,7 +81,7 @@ def scoreboard(self, matchupPeriod: int = None) -> List[Matchup]:
matchup.home_team = team
elif matchup.away_team == team.team_id:
matchup.away_team = team

return matchups

def get_team_data(self, team_id: int) -> Team:
Expand All @@ -90,7 +90,7 @@ def get_team_data(self, team_id: int) -> Team:
return team
return None

def recent_activity(self, size: int = 25, msg_type: str = None) -> List[Activity]:
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:
raise Exception('Cant use recent activity before 2019')
Expand All @@ -102,7 +102,7 @@ def recent_activity(self, size: int = 25, msg_type: str = None) -> List[Activity
'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}}}
filters = {"topics":{"filterType":{"value":["ACTIVITY_TRANSACTIONS"]},"limit":size,"limitPerMessageSet":{"value":25},"offset":offset,"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']
Expand All @@ -118,14 +118,14 @@ def free_agents(self, week: int=None, size: int=50, position: str=None, position
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,
Expand Down

0 comments on commit 043639b

Please sign in to comment.