NHL API Powershell Scripts.

Everything about Third Party and Add-ons Programs; request, support, suggestions, etc / Tout ce qui touche les programmes tiers: demandes, support, suggestions, etc
laving
The Crazy / Le Fou
Posts: 410
Joined: Tue Feb 28, 2006 10:37 am
Location: Oslo, Norway
Contact:

Re: NHL API Powershell Scripts.

Post by laving »

doogiski wrote: Mon Nov 20, 2023 3:26 pm
laving wrote: Fri Nov 17, 2023 2:25 pm I'm more interested in getting a list of player names for database creation. Back in the days I used to add all players manually to my database creation. I want something easier this time. :P
It looks like you can obtain this information by using the URL from the network console when viewing thestats page on NHL.com <https://www.nhl.com/stats/skaters?repor ... ageSize=50>.

IE. the API url that returns all the information to show the points stat leaders is https://api.nhle.com/stats/rest/en/skat ... E=20232024

The API has a record limit of 100 records per page so pagination will need to be implemented. Judging by the total field at the bottom of each JSON object returned, 734 players have played at least 1 NHL game this season. If you need help extracting and parsing this information, let me know. :)
You're speaking a language that I don't understand buddy. I was able to follow the instructions higher up in this thread, but my understanding stops there. :)
doogiski
New in Town / Le Ptit Nouveau
Posts: 20
Joined: Thu Sep 09, 2021 12:12 pm

Re: NHL API Powershell Scripts.

Post by doogiski »

laving wrote: Fri Nov 24, 2023 2:20 pm
doogiski wrote: Mon Nov 20, 2023 3:26 pm
laving wrote: Fri Nov 17, 2023 2:25 pm I'm more interested in getting a list of player names for database creation. Back in the days I used to add all players manually to my database creation. I want something easier this time. :P
It looks like you can obtain this information by using the URL from the network console when viewing thestats page on NHL.com <https://www.nhl.com/stats/skaters?repor ... ageSize=50>.

IE. the API url that returns all the information to show the points stat leaders is https://api.nhle.com/stats/rest/en/skat ... E=20232024

The API has a record limit of 100 records per page so pagination will need to be implemented. Judging by the total field at the bottom of each JSON object returned, 734 players have played at least 1 NHL game this season. If you need help extracting and parsing this information, let me know. :)
You're speaking a language that I don't understand buddy. I was able to follow the instructions higher up in this thread, but my understanding stops there. :)
I wrote the script in Python, but since I don't know how to script in Powershell I used ChatGPT to convert it into a Powershell script. This works when I run it only my machine. It creates a CSV of the 748 to-date that have played in at least one NHL game this season. It only returns the player id and full name right now.

Code: Select all

# API call to determine how many pages
$response = Invoke-RestMethod -Uri "https://api.nhle.com/stats/rest/en/skater/summary?isAggregate=false&isGame=false&sort=[{%22property%22:%22points%22,%22direction%22:%22DESC%22},{%22property%22:%22playerId%22,%22direction%22:%22ASC%22}]&start=0&limit=100&factCayenneExp=gamesPlayed%3E=1&cayenneExp=gameTypeId=2%20and%20seasonId%3C=20232024%20and%20seasonId%3E=20232024" -Method Get
$count = $response.total
$pages = [math]::ceiling($count / 100)

# Loop through pages
$players = @()
for ($i = 0; $i -lt $pages; $i++) {
    $urlPaged = "https://api.nhle.com/stats/rest/en/skater/summary?isAggregate=false&isGame=false&sort=[{%22property%22:%22points%22,%22direction%22:%22DESC%22},{%22property%22:%22playerId%22,%22direction%22:%22ASC%22}]&start=0&limit=$($i * 100)&factCayenneExp=gamesPlayed%3E=1&cayenneExp=gameTypeId=2%20and%20seasonId%3C=20232024%20and%20seasonId%3E=20232024"
    $responsePaged = Invoke-RestMethod -Uri $urlPaged -Method Get
    $data = $responsePaged.data

    foreach ($d in $data) {
        $players += [PsCustomObject]@{
            playerId = $d.playerId
            skaterFullName = $d.skaterFullName
        }
    }
}

# Export data to CSV
$players | Export-Csv -Path 'skater_info.csv' -NoTypeInformation
laving
The Crazy / Le Fou
Posts: 410
Joined: Tue Feb 28, 2006 10:37 am
Location: Oslo, Norway
Contact:

Re: NHL API Powershell Scripts.

Post by laving »

doogiski wrote: Wed Nov 29, 2023 2:06 pm
laving wrote: Fri Nov 24, 2023 2:20 pm
doogiski wrote: Mon Nov 20, 2023 3:26 pm

It looks like you can obtain this information by using the URL from the network console when viewing thestats page on NHL.com <https://www.nhl.com/stats/skaters?repor ... ageSize=50>.

IE. the API url that returns all the information to show the points stat leaders is https://api.nhle.com/stats/rest/en/skat ... E=20232024

