Page 2 of 2

Re: NHL API Powershell Scripts.

Posted: Fri Nov 24, 2023 2:20 pm
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. :)

Re: NHL API Powershell Scripts.

Posted: Wed Nov 29, 2023 2:06 pm
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

Re: NHL API Powershell Scripts.

Posted: Wed Nov 29, 2023 4:47 pm
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

Re: NHL API Powershell Scripts.

Posted: Wed Mar 13, 2024 2:31 pm
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?