Zmalski / NHL-API-Reference

Unofficial reference for the NHL API endpoints.
MIT License
256 stars 19 forks source link

Get player nationality by player id #5

Open FrantisekHrdina opened 11 months ago

FrantisekHrdina commented 11 months ago

Hi,

is there a way how to get player nationality by player id?

Thanks in advance

Afischbacher commented 11 months ago

Hey @FrantisekHrdina,

Based on my current analysis of the new NHL Web API, one of the methods to achieve this is by calling the HTTP GET endpoint v1/player/{playerId}/landing.

Here is an example of the endpoint being called: https://api-web.nhle.com/v1/player/8478402/landing

Here is a sample response of the JSON, within the response there is information regarding the nationality of the player

   "birthDate": "1997-01-13",
    "birthCity": {
        "default": "Richmond Hill"
    },
    "birthStateProvince": {
        "default": "Ontario"
    },
    "birthCountry": "CAN",
FrantisekHrdina commented 11 months ago

Hey @Afischbacher,

Thank you for your answer.

I have noticed this one, but unfortunately, this is not the nationality, but just the birthplace, which can be different from the actual nationality.

I tried to find the players for a given nationality using the stats in this way, which is the best way I have. My use case is, that I want to have info for Czech guys every day, how they were doing, and I have no other way how to check from game info than to compare players there with the list of players from the following request. https://api.nhle.com/stats/rest/en/skater/summary?isAggregate=false&isGame=false&sort=[{"property":"points","direction":"DESC"},{"property":"goals","direction":"DESC"},{"property":"assists","direction":"DESC"},{"property":"playerId","direction":"ASC"}]&start=0&limit=50&factCayenneExp=gamesPlayed>=1&cayenneExp=nationalityCode="CZE" and seasonId=20232024

Zmalski commented 10 months ago

From what I can see, I think your approach seems to be the best way to achieve this. You retrieve a list of all players that have Czech nationality, and use the playerId from those results to fetch the game info for each player.

timo-eloranta commented 10 months ago

If you need to find out the nationality of a single skater - using the playerId - then this will work: https://api.nhle.com/stats/rest/en/skater/bios?cayenneExp=seasonId=20232024 and playerId=8478427

The response for the example playerId is:

{
  "data": [
    {
      "assists": 27,
      "birthCity": "Rauma",
      "birthCountryCode": "FIN",
      "birthDate": "1997-07-26",
      "birthStateProvinceCode": null,
      "currentTeamAbbrev": "CAR",
      "currentTeamName": "Carolina Hurricanes",
      "draftOverall": 35,
      "draftRound": 2,
      "draftYear": 2015,
      "firstSeasonForGameType": 20162017,
      "gamesPlayed": 34,
      "goals": 15,
      "height": 72,
      "isInHallOfFameYn": "N",
      "lastName": "Aho",
      "nationalityCode": "FIN",
      "playerId": 8478427,
      "points": 42,
      "positionCode": "C",
      "shootsCatches": "L",
      "skaterFullName": "Sebastian Aho",
      "weight": 176
    }
  ],
  "total": 1
}

From where you can then easily extract the nationalityCode.

In my case I wanted to get the nationalities for ALL of the players, so I could then cache them locally in my app for quick lookups. For this need these requests seem to work - for skaters and goalies respectively:

https://api.nhle.com/stats/rest/en/skater/bios?sort=[{"property":"nationalityCode","direction":"ASC_CI"},{"property":"lastName","direction":"ASC_CI"}]&limit=-1&cayenneExp=gameTypeId=2 and seasonId=20232024 and https://api.nhle.com/stats/rest/en/goalie/bios?sort=[{"property":"nationalityCode","direction":"ASC_CI"},{"property":"lastName","direction":"ASC_CI"}]&limit=-1&cayenneExp=gameTypeId=2 and seasonId=20232024

(Feel free to leave out the sort parameter in case you don't care about the sort order.)

Btw, if you know how one uses the optional include and exclude parameters in the skater and goalie endpoints, I'd be happy to learn! Perhaps one could limit the properties in the responses by using these parameters.