nba_py
nba_py copied to clipboard
Shot chart API not working
Think it's a problem with NBA stats API?
shot_chart_url = 'http://stats.nba.com/stats/shotchartdetail?Period=0&VsConference=&LeagueID=00&LastNGames=0&TeamID=0&Position=&Location=&Outcome=&ContextMeasure=FGA&DateFrom=&StartPeriod=&DateTo=&OpponentTeamID=0&ContextFilter=&RangeType=&Season=2015-16&AheadBehind=&PlayerID=201939&EndRange=&VsDivision=&PointDiff=&RookieYear=&GameSegment=&Month=0&ClutchTime=&StartRange=&EndPeriod=&SeasonType=Regular+Season&SeasonSegment=&GameID='
response = requests.get(shot_chart_url)
headers = response.json()['resultSets'][0]['headers']
ValueError Traceback (most recent call last)
/Users/crawles/anaconda/lib/python2.7/site-packages/requests/models.pyc in json(self, **kwargs) 824 # used. 825 pass --> 826 return complexjson.loads(self.text, **kwargs) 827 828 @property
/Users/crawles/anaconda/lib/python2.7/json/init.pyc in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw) 337 parse_int is None and parse_float is None and 338 parse_constant is None and object_pairs_hook is None and not kw): --> 339 return _default_decoder.decode(s) 340 if cls is None: 341 cls = JSONDecoder
/Users/crawles/anaconda/lib/python2.7/json/decoder.pyc in decode(self, s, _w) 362 363 """ --> 364 obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 365 end = _w(s, end).end() 366 if end != len(s):
/Users/crawles/anaconda/lib/python2.7/json/decoder.pyc in raw_decode(self, s, idx) 380 obj, end = self.scan_once(s, idx) 381 except StopIteration: --> 382 raise ValueError("No JSON object could be decoded") 383 return obj, end
ValueError: No JSON object could be decoded
PlayerPosition
is a required parameter for this endpoint. If you change your url to http://stats.nba.com/stats/shotchartdetail?Period=0&VsConference=&LeagueID=00&LastNGames=0&TeamID=0&Position=&Location=&Outcome=&ContextMeasure=FGA&DateFrom=&StartPeriod=&DateTo=&OpponentTeamID=0&ContextFilter=&RangeType=&Season=2015-16&AheadBehind=&PlayerID=201939&EndRange=&VsDivision=&PointDiff=&RookieYear=&GameSegment=&Month=0&ClutchTime=&StartRange=&EndPeriod=&SeasonType=Regular+Season&SeasonSegment=&GameID=&PlayerPosition=
(note this final query parameter) it should work.
Before trying to decode the JSON of the request, I'd recommend checking the status code and printing the response body if not a 400 -- unfortunately the NBA's API sends back non-JSON error messages when something goes wrong. They are however pretty informative.
Ah awesome. Thank you @nickb1080!!
Updated the wiki about for the stats.nba.com api. Think this is a relatively recent change as I've come across this problem following multiple tutorials.
@bttmly I tried your request link in the browser adding PlayerPosition=Guard, it returned "<Error><Message>An error has occurred.</Message></Error>" I also tried the wiki page way, matched all the required fields, still return the same error. Does this mean this endpoint no longer supported?
@zgvidwck Hmm. Yeah it's returning a 500 error; seems like an issue on their end. If you try it with an invalid parameter (like PlayerPosition as "G") it returns a 400 that says "Forward" "Guard" and "Center" are valid values. So according to their own error messages it should work.
The shot chart API is a pain. If you can, the most effective thing to do is just download all the data in bulk (set PlayerID to 0
) and slice and dice it as you please, or load it into your own database.
@bttmly I found a solution on reddit. Adding "&CFID=" to the parameters, it worked.
@zgvidwck huh! can you post the link to the reddit thread? I have a package that intends to document all required parameters for each endpoint so I should probably add that one
@bttmly https://www.reddit.com/r/nba/comments/5iu7jb/did_statsnbacoms_shotchartdetail_json_no_longer/
thanks
I tried this solution and I am still getting an 500 error if anyone could clarify this that would be great thanks