The API has a record limit of 100 records per page so pagination will need to be implemented. Judging by the total field at the bottom of each JSON object returned, 734 players have played at least 1 NHL game this season. If you need help extracting and parsing this information, let me know. :)
You're speaking a language that I don't understand buddy. I was able to follow the instructions higher up in this thread, but my understanding stops there. :)
I wrote the script in Python, but since I don't know how to script in Powershell I used ChatGPT to convert it into a Powershell script. This works when I run it only my machine. It creates a CSV of the 748 to-date that have played in at least one NHL game this season. It only returns the player id and full name right now.

Code: Select all

# API call to determine how many pages
$response = Invoke-RestMethod -Uri "https://api.nhle.com/stats/rest/en/skater/summary?isAggregate=false&isGame=false&sort=[{%22property%22:%22points%22,%22direction%22:%22DESC%22},{%22property%22:%22playerId%22,%22direction%22:%22ASC%22}]&start=0&limit=100&factCayenneExp=gamesPlayed%3E=1&cayenneExp=gameTypeId=2%20and%20seasonId%3C=20232024%20and%20seasonId%3E=20232024" -Method Get
$count = $response.total
$pages = [math]::ceiling($count / 100)

# Loop through pages
$players = @()
for ($i = 0; $i -lt $pages; $i++) {
    $urlPaged = "https://api.nhle.com/stats/rest/en/skater/summary?isAggregate=false&isGame=false&sort=[{%22property%22:%22points%22,%22direction%22:%22DESC%22},{%22property%22:%22playerId%22,%22direction%22:%22ASC%22}]&start=0&limit=$($i * 100)&factCayenneExp=gamesPlayed%3E=1&cayenneExp=gameTypeId=2%20and%20seasonId%3C=20232024%20and%20seasonId%3E=20232024"
    $responsePaged = Invoke-RestMethod -Uri $urlPaged -Method Get
    $data = $responsePaged.data

    foreach ($d in $data) {
        $players += [PsCustomObject]@{
            playerId = $d.playerId
            skaterFullName = $d.skaterFullName
        }
    }
}

# Export data to CSV
$players | Export-Csv -Path 'skater_info.csv' -NoTypeInformation
Oooh. I'd be happy to try that out. :D
LHGMQ
New in Town / Le Ptit Nouveau
Posts: 9
Joined: Tue Sep 03, 2019 7:49 am

Re: NHL API Powershell Scripts.

Post by LHGMQ »

doogiski wrote: Wed Nov 29, 2023 2:06 pm
laving wrote: Fri Nov 24, 2023 2:20 pm
doogiski wrote: Mon Nov 20, 2023 3:26 pm

It looks like you can obtain this information by using the URL from the network console when viewing thestats page on NHL.com <https://www.nhl.com/stats/skaters?repor ... ageSize=50>.

IE. the API url that returns all the information to show the points stat leaders is https://api.nhle.com/stats/rest/en/skat ... E=20232024

The API has a record limit of 100 records per page so pagination will need to be implemented. Judging by the total field at the bottom of each JSON object returned, 734 players have played at least 1 NHL game this season. If you need help extracting and parsing this information, let me know. :)
You're speaking a language that I don't understand buddy. I was able to follow the instructions higher up in this thread, but my understanding stops there. :)
I wrote the script in Python, but since I don't know how to script in Powershell I used ChatGPT to convert it into a Powershell script. This works when I run it only my machine. It creates a CSV of the 748 to-date that have played in at least one NHL game this season. It only returns the player id and full name right now.

Code: Select all

# API call to determine how many pages
$response = Invoke-RestMethod -Uri "https://api.nhle.com/stats/rest/en/skater/summary?isAggregate=false&isGame=false&sort=[{%22property%22:%22points%22,%22direction%22:%22DESC%22},{%22property%22:%22playerId%22,%22direction%22:%22ASC%22}]&start=0&limit=100&factCayenneExp=gamesPlayed%3E=1&cayenneExp=gameTypeId=2%20and%20seasonId%3C=20232024%20and%20seasonId%3E=20232024" -Method Get
$count = $response.total
$pages = [math]::ceiling($count / 100)

# Loop through pages
$players = @()
for ($i = 0; $i -lt $pages; $i++) {
    $urlPaged = "https://api.nhle.com/stats/rest/en/skater/summary?isAggregate=false&isGame=false&sort=[{%22property%22:%22points%22,%22direction%22:%22DESC%22},{%22property%22:%22playerId%22,%22direction%22:%22ASC%22}]&start=0&limit=$($i * 100)&factCayenneExp=gamesPlayed%3E=1&cayenneExp=gameTypeId=2%20and%20seasonId%3C=20232024%20and%20seasonId%3E=20232024"
    $responsePaged = Invoke-RestMethod -Uri $urlPaged -Method Get
    $data = $responsePaged.data

    foreach ($d in $data) {
        $players += [PsCustomObject]@{
            playerId = $d.playerId
            skaterFullName = $d.skaterFullName
        }
    }
}

# Export data to CSV
$players | Export-Csv -Path 'skater_info.csv' -NoTypeInformation

Do you think you could adapt it so it create a file with every id? not just NHL games played?
I figured the file would be insanely huge, but updating it in STHS would discard players not created, and update every single players, even those in the farm, right?
Post Reply