Page 1 of 2

NHL API Powershell Scripts.

Posted: Tue Jul 28, 2015 10:25 pm
by SimonT
Hi everyone.

While searching on the Internet, I came across this very interesting post (http://hfboards.hockeysfuture.com/showt ... ?t=1596119 ) regarding NHL JSON Data. So I build a Powershell V3 script to extract this data to CSV. The CSV has a lot of interesting information. Personally, I was looking for the players name and team abbreviation information to use with the “Assign Players to Team CSV” function. Here is my script.

This code doesn't work anymore. Please look below.

Code: Select all

function Get-NHLData{
Write-Host "Fethcing" $URL
$Data = Invoke-RestMethod -uri $URL
$Data.goalie | Add-Member -MemberType NoteProperty -Name Team -Value $Team
$Data.defensemen | Add-Member -MemberType NoteProperty -Name Team -Value $Team
$Data.forwards | Add-Member -MemberType NoteProperty -Name Team -Value $Team
$Data.goalie | Export-csv $File  -Append -NoTypeInformation -Force
$Data.defensemen | Export-csv $File  -Append -NoTypeInformation -Force
$Data.forwards | Export-csv $File  -Append -NoTypeInformation -Force
}

$File = "NHLData.csv"
If(Test-Path -Path $file){Remove-Item $File}
$NHLTeamArray = @("ANA","ARI","BOS","BUF","CAR","CBJ","CGY","CHI","COL","DAL","DET","EDM","FLA","LAK","MIN","MTL","NJD","NSH","NYI","NYR","OTT","PHI","PIT","SJS","STL","TBL","TOR","VAN","WPG","WSH")

ForEach($Team in $NHLTeamArray){
$URL = "http://nhlwc.cdnak.neulion.com/fs1/nhl/league/teamroster/"+$Team+"/iphone/clubroster.json"
Get-NHLData -url  $URL
}
The only value you need to change is $File variable path.

If Powershell is completely unknown to you, it’s build-in in Windows. Simply search for “Windows PowerShell ISE” on your computer, open it, copy the script in the top pane, change the $FilePath variable path at line 12 and run the script. You’ll get a very nice CSV files with a bunch of information directly from NHL.com!

Good luck and enjoy!

Edit Novembre 2017. The old code doesn't seem to work. Try this one.

Code: Select all

$File = "NHLData.csv"
If(Test-Path -Path $file){Remove-Item $File}
$JSON = Invoke-RestMethod -uri "https://statsapi.web.nhl.com/api/v1/teams?expand=team.roster&season=20172018&site=en_nhl"
$Team = $JSON.teams
ForEach($TeamData in $Team){
    $TeamAbre = $TeamData.abbreviation
    Write-host $TeamAbre -BackgroundColor Green
    ForEach($Person in $TeamData.roster.roster.person){        
        $Person | Add-Member -MemberType NoteProperty -Name Team -Value $TeamAbre
        $Person | Export-csv $File -Append -NoTypeInformation -Force
    }
}


Re: NHL API Powershell Scripts.

Posted: Thu Nov 26, 2015 1:50 pm
by opeth771
This looks awesome. but I'm getting bunch of errors:

Image

Thanks

Re: NHL API Powershell Scripts.

Posted: Thu Nov 26, 2015 1:57 pm
by opeth771
Ok I found my problem. It is because cmdlet was not introduced before version 3 of Powershell. So make sure you have version 3 or higher!

Re: NHL API Powershell Scripts.

Posted: Thu Nov 26, 2015 6:53 pm
by SimonT
opeth771 wrote:Ok I found my problem. It is because cmdlet was not introduced before version 3 of Powershell. So make sure you have version 3 or higher!
Windows PowerShell version 3 is build in Windows 8 and more.

If you have Windows Vista or 7, see here to download it : http://www.microsoft.com/en-us/download ... x?id=34595

Re: NHL API Powershell Scripts.

Posted: Thu Jun 02, 2016 9:33 am
by opeth771
Hi Simon,

I was wondering if you could maybe write us a script that fetches data from team pages like this: http://nhlwc.cdnak.neulion.com/fs1/nhl/ ... sline.json

I can grab the data but I just can't figure out how to divide the columns, so all I get is raw data.

Thanks!

Re: NHL API Powershell Scripts.

Posted: Thu Jun 02, 2016 5:59 pm
by SimonT
Hi.

With

Code: Select all

$Data = Invoke-RestMethod -uri http://nhlwc.cdnak.neulion.com/fs1/nhl/league/playerstatsline/20132014/2/ANA/iphone/playerstatsline.json
$Data.goalieData | Export-csv "GoaliesData.csv" -NoTypeInformation -Force
$Data.skaterData | Export-csv "SkatersData.csv" -NoTypeInformation -Force
Open the file with Notepad (Notepad++ is way better) and remove all the " from the file. It should open nicely in Excel after.

The split can be done in PowerShell but it's a &*%$%?$%? to do because the JSON is format to give you back a single string value with comma inside of then.

Re: NHL API Powershell Scripts.

Posted: Sat Jul 30, 2016 9:04 am
by Str_QNHL
je paste dans le top pane et je run le script.
sa ecrit le script dans le bottom pane mais sa cree pas le fichier CSV ??

Re: NHL API Powershell Scripts.

Posted: Sat Jul 30, 2016 5:58 pm
by SimonT
Essaye encore. Le code envoyais le fichier CSV dur le disque D:

J'ai édité un peu le code et ça devrait créer le fichier dans le répertoire par défault de PowerShell.

Re: NHL API Powershell Scripts.

Posted: Thu Sep 02, 2021 10:12 pm
by SimonT
This code will get you the NHL Pre-Season and Season Schedule (See GameType Value). You simply need to change the first line to change year.

Code: Select all

$JSON = Invoke-RestMethod -uri "https://statsapi.web.nhl.com/api/v1/schedule?season=20212022"
$Schedule = $JSON.Dates
$Day = 0
ForEach($ScheduleData in $Schedule){
    $Day++
    $Games = $ScheduleData.games
    ForEach($GamesData in $Games){        
        $RawData = new-object PSObject
        $RawData | Add-Member -MemberType NoteProperty -Name "Day" -Value $Day   
        $RawData | Add-Member -MemberType NoteProperty -Name "GameDate" -Value $GamesData.gameDate
        $RawData | Add-Member -MemberType NoteProperty -Name "GameType" -Value $GamesData.gameType
        $RawData | Add-Member -MemberType NoteProperty -Name "Visitor" $GamesData.teams.away.team.name
        $RawData | Add-Member -MemberType NoteProperty -Name "Home" $GamesData.teams.home.team.name
        $RawData | Export-csv "NHLData.csv" -Append -NoTypeInformation -Force -Encoding utf8
    }
}

Re: NHL API Powershell Scripts.

Posted: Thu Nov 09, 2023 5:08 pm
by laving
I got this to work just a month ago. Now I can't get it to work.

Invoke-RestMethod : The remote name could not be resolved: 'statsapi.web.nhl.com'
At line:3 char:9
+ $JSON = Invoke-RestMethod -uri "https://statsapi.web.nhl.com/api/v1/t ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

I don't really know what I'm doing. I would just just like to get this to work. :)

