-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Support for Unofficial Endpoints (#12)
### Added - API Clients for unofficial endpoints - Examples for new API Clients - Enum values ### Fixed - Missing imports
- Loading branch information
1 parent
6c0b5a2
commit 56c3110
Showing
45 changed files
with
1,565 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from sleeper.api.unofficial import UPlayerAPIClient | ||
from sleeper.enum import Sport | ||
from sleeper.enum.nfl import NFLPosition | ||
from sleeper.model import PlayerStats | ||
|
||
if __name__ == "__main__": | ||
# get player stats for a player for a particular sport, week, and season | ||
player_stats_week: PlayerStats = UPlayerAPIClient.get_player_stats(sport=Sport.NFL, | ||
player_id="1234", | ||
season="2020", | ||
week=1) | ||
|
||
# get player stats for a player for the entire season in a particular sport and season | ||
player_stats_season: PlayerStats = UPlayerAPIClient.get_player_stats(sport=Sport.NFL, | ||
player_id="1234", | ||
season="2020") | ||
|
||
# get player projections for a player for a particular sport, week, and season | ||
player_projections_week: PlayerStats = UPlayerAPIClient.get_player_projections(sport=Sport.NFL, | ||
player_id="1234", | ||
season="2020", | ||
week=1) | ||
|
||
# get player projections for a player for the entire season in a particular sport and season | ||
player_projections_season: PlayerStats = UPlayerAPIClient.get_player_projections(sport=Sport.NFL, | ||
player_id="1234", | ||
season="2020") | ||
|
||
# get all player stats for a particular sport, season, and week | ||
all_player_stats: list[PlayerStats] = UPlayerAPIClient.get_all_player_stats(sport=Sport.NFL, | ||
season="2020", | ||
week=1) | ||
|
||
# get all player stats for QBs and RBs for a particular sport, season, and week | ||
all_player_stats_qbs_rbs: list[PlayerStats] = UPlayerAPIClient.get_all_player_stats(sport=Sport.NFL, | ||
season="2020", | ||
week=1, | ||
positions=[NFLPosition.QB, | ||
NFLPosition.RB]) | ||
|
||
# get all player projections for a particular sport, season, and week | ||
all_player_projections: list[PlayerStats] = UPlayerAPIClient.get_all_player_projections(sport=Sport.NFL, | ||
season="2020", | ||
week=1) | ||
|
||
# get all player projections for QBs and RBs for a particular sport, season, and week | ||
all_player_projections_qbs_rbs: list[PlayerStats] = UPlayerAPIClient.get_all_player_projections(sport=Sport.NFL, | ||
season="2020", | ||
week=1, | ||
positions=[ | ||
NFLPosition.QB, | ||
NFLPosition.RB]) | ||
|
||
# get a player's headshot and save locally | ||
# the file path should save to a file that has the extension '.png' | ||
UPlayerAPIClient.get_player_head_shot(sport=Sport.NFL, | ||
player_id="1234", | ||
save_to_path="C:\\Desktop\\avatar\\my_headshot.png") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from sleeper.api.unofficial import USportAPIClient | ||
from sleeper.enum import Sport | ||
from sleeper.model import Game | ||
|
||
if __name__ == "__main__": | ||
# get regular season schedule for a particular sport and season | ||
regular_season: list[Game] = USportAPIClient.get_regular_season_schedule(sport=Sport.NFL, season="2020") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from sleeper.api.unofficial import UTeamAPIClient | ||
from sleeper.enum import Sport | ||
from sleeper.enum.nfl import NFLTeam | ||
from sleeper.model import DepthChart | ||
|
||
if __name__ == "__main__": | ||
# get depth chart for a particular sport and team | ||
depth_chart: DepthChart = UTeamAPIClient.get_team_depth_chart(sport=Sport.NFL, team=NFLTeam.GB) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
from typing import Any | ||
|
||
from sleeper.api.SleeperAPIClient import SleeperAPIClient | ||
from sleeper.enum import SeasonType, PlayerPosition | ||
from sleeper.enum.Sport import Sport | ||
from sleeper.model.PlayerStats import PlayerStats | ||
|
||
|
||
class UPlayerAPIClient(SleeperAPIClient): | ||
|
||
@classmethod | ||
def get_player_stats(cls, *, sport: Sport, player_id: str, season: str, **kwargs) -> PlayerStats: | ||
""" | ||
Gets player stats for the given season OR just the given week. | ||
""" | ||
season_type: SeasonType = kwargs.pop("season_type", SeasonType.REGULAR) | ||
week: int = kwargs.pop("week", None) | ||
|
||
url = cls._build_route(cls._SLEEPER_APP_BASE_URL, None, cls._STATS_ROUTE, sport.name.lower(), cls._PLAYER_ROUTE, | ||
player_id) | ||
url = cls._add_filters(url, ("season_type", season_type.name.lower()), ("season", season), ("week", week)) | ||
|
||
response_dict = cls._get(url) | ||
if response_dict is None: | ||
error_message = f"Could not get PlayerStats for sport: '{sport.name}', player_id: '{player_id}', season_type: '{season_type}', season: '{season}'" | ||
if week is not None: | ||
error_message += f", week: '{week}'" | ||
error_message += "." | ||
raise ValueError(error_message) | ||
return PlayerStats.from_dict(response_dict) | ||
|
||
@classmethod | ||
def get_player_projections(cls, *, sport: Sport, player_id: str, season: str, **kwargs) -> PlayerStats: | ||
""" | ||
Gets player projections for the given season OR just the given week. | ||
""" | ||
season_type: SeasonType = kwargs.pop("season_type", SeasonType.REGULAR) | ||
week: int = kwargs.pop("week", None) | ||
|
||
url = cls._build_route(cls._SLEEPER_APP_BASE_URL, None, cls._PROJECTIONS_ROUTE, sport.name.lower(), | ||
cls._PLAYER_ROUTE, player_id) | ||
url = cls._add_filters(url, ("season_type", season_type.name.lower()), ("season", season), ("week", week)) | ||
|
||
response_dict = cls._get(url) | ||
if response_dict is None: | ||
error_message = f"Could not get PlayerStats for sport: '{sport.name}', player_id: '{player_id}', season_type: '{season_type}', season: '{season}'" | ||
if week is not None: | ||
error_message += f", week: '{week}'" | ||
error_message += "." | ||
raise ValueError(error_message) | ||
return PlayerStats.from_dict(response_dict) | ||
|
||
@classmethod | ||
def get_all_player_stats(cls, *, sport: Sport, season: str, week: int, **kwargs) -> list[PlayerStats]: | ||
season_type: SeasonType = kwargs.pop("season_type", SeasonType.REGULAR) | ||
positions: list[PlayerPosition] = kwargs.pop("positions", list()) | ||
|
||
url = cls._build_route(cls._SLEEPER_APP_BASE_URL, None, cls._STATS_ROUTE, sport.name.lower(), season, week) | ||
filters: list[tuple[str, Any]] = [("season_type", season_type.name.lower())] | ||
for position in positions: | ||
filters.append(("position[]", position.name.upper())) | ||
url = cls._add_filters(url, *filters) | ||
|
||
response_list = cls._get(url) | ||
if response_list is None: | ||
error_message = f"Could not get PlayerStats list for sport: '{sport.name}', season_type: '{season_type}', season: '{season}', week: '{week}'" | ||
if week is not None: | ||
error_message += f", week: '{week}'" | ||
if len(positions) > 0: | ||
error_message += f", positions: '{positions}'" | ||
error_message += "." | ||
raise ValueError(error_message) | ||
return PlayerStats.from_dict_list(response_list) | ||
|
||
@classmethod | ||
def get_all_player_projections(cls, *, sport: Sport, season: str, week: int, **kwargs) -> list[PlayerStats]: | ||
season_type: SeasonType = kwargs.pop("season_type", SeasonType.REGULAR) | ||
positions: list[PlayerPosition] = kwargs.pop("positions", list()) | ||
|
||
url = cls._build_route(cls._SLEEPER_APP_BASE_URL, None, cls._PROJECTIONS_ROUTE, sport.name.lower(), season, | ||
week) | ||
filters: list[tuple[str, Any]] = [("season_type", season_type.name.lower())] | ||
for position in positions: | ||
filters.append(("position[]", position.name.upper())) | ||
url = cls._add_filters(url, *filters) | ||
|
||
response_list = cls._get(url) | ||
if response_list is None: | ||
error_message = f"Could not get PlayerStats list for sport: '{sport.name}', season_type: '{season_type}', season: '{season}', week: '{week}'" | ||
if week is not None: | ||
error_message += f", week: '{week}'" | ||
if len(positions) > 0: | ||
error_message += f", positions: '{positions}'" | ||
error_message += "." | ||
raise ValueError(error_message) | ||
return PlayerStats.from_dict_list(response_list) | ||
|
||
@classmethod | ||
def get_player_head_shot(cls, *, sport: Sport, player_id: str, save_to_path: str) -> None: | ||
""" | ||
save_to_path should end in ".png". | ||
""" | ||
|
||
url = cls._build_route(cls._SLEEPER_CDN_BASE_URL, None, cls._CONTENT_ROUTE, sport.name.lower(), | ||
cls._PLAYERS_ROUTE, player_id) | ||
url += ".jpg" | ||
image_file = cls._get_image_file(url) | ||
image_file.save(save_to_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from sleeper.api.SleeperAPIClient import SleeperAPIClient | ||
from sleeper.enum import SeasonType | ||
from sleeper.enum.Sport import Sport | ||
from sleeper.model.Game import Game | ||
|
||
|
||
class USportAPIClient(SleeperAPIClient): | ||
|
||
@classmethod | ||
def get_regular_season_schedule(cls, *, sport: Sport, season: str, **kwargs) -> list[Game]: | ||
season_type: SeasonType = kwargs.pop("season_type", SeasonType.REGULAR) | ||
url = cls._build_route(cls._SLEEPER_APP_BASE_URL, None, cls._SCHEDULE_ROUTE, sport.name.lower(), | ||
season_type.name.lower(), season) | ||
|
||
response_list = cls._get(url) | ||
if response_list is None: | ||
raise ValueError( | ||
f"Could not get Game list for sport: '{sport.name}', season: '{season}', season_type: '{season_type.name}'.") | ||
return Game.from_dict_list(response_list, sport) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from sleeper.api.SleeperAPIClient import SleeperAPIClient | ||
from sleeper.enum import SportTeam | ||
from sleeper.enum.Sport import Sport | ||
from sleeper.model.DepthChart import DepthChart | ||
|
||
|
||
class UTeamAPIClient(SleeperAPIClient): | ||
|
||
@classmethod | ||
def get_team_depth_chart(cls, *, sport: Sport, team: SportTeam) -> DepthChart: | ||
url = cls._build_route(cls._SLEEPER_APP_BASE_URL, None, cls._PLAYERS_ROUTE, sport.name.lower(), | ||
team.name.lower(), cls._DEPTH_CHART_ROUTE) | ||
|
||
response_dict = cls._get(url) | ||
if response_dict is None: | ||
raise ValueError(f"Could not get DepthChart for sport: '{sport.name}', team: '{team.name}'") | ||
return DepthChart.model(sport).from_dict(response_dict) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .UPlayerAPIClient import UPlayerAPIClient | ||
from .USportAPIClient import USportAPIClient | ||
from .UTeamAPIClient import UTeamAPIClient |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from __future__ import annotations | ||
|
||
from enum import unique | ||
|
||
from sleeper.enum.ModelEnum import ModelEnum | ||
|
||
|
||
@unique | ||
class Category(ModelEnum): | ||
PROJ = "PROJ" | ||
STAT = "STAT" | ||
|
||
@classmethod | ||
def from_str(cls, s: str) -> Category: | ||
if s.upper() == "PROJ": | ||
return Category.PROJ | ||
elif s.upper() == "STAT": | ||
return Category.STAT | ||
else: | ||
cls._handle_unknown_value(Category, s) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from __future__ import annotations | ||
|
||
from enum import unique | ||
|
||
from sleeper.enum.ModelEnum import ModelEnum | ||
|
||
|
||
@unique | ||
class Company(ModelEnum): | ||
ROTOWIRE = "ROTOWIRE" | ||
SPORTRADAR = "SPORTRADAR" | ||
|
||
@classmethod | ||
def from_str(cls, s: str) -> Company: | ||
if s.upper() == "ROTOWIRE": | ||
return Company.ROTOWIRE | ||
elif s.upper() == "SPORTRADAR": | ||
return Company.SPORTRADAR | ||
else: | ||
cls._handle_unknown_value(Company, s) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.