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

2018 Fantasy Football API Update #615

Merged
merged 17 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 0 additions & 2 deletions espn_api/base_league.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def __repr__(self):

def _fetch_league(self, SettingsClass = BaseSettings):
data = self.espn_request.get_league()

self.currentMatchupPeriod = data['status']['currentMatchupPeriod']
self.scoringPeriodId = data['scoringPeriodId']
self.firstScoringPeriod = data['status']['firstScoringPeriod']
Expand All @@ -49,7 +48,6 @@ def _fetch_league(self, SettingsClass = BaseSettings):
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
Expand Down
44 changes: 43 additions & 1 deletion espn_api/requests/espn_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ def __init__(self, sport: str, year: int, league_id: int, cookies: dict = None,
self.LEAGUE_ENDPOINT += "/leagueHistory/" + str(league_id) + "?seasonId=" + str(year)
else:
self.LEAGUE_ENDPOINT += "/seasons/" + str(year) + "/segments/0/leagues/" + str(league_id)


# Try the first endpoint and fall back to the second if the first fails
self.check_league_endpoint()
def league_get(self, params: dict = None, headers: dict = None, extend: str = ''):
endpoint = self.LEAGUE_ENDPOINT + extend
r = requests.get(endpoint, params=params, headers=headers, cookies=self.cookies)
Expand All @@ -73,6 +75,10 @@ def get_league(self):
'view': ['mTeam', 'mRoster', 'mMatchup', 'mSettings', 'mStandings']
}
data = self.league_get(params=params)
# Check if the data is a list (which happens when year is 2018 or earlier)
if isinstance(data, list):
cwendt94 marked this conversation as resolved.
Show resolved Hide resolved
# If it's a list, we assume the relevant data is at index 0
data = data[0]
return data

def get_pro_schedule(self):
Expand All @@ -99,6 +105,10 @@ def get_league_draft(self):
'view': 'mDraftDetail',
}
data = self.league_get(params=params)
# Check if the data is a list (which happens when year is 2018 in some leagues)
if isinstance(data, list):
# If it's a list, we assume the relevant data is at index 0
data = data[0]
return data

def get_league_message_board(self, msg_types = None):
Expand Down Expand Up @@ -130,8 +140,40 @@ def get_player_card(self, playerIds: List[int], max_scoring_period: int, additio
headers = {'x-fantasy-filter': json.dumps(filters)}

data = self.league_get(params=params, headers=headers)

# Check if the data is a list (which happens when year is 2018 or earlier)
if isinstance(data, list):
# If it's a list, we assume the relevant data is at index 0
data = data[0]
return data

def check_league_endpoint(self):
# First, try the current LEAGUE_ENDPOINT
r = requests.get(self.LEAGUE_ENDPOINT, cookies=self.cookies)

# If the response is 401 Unauthorized, switch to the other endpoint
if r.status_code == 401:
# If the current LEAGUE_ENDPOINT was using the /leagueHistory/ endpoint, switch to the /seasons/{year}/segments/0 endpoint
if "/leagueHistory/" in self.LEAGUE_ENDPOINT:
alternate_endpoint = self.LEAGUE_ENDPOINT.replace("/leagueHistory/", "/seasons/" + str(self.year) + "/segments/0/leagues/")
else:
# Otherwise, switch to the /leagueHistory/{league_id}?seasonId={year} endpoint
alternate_endpoint = self.LEAGUE_ENDPOINT.replace("/seasons/" + str(self.year) + "/segments/0/leagues/", "/leagueHistory/") + "?seasonId=" + str(self.year)

# Try the alternate endpoint
r = requests.get(alternate_endpoint, cookies=self.cookies)

# If the alternate endpoint works (status 200), update the LEAGUE_ENDPOINT
if r.status_code == 200:
self.LEAGUE_ENDPOINT = alternate_endpoint
if self.logger:
self.logger.logging.info(f"Switched to alternate endpoint: {self.LEAGUE_ENDPOINT}")
else:
# If both endpoints fail, log the error and raise an exception
if self.logger:
self.logger.logging.error(f"Both endpoints failed with status: {r.status_code}")
raise Exception(f"Both endpoints for league {self.league_id} failed with status {r.status_code}")

# Username and password no longer works using their API without using google recaptcha
# Possibly revisit in future if anything changes

Expand Down
Loading