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
SimonT
STHS Owner / Propriétaire du STHS
Posts: 14755
Joined: Sat Oct 08, 2005 4:18 pm
Location: Montreal, Canada
Contact:

NHL API Powershell Scripts.

Post 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
    }
}

-SimonT
Forum Administrator / Administrateur du Forum
STHS Owner / Propriètaire du STHS
English V2 & V3 Manual - Manuel V2 & V3 Français
opeth771
The Accomplished One / L'Accompli
Posts: 129
Joined: Sat Feb 25, 2012 9:49 am

Re: NHL API Powershell Scripts.

Post by opeth771 »

This looks awesome. but I'm getting bunch of errors:

Image

Thanks
opeth771
The Accomplished One / L'Accompli
Posts: 129
Joined: Sat Feb 25, 2012 9:49 am

Re: NHL API Powershell Scripts.

Post 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!
SimonT
STHS Owner / Propriétaire du STHS
Posts: 14755
Joined: Sat Oct 08, 2005 4:18 pm
Location: Montreal, Canada
Contact:

Re: NHL API Powershell Scripts.

Post 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
-SimonT
Forum Administrator / Administrateur du Forum
STHS Owner / Propriètaire du STHS
English V2 & V3 Manual - Manuel V2 & V3 Français
opeth771
The Accomplished One / L'Accompli
Posts: 129
Joined: Sat Feb 25, 2012 9:49 am

Re: NHL API Powershell Scripts.

Post 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!
SimonT
STHS Owner / Propriétaire du STHS
Posts: 14755
Joined: Sat Oct 08, 2005 4:18 pm
Location: Montreal, Canada
Contact:

Re: NHL API Powershell Scripts.

Post 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.
-SimonT
Forum Administrator / Administrateur du Forum
STHS Owner / Propriètaire du STHS
English V2 & V3 Manual - Manuel V2 & V3 Français
Str_QNHL
The Addict / Le Drogué
Posts: 752
Joined: Fri Oct 28, 2005 2:42 pm
Location: Ste-Julie
Contact:

Re: NHL API Powershell Scripts.

Post 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 ??
_______________
Stephane Roy
STHS Beta Tester
STHS Support Team
http://qnhl.net
SimonT
STHS Owner / Propriétaire du STHS
Posts: 14755
Joined: Sat Oct 08, 2005 4:18 pm
Location: Montreal, Canada
Contact:

Re: NHL API Powershell Scripts.

Post 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.
-SimonT
Forum Administrator / Administrateur du Forum
STHS Owner / Propriètaire du STHS
English V2 & V3 Manual - Manuel V2 & V3 Français
SimonT
STHS Owner / Propriétaire du STHS
Posts: 14755
Joined: Sat Oct 08, 2005 4:18 pm
Location: Montreal, Canada
Contact:

Re: NHL API Powershell Scripts.

Post 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
    }
}
-SimonT
Forum Administrator / Administrateur du Forum
STHS Owner / Propriètaire du STHS
English V2 & V3 Manual - Manuel V2 & V3 Français
laving
The Crazy / Le Fou
Posts: 396
Joined: Tue Feb 28, 2006 10:37 am
Location: Oslo, Norway
Contact:

Re: NHL API Powershell Scripts.

Post 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. :)
SimonT
STHS Owner / Propriétaire du STHS
Posts: 14755
Joined: Sat Oct 08, 2005 4:18 pm
Location: Montreal, Canada
Contact:

Re: NHL API Powershell Scripts.

Post 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
-SimonT
Forum Administrator / Administrateur du Forum
STHS Owner / Propriètaire du STHS
English V2 & V3 Manual - Manuel V2 & V3 Français
SimonT
STHS Owner / Propriétaire du STHS
Posts: 14755
Joined: Sat Oct 08, 2005 4:18 pm
Location: Montreal, Canada
Contact:

Re: NHL API Powershell Scripts.

Post 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.
-SimonT
Forum Administrator / Administrateur du Forum
STHS Owner / Propriètaire du STHS
English V2 & V3 Manual - Manuel V2 & V3 Français
laving
The Crazy / Le Fou
Posts: 396
Joined: Tue Feb 28, 2006 10:37 am
Location: Oslo, Norway
Contact:

Re: NHL API Powershell Scripts.

Post 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
doogiski
New in Town / Le Ptit Nouveau
Posts: 13
Joined: Thu Sep 09, 2021 12:12 pm

Re: NHL API Powershell Scripts.

Post 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.
Last edited by doogiski on Mon Nov 20, 2023 3:27 pm, edited 1 time in total.
doogiski
New in Town / Le Ptit Nouveau
Posts: 13
Joined: Thu Sep 09, 2021 12:12 pm

Re: NHL API Powershell Scripts.

Post 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. :)
Post Reply