seemethere / nba_py

Python client for NBA statistics located at stats.nba.com
BSD 3-Clause "New" or "Revised" License
1.05k stars 254 forks source link

Shot chart API not working #74

Open crawles opened 7 years ago

crawles commented 7 years ago

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)

in () 6 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=' 7 response = requests.get(shot_chart_url) ----> 8 headers = response.json()['resultSets'][0]['headers'] 9 10 print shot_chart_url /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
bttmly commented 7 years ago

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.

crawles commented 7 years ago

Ah awesome. Thank you @nickb1080!!

crawles commented 7 years ago

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.

zgvidwck commented 7 years ago

@bttmly I tried your request link in the browser adding PlayerPosition=Guard, it returned "An error has occurred." 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?

bttmly commented 7 years ago

@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.

zgvidwck commented 7 years ago

@bttmly I found a solution on reddit. Adding "&CFID=" to the parameters, it worked.

bttmly commented 7 years ago

@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

zgvidwck commented 7 years ago

@bttmly https://www.reddit.com/r/nba/comments/5iu7jb/did_statsnbacoms_shotchartdetail_json_no_longer/

bttmly commented 7 years ago

thanks

nharrisanalyst commented 6 years ago

I tried this solution and I am still getting an 500 error if anyone could clarify this that would be great thanks