From 8a63958ae3e098217ef7f599a65f78eb7758c528 Mon Sep 17 00:00:00 2001 From: Corey Schaf Date: Tue, 13 Feb 2024 17:27:03 -0500 Subject: [PATCH] Adds /{player_id}/game-log/{season}/{game_type} endpoints (#36) * Adds /{player_id}/game-log/{season}/{game_type} endpoints * Player/Franchise/Game IDs should all be referenced as strings. Integers are not a suitable type for these. --- README.md | 4 ++-- nhlpy/_version.py | 2 +- nhlpy/api/stats.py | 16 +++++++++++++--- nhlpy/config.py | 1 + pyproject.toml | 2 +- tests/test_schedule.py | 7 +++++++ tests/test_stats.py | 14 ++++++++++++++ 7 files changed, 39 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 3537944..df1584f 100644 --- a/README.md +++ b/README.md @@ -34,9 +34,9 @@ client = NHLClient(verbose=True) # a tad more logging such as the URL being call ```python -client.club_stats_season(team_abbr="BUF") # kinda weird endpoint. +client.stats.club_stats_season(team_abbr="BUF") # kinda weird endpoint. -client.player_career_stats(player_id=8478402) +client.stats.player_career_stats(player_id="8478402") # Team Summary Stats. # These have lots of available parameters. You can also tap into the apache cayenne expressions to build custom diff --git a/nhlpy/_version.py b/nhlpy/_version.py index d1209e7..ab8df02 100644 --- a/nhlpy/_version.py +++ b/nhlpy/_version.py @@ -1,3 +1,3 @@ # Should this be driven by the main pyproject.toml file? yes, is it super convoluted? yes, can it wait? sure -__version__ = "2.2.5" +__version__ = "2.2.6" diff --git a/nhlpy/api/stats.py b/nhlpy/api/stats.py index 93ac7b4..ede4661 100644 --- a/nhlpy/api/stats.py +++ b/nhlpy/api/stats.py @@ -18,7 +18,7 @@ def club_stats_season(self, team_abbr: str) -> dict: """ return self.client.get(resource=f"club-stats-season/{team_abbr}").json() - def player_career_stats(self, player_id: int) -> dict: + def player_career_stats(self, player_id: str) -> dict: """ This returns the career stats for a player as well as player information. @@ -29,6 +29,16 @@ def player_career_stats(self, player_id: int) -> dict: """ return self.client.get(resource=f"player/{player_id}/landing").json() + def player_game_log(self, player_id: str, season_id: str, game_type: int) -> List[dict]: + """ + Returns the game log, for the given player, for the given season and game type. + :param game_type: 1 is for preseason, 2 is for regular season, 3 is for playoffs. + :param season_id: Season format of "20222023", "20232024", etc. + :param player_id: + :return: + """ + return self.client.get(resource=f"player/{player_id}/game-log/{season_id}/{game_type}").json()["gameLog"] + def team_summary( self, start_season: str, @@ -95,7 +105,7 @@ def skater_stats_summary( self, start_season: str, end_season: str, - franchise_id: int = None, + franchise_id: str = None, game_type_id: int = 2, aggregate: bool = False, sort_expr: List[dict] = None, @@ -109,7 +119,7 @@ def skater_stats_summary( c.stats.skater_stats_summary(franchise_id=10, start_season="20232024", end_season="20232024") :param start_season: Season id, in format 20202021, 20212022, etc, that will be the start of the range. :param end_season: Season id for the end range. - :param franchise_id: The ID of the franchise. Not to be confused with team_id found on other endpoints. + :param franchise_id: String, The ID of the franchise. Not to be confused with team_id found on other endpoints. This seems to be specific to the /stats apis. :param game_type_id: 2 is for regular season, 3 is for playoffs. I think 1 is for preseason. :param aggregate: If doing multiple years, you can choose to aggreate the date per player, or have separate diff --git a/nhlpy/config.py b/nhlpy/config.py index 3a3b669..e071da5 100644 --- a/nhlpy/config.py +++ b/nhlpy/config.py @@ -2,4 +2,5 @@ class ClientConfig: def __init__(self, verbose: bool = False) -> None: self.verbose = verbose self.api_web_base_url = "https://api-web.nhle.com" + self.api_base_url = "https://api.nhle.com" self.api_web_api_ver = "/v1/" diff --git a/pyproject.toml b/pyproject.toml index 4c494f2..b9304b4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "nhl-api-py" -version = "2.2.5" +version = "2.2.6" description = "NHL API. For standings, team stats, outcomes, player information. Contains each individual API endpoint as well as convience methods for easy data loading in Pandas or any ML applications." authors = ["Corey Schaf "] readme = "README.md" diff --git a/tests/test_schedule.py b/tests/test_schedule.py index a04011f..6f15a2e 100644 --- a/tests/test_schedule.py +++ b/tests/test_schedule.py @@ -36,6 +36,13 @@ def test_get_schedule_by_team_by_week(h_m, nhl_client): assert h_m.call_args[1]["url"] == "https://api-web.nhle.com/v1/club-schedule/BUF/week/now" +@mock.patch("httpx.get") +def test_get_schedule_by_team_by_week_with_date(h_m, nhl_client): + nhl_client.schedule.get_schedule_by_team_by_week(team_abbr="BUF", date="2024-02-10") + h_m.assert_called_once() + assert h_m.call_args[1]["url"] == "https://api-web.nhle.com/v1/club-schedule/BUF/week/2024-02-10" + + @mock.patch("httpx.get") def test_get_season_schedule(h_m, nhl_client): nhl_client.schedule.get_season_schedule(team_abbr="BUF", season="20202021") diff --git a/tests/test_stats.py b/tests/test_stats.py index 771c22d..c5d5e47 100644 --- a/tests/test_stats.py +++ b/tests/test_stats.py @@ -103,3 +103,17 @@ def test_skater_stats_summary_franchise(h_m, nhl_client): "%3A%20%22playerId%22%2C%20%22direction%22%3A%20%22ASC%22%7D%5D", "cayenneExp": "franchiseId=19 and gameTypeId=2 and seasonId<=20232024 and seasonId>=20232024", } + + +@mock.patch("httpx.get") +def test_player_game_log(h_m, nhl_client): + nhl_client.stats.player_game_log(player_id="8481528", season_id="20232024", game_type=2) + h_m.assert_called_once() + assert h_m.call_args[1]["url"] == "https://api-web.nhle.com/v1/player/8481528/game-log/20232024/2" + + +@mock.patch("httpx.get") +def test_player_game_log_playoffs(h_m, nhl_client): + nhl_client.stats.player_game_log(player_id="8481528", season_id="20232024", game_type=3) + h_m.assert_called_once() + assert h_m.call_args[1]["url"] == "https://api-web.nhle.com/v1/player/8481528/game-log/20232024/3"