Skip to content

Commit

Permalink
Adds skater stats summary endpoints, tests (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyjs authored Feb 8, 2024
1 parent 9998a0b commit ee4a581
Show file tree
Hide file tree
Showing 6 changed files with 1,350 additions and 3 deletions.
964 changes: 963 additions & 1 deletion nhl_api_2_2_1.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion nhlpy/_version.py
Original file line number Diff line number Diff line change
@@ -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.1"
__version__ = "2.2.2"
110 changes: 110 additions & 0 deletions nhlpy/api/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,113 @@ def team_summary(
return self.client.get_by_url("https://api.nhle.com/stats/rest/en/team/summary", query_params=q_params).json()[
"data"
]

def skater_stats_summary(
self,
start_season: str,
end_season: str,
franchise_id: int = None,
game_type_id: int = 2,
aggregate: bool = False,
sort_expr: List[dict] = None,
start: int = 0,
limit: int = 70,
fact_cayenne_exp: str = "gamesPlayed>=1",
default_cayenne_exp: str = None,
) -> List[dict]:
"""
Example: c.stats.skater_stats_summary(start_season="20232024", end_season="20232024")
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.
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
entries for each one.
:param sort_expr: Default sorting expresions. Provided as an array of key/value pairs. For example:
[
{"property": "points", "direction": "DESC"},
{"property": "gamesPlayed", "direction": "ASC"},
{"property": "playerId", "direction": "ASC"}
]
:param start: Possibly start of the retrived data, based on limit.
:param limit: How many to return.
:param fact_cayenne_exp: An anchor expression almost, default criteria. Only players with more than 1 game
played. I default this to gamesPlayed>=1, which is what the nhl.com site uses. But you can play with it.
:param default_cayenne_exp:
:return:
"""
q_params = {
"isAggregate": aggregate,
"isGame": False,
"start": start,
"limit": limit,
"factCayenneExp": fact_cayenne_exp,
}

if not sort_expr:
sort_expr = [
{"property": "points", "direction": "DESC"},
{"property": "gamesPlayed", "direction": "ASC"},
{"property": "playerId", "direction": "ASC"},
]
q_params["sort"] = urllib.parse.quote(json.dumps(sort_expr))

if not default_cayenne_exp:
default_cayenne_exp = f"gameTypeId={game_type_id} and seasonId<={end_season} and seasonId>={start_season}"
if franchise_id:
default_cayenne_exp = f"franchiseId={franchise_id} and {default_cayenne_exp}"
q_params["cayenneExp"] = default_cayenne_exp

return self.client.get_by_url("https://api.nhle.com/stats/rest/en/skater/summary", query_params=q_params).json()[
"data"
]

def skater_stats_summary_by_expression(
self,
cayenne_exp: str,
sort_expr: List[dict],
aggregate: bool = False,
start: int = 0,
limit: int = 70,
fact_cayenne_exp: str = "gamesPlayed>=1",
) -> dict:
"""
A more bare bones / raw version of skater_stats_summary. This allows for more flexibility in the query params.
You must supply your own cayenne expressions and sort expressions.
example:
sort_expr = [
{"property": "points", "direction": "DESC"},
{"property": "gamesPlayed", "direction": "ASC"},
{"property": "playerId", "direction": "ASC"}
]
cayenne_exp = "gameTypeId=2 and seasonId<=20232024 and seasonId>=20232024"
client.stats.skater_stats_summary_by_expression(cayenne_exp=expr, sort_expr=sort_expr)
:param aggregate: bool - If doing multiple years, you can choose to aggreate the date per player,
or have separate entries for each one.
:param sort_expr: A list of key/value pairs for sort criteria. As used in skater_stats_summary(), this is
in the format:
[
{"property": "points", "direction": "DESC"},
{"property": "gamesPlayed", "direction": "ASC"},
{"property": "playerId", "direction": "ASC"}
]
:param start:
:param limit:
:param fact_cayenne_exp:
:param default_cayenne_exp:
:return:
"""
q_params = {
"isAggregate": aggregate,
"isGame": False,
"start": start,
"limit": limit,
"factCayenneExp": fact_cayenne_exp,
}
q_params["sort"] = urllib.parse.quote(json.dumps(sort_expr))
q_params["cayenneExp"] = cayenne_exp
return self.client.get_by_url("https://api.nhle.com/stats/rest/en/skater/summary", query_params=q_params).json()
239 changes: 239 additions & 0 deletions nhlpy/data/team_stat_ids.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
{
"data": [
{
"id": 1,
"fullName": "Montréal Canadiens",
"teamCommonName": "Canadiens",
"teamPlaceName": "Montréal"
},
{
"id": 2,
"fullName": "Montreal Wanderers",
"teamCommonName": "Wanderers",
"teamPlaceName": "Montreal"
},
{
"id": 3,
"fullName": "St. Louis Eagles",
"teamCommonName": "Eagles",
"teamPlaceName": "St. Louis"
},
{
"id": 4,
"fullName": "Hamilton Tigers",
"teamCommonName": "Tigers",
"teamPlaceName": "Hamilton"
},
{
"id": 5,
"fullName": "Toronto Maple Leafs",
"teamCommonName": "Maple Leafs",
"teamPlaceName": "Toronto"
},
{
"id": 6,
"fullName": "Boston Bruins",
"teamCommonName": "Bruins",
"teamPlaceName": "Boston"
},
{
"id": 7,
"fullName": "Montreal Maroons",
"teamCommonName": "Maroons",
"teamPlaceName": "Montreal"
},
{
"id": 8,
"fullName": "Brooklyn Americans",
"teamCommonName": "Americans",
"teamPlaceName": "Brooklyn"
},
{
"id": 9,
"fullName": "Philadelphia Quakers",
"teamCommonName": "Quakers",
"teamPlaceName": "Philadelphia"
},
{
"id": 10,
"fullName": "New York Rangers",
"teamCommonName": "Rangers",
"teamPlaceName": "New York"
},
{
"id": 11,
"fullName": "Chicago Blackhawks",
"teamCommonName": "Blackhawks",
"teamPlaceName": "Chicago"
},
{
"id": 12,
"fullName": "Detroit Red Wings",
"teamCommonName": "Red Wings",
"teamPlaceName": "Detroit"
},
{
"id": 13,
"fullName": "Cleveland Barons",
"teamCommonName": "Barons",
"teamPlaceName": "Cleveland"
},
{
"id": 14,
"fullName": "Los Angeles Kings",
"teamCommonName": "Kings",
"teamPlaceName": "Los Angeles"
},
{
"id": 15,
"fullName": "Dallas Stars",
"teamCommonName": "Stars",
"teamPlaceName": "Dallas"
},
{
"id": 16,
"fullName": "Philadelphia Flyers",
"teamCommonName": "Flyers",
"teamPlaceName": "Philadelphia"
},
{
"id": 17,
"fullName": "Pittsburgh Penguins",
"teamCommonName": "Penguins",
"teamPlaceName": "Pittsburgh"
},
{
"id": 18,
"fullName": "St. Louis Blues",
"teamCommonName": "Blues",
"teamPlaceName": "St. Louis"
},
{
"id": 19,
"fullName": "Buffalo Sabres",
"teamCommonName": "Sabres",
"teamPlaceName": "Buffalo"
},
{
"id": 20,
"fullName": "Vancouver Canucks",
"teamCommonName": "Canucks",
"teamPlaceName": "Vancouver"
},
{
"id": 21,
"fullName": "Calgary Flames",
"teamCommonName": "Flames",
"teamPlaceName": "Calgary"
},
{
"id": 22,
"fullName": "New York Islanders",
"teamCommonName": "Islanders",
"teamPlaceName": "New York"
},
{
"id": 23,
"fullName": "New Jersey Devils",
"teamCommonName": "Devils",
"teamPlaceName": "New Jersey"
},
{
"id": 24,
"fullName": "Washington Capitals",
"teamCommonName": "Capitals",
"teamPlaceName": "Washington"
},
{
"id": 25,
"fullName": "Edmonton Oilers",
"teamCommonName": "Oilers",
"teamPlaceName": "Edmonton"
},
{
"id": 26,
"fullName": "Carolina Hurricanes",
"teamCommonName": "Hurricanes",
"teamPlaceName": "Carolina"
},
{
"id": 27,
"fullName": "Colorado Avalanche",
"teamCommonName": "Avalanche",
"teamPlaceName": "Colorado"
},
{
"id": 28,
"fullName": "Arizona Coyotes",
"teamCommonName": "Coyotes",
"teamPlaceName": "Arizona"
},
{
"id": 29,
"fullName": "San Jose Sharks",
"teamCommonName": "Sharks",
"teamPlaceName": "San Jose"
},
{
"id": 30,
"fullName": "Ottawa Senators",
"teamCommonName": "Senators",
"teamPlaceName": "Ottawa"
},
{
"id": 31,
"fullName": "Tampa Bay Lightning",
"teamCommonName": "Lightning",
"teamPlaceName": "Tampa Bay"
},
{
"id": 32,
"fullName": "Anaheim Ducks",
"teamCommonName": "Ducks",
"teamPlaceName": "Anaheim"
},
{
"id": 33,
"fullName": "Florida Panthers",
"teamCommonName": "Panthers",
"teamPlaceName": "Florida"
},
{
"id": 34,
"fullName": "Nashville Predators",
"teamCommonName": "Predators",
"teamPlaceName": "Nashville"
},
{
"id": 35,
"fullName": "Winnipeg Jets",
"teamCommonName": "Jets",
"teamPlaceName": "Winnipeg"
},
{
"id": 36,
"fullName": "Columbus Blue Jackets",
"teamCommonName": "Blue Jackets",
"teamPlaceName": "Columbus"
},
{
"id": 37,
"fullName": "Minnesota Wild",
"teamCommonName": "Wild",
"teamPlaceName": "Minnesota"
},
{
"id": 38,
"fullName": "Vegas Golden Knights",
"teamCommonName": "Golden Knights",
"teamPlaceName": "Vegas"
},
{
"id": 39,
"fullName": "Seattle Kraken",
"teamCommonName": "Kraken",
"teamPlaceName": "Seattle"
}
],
"total": 39
}
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "nhl-api-py"
version = "2.2.1"
version = "2.2.2"
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 <[email protected]>"]
readme = "README.md"
Expand Down
Loading

0 comments on commit ee4a581

Please sign in to comment.