New NBA API Library / Website

Home for all your discussion of basketball statistical analysis.
Post Reply
Warthog
Posts: 22
Joined: Tue Jul 12, 2016 8:23 pm

New NBA API Library / Website

Post by Warthog »

Hi all. You might recall, before the 2017 draft I posted a little site I whipped up to compare draft pick trade value charts based on Kevin Pelton's article(s) on the topic. In the meantime I have been working on a couple things that many of you might be interested in.

The first is a new open-source client library in the mold of nba.js and nba_py. It's written in PHP, uses modern programming techniques/libraries, is fully unit-tested, and is very, very easy to use. You can find it on GitHub at the following link: https://github.com/jasonroman/nba-api . One of the main advantages my library currently has over the others is the amount of data it can pull. Most other libraries have around 80 endpoints of data, but I've discovered 242. There's a whole wealth of information (and not just stats) that you can grab from the NBA. I plan to update regularly, and being open-source, I hope that my library becomes the unofficial place to find every endpoint.

The second thing is a new website - http://nbasense.com . This does contain the original Draft Pick Trade Value Chart tool, but more importantly it contains *extensive* documentation for using my new open-source library. I give you all of the code right on the site that you can use to create your requests.

What I'm even more proud of is the documentation I have on all 242 requests. With every request you have:

- the URL endpoint along with any placeholders
- a link to the request class on GitHub
- code you can copy/paste to generate a response
- code you can copy/paste that contains every filterable variable and default/example values for each
- an example URL that you can open to see what the request returns
- an example URL that you can submit to my library and retrieve as a traversable array/object right in the browser
- a listing of every single parameter you can enter for the request, telling you the data type, allowed values/choices, and default value
- a dynamically generated form for filtering data

I want to touch on the last point since it's the one I'm most proud of. For any request, my website creates a form that contains every single parameter in that request, along with default/example values. You can change any of these fields and submit the form. Upon submitting, I create the dynamic URL endpoint based on your search criteria. I also create the specific PHP code for that request containing your search criteria that you can copy/paste. I also show you a traversable array/object of all the results. For example, check that out here: http://nbasense.com/nba-api/Data/Prod/T ... playground

So essentially, even if you don't want to use PHP, or my library, you can still use my website to generate any of the available 242 URLs and pass that into your own client, or just take the resulting raw JSON/XML data and pass it to R or Python, or cURL, or whatever you want. It's as flexible as you want/need it to be.

The only caveat is that right now the NBA is blocking my requests to 102 of the 104 stats.nba.com/stats endpoints from nbasense.com, which I know are some of the most useful endpoints. So if you submit my form for one of those requests you'll only see the URL/code, and not the result directly in the browser. I think they block a lot of web hosts like DigitalOcean, Heroku, and DreamHost. I have yet to find a solution for that, but I'm working on it.

Anyway, enjoy!
Rd11490
Posts: 121
Joined: Mon Sep 29, 2014 4:54 am

Re: New NBA API Library / Website

Post by Rd11490 »

I have not played with the website yet, but I want to thank you again for going through the various endpoints and figuring out exactly what each return and parameter is. Thanks to your great comments/information, I learned tonight how to determine who is on the court at any given time with out having to go through and parse the play by play data, saving me a ton of time.
Warthog
Posts: 22
Joined: Tue Jul 12, 2016 8:23 pm

Re: New NBA API Library / Website

Post by Warthog »

That's great! If you wanna share specifics or which endpoint(s) you found useful I'd love to hear it.

Interestingly enough, it looks like one of the endpoints to retrieve video information is no longer working. I had a method to find the video of any play-by-play event of any game, and was going to write a post on how to do that, but it's no longer valid. I'm hoping that's just temporary.
Rd11490
Posts: 121
Joined: Mon Sep 29, 2014 4:54 am

Re: New NBA API Library / Website

Post by Rd11490 »

https://github.com/jasonroman/nba-api/b ... eParam.php

The Start and End range params in combination with any of the box score endpoints allow you to determine who is on the court at any given time with a little bit of adjustment to the time in the result from the playbyplayv2 endpoint or the shotchartdetail endpoint.

For example, I can look at the play by play for the following game:
http://stats.nba.com/stats/playbyplayv2 ... artRange=0

and determine who was on the court for the 447th event. I can do this by grabbing the time remaining in the period "3:59" and the period "4" and calculate the total time elapsed in the game (2641 seconds). From there I can call boxscoreadvanced endpoint with the following parameters to get the players on the court during that event.

RangeType=2
StartRange=26409
EndRange=26410

http://stats.nba.com/stats/boxscoreadva ... ange=26400
Warthog
Posts: 22
Joined: Tue Jul 12, 2016 8:23 pm

Re: New NBA API Library / Website

Post by Warthog »

Thank you for the explanation - very cool way and efficient way of determining who is on the court at any given time! I might add a function to the library that converts the period/game time to an elapsed time, that sounds useful.
Crow
Posts: 10536
Joined: Thu Apr 14, 2011 11:10 pm

Re: New NBA API Library / Website

Post by Crow »

Also want to thank. I might not be a direct user but will look around later. Certainly I hope others will take advantage and publish findings.
Warthog
Posts: 22
Joined: Tue Jul 12, 2016 8:23 pm

Re: New NBA API Library / Website

Post by Warthog »

I found a new endpoint - retrieving the full schedule and including the week #. Hat tip to this Reddit thread: https://www.reddit.com/r/nba/comments/7 ... om_easily/

I added the endpoint and updated my NBA API to version 1.0.5. If you want to be alerted of new updates faster than this forum thread you can follow https://twitter.com/nbasense

Details for the new request can be found here: http://nbasense.com/nba-api/Data/Mobile ... heduleWeek
Rd11490
Posts: 121
Joined: Mon Sep 29, 2014 4:54 am

Re: New NBA API Library / Website

Post by Rd11490 »

Just a heads up, it looks like stats.nba.com has killed the shotchartdetail endpoint. All shot charts have disappeared for games prior to this season and based on inspection there is no endpoint returning the raw data for this season's shots.

EDIT* the full playbyplay response seems to have the data now (i.e. https://data.nba.com/data/10s/v2015/jso ... l_pbp.json)

I have a copy of all shot chart data 2000-01 to 2016-17 seasons if anyone needs it.
Warthog
Posts: 22
Joined: Tue Jul 12, 2016 8:23 pm

Re: New NBA API Library / Website

Post by Warthog »

I noticed that too - in my class I put "This appears to no longer be publicly available on the stats nba website." when I first added it.

However, the endpoint still works for me, and I *can* pull 2017-18 seasonal data. Are you not experiencing differently?
Rd11490
Posts: 121
Joined: Mon Sep 29, 2014 4:54 am

Re: New NBA API Library / Website

Post by Rd11490 »

It's working for me now. I'm not going to the time to go through postman history to find out what call I was making, but I must have generated it incorrectly. Thanks for responding, I wouldn't have realized it otherwise.

It does worry me that it not longer shows up in the endpoints called when the page generates a shot chart. I wonder if it has been deprecated and will be removed soon.
Warthog
Posts: 22
Joined: Tue Jul 12, 2016 8:23 pm

Re: New NBA API Library / Website

Post by Warthog »

Could just be a quirk. I know that certain video URLs were not working during preseason, but now they are back.
Post Reply