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
Post Reply
SimonT
STHS Owner / Propriétaire du STHS
Posts: 14126
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: 14126
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: 14126
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
STHS Support Team
STHS Support Team
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: 14126
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: 14126
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
Post Reply