본문 바로가기
프로그램 (PHP,Python)

Using the ESPN API with PowerShell

by 날으는물고기 2013. 11. 8.

Using the ESPN API with PowerShell

Being a sports fan, I wanted to see what the ESPN API (just started in March 2012) had to offer (not much unfortunately unless you are a premium member) and make use of what PowerShell has to offer to pull back some data from those APIs. Using what useful APIs are available, I will show you how you can simply pull some data from ESPN and return it into a UI that you can then select from and open via your default web browser.

Get Your API!

The ESPN API is only available to those who have an API key available. First go out tohttp://developer.espn.com/ and register for the API.

After a minute or so you should have access to your API key.

Now we can start playing around with what we can actually query.

I mentioned before the lack of things you can query for because this is just for public use. Unless you have bigger aspirations, this is as good as it is going to get. You will be able to locate specific IDs for teams, athletes, etc… but will not be able to generate rosters, stats or anything else of the sort.

You can find the possible things to query here: http://developer.espn.com/docs and dig deeper into each area to actually see what methods are usable as well as the URI Parameters.

You can even build out your own web request string here: http://developer.espn.com/io-docs which will allow you to add your own parameters and then just copy that string and use it in your own request!

Moving on from that, I am going to take a look at the headlines from the day in the Green Bay Packers. But before I can do that, I need to figure out what their team ID is. So with that I will perform a query to get all of the NFL teams IDs.

#First figure out the team IDs
$team = Invoke-RestMethod `
'http://api.espn.com/v1/sports/football/nfl/teams/?apikey=<insert your own API>'

#Create a hash table to associate ID with Team
$teams = @{}
$team.sports.leagues.teams | ForEach {
    $teams[$_.Name] = $_.id
}

#Look for the Green Bay Packers
$teams['Packers']

Knowing that the team ID for the Packers is 9, I can now look at their latest headlines.

$packers = Invoke-RestMethod `
'http://api.espn.com/v1/now/?leagues=nfl&teams=9&apikey='<insert api>'
$packers.feed | ForEach {
    [pscustomobject]@{
        Headline = $_.headline
        Description = $_.description
        Modified = [datetime]$_.lastModified
        Type = $_.type
        Source = $_.source
        Link = $_.links.web.href
    }
} | Out-GridView -PassThru | ForEach {Start-Process $_.link}

I make use of Out-Gridview with the –PassThru parameter which will allow me to select one item from the UI and then output the object into the pipeline which then will open of the link in my default browser.

I have to use a ForEach because Start-Process doesn’t know how to handle the pipeline input of the Link property of the PSObject custom object.

The last thing is that I want to check out the current 25 news events going on in the NFL using the same approach as before.

#Get the latest news across the NFL
$news = Invoke-RestMethod `
'http://api.espn.com/v1/sports/football/nfl/news/headlines/top/?insider=no&limit=25&apikey=<insert api>'
$news.headlines | ForEach {
    [pscustomobject]@{
        Headline = $_.headline
        Description = $_.description
        Modified = [datetime]$_.lastModified
        Type = $_.type
        Source = $_.source
        Link = $_.links.web.href
    }
} | Out-GridView -PassThru | ForEach {Start-Process $_.link}

Same output as before with the UI made from Out-GridView and then opening up the same site (if I chose something to view)

So that is really that for working with the ESPN API unless you have a better account. For what its worth, you at least get some access to the headlines so you could have your own feed going. But all of the stuff that I really would like such as teams and accounts is just not available.


출처 : learn-powershell.net

728x90

댓글