Re: NHL API Powershell Scripts.

Posted: Thu Nov 09, 2023 7:53 pm
by SimonT
It seems the NHL has changed their API. It'll take some time for the community to figure everything out.

Source : https://gitlab.com/dword4/nhlapi/-/blob ... new-api.md

Re: NHL API Powershell Scripts.

Posted: Thu Nov 16, 2023 4:20 pm
by SimonT
I found another link that look at NHL API (https://github.com/Zmalski/NHL-API-Refe ... nformation) but doesn't seem to allow to download the full year schedule either. Maybe the NHL stop allowing this. The best I can have right now is the schedule for 1 team.

Re: NHL API Powershell Scripts.

Posted: Fri Nov 17, 2023 2:25 pm
by laving
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

Re: NHL API Powershell Scripts.

Posted: Mon Nov 20, 2023 3:22 pm
by doogiski
SimonT wrote: Thu Nov 16, 2023 4:20 pm I found another link that look at NHL API (https://github.com/Zmalski/NHL-API-Refe ... nformation) but doesn't seem to allow to download the full year schedule either. Maybe the NHL stop allowing this. The best I can have right now is the schedule for 1 team.
Simon, it appears the API allows for a call for all games on a single day of the schedule by adding the date as a parameter at the end of the URL. IE. To get the last day of the 2023-2024 the URL is https://api-web.nhle.com/v1/schedule/2024-04-18.

I'm not familiar with PowerShell, but if anyone is interested I can write a simple script in Python to loop from the first to last days of the season to create the yearly CSV schedule moving forward.

Re: NHL API Powershell Scripts.

Posted: Mon Nov 20, 2023 3:26 pm
by doogiski
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. :)