swar / nba_api Goto Github PK
View Code? Open in Web Editor NEWAn API Client package to access the APIs for NBA.com
License: MIT License
An API Client package to access the APIs for NBA.com
License: MIT License
Figured we'd move this request into it's own issue, continuation of #32 and I'm sure elsewhere.
Please share ideas of what you think would be an appropriate way to share and display this information.
Some initial ideas I had.
Hi swar, I really appreciate you setting this up! I've been using nba_py for a while, but as it is no longer being supported I'm having issues with it.
I'm fairly new to python/scraping - I was wondering how to actually access the data - if for example, I wanted to pull all game stats from a certain player, for a certain game. I had trouble finding an explanation in the documentation.
I can't run the 2 examples scripts.
And a simple test code as this doesn't work for me :(
url = 'https://stats.nba.com/stats/boxscoreadvancedv2?EndPeriod=1&EndRange=0&GameID=0021800441&RangeType=0&StartPeriod=1&StartRange=0'
response = requests.get(url)
Very nice work by the way, I intend to use it to predict next game individual performances for each player !
More an idea than a need, but I think this repo is reaching a size that we should have some clearer instructions on how to raise issues and make pull requests, etc. I believe the standard for this is a CONTRIBUTING file. GitHub itself has some background here: https://help.github.com/en/articles/setting-guidelines-for-repository-contributors
Hey, so I'm planning to use this API (particularly the generic query here: [redacted]) and filter it out for major league game ID's for the past two years. Then I'll probably be hitting each game ID page once for data, pre-processing/filtering the data and storing it in a database so that when I host the (free, not-for-profit) web application I've been working on, users aren't hitting nba.com directly.
I was wondering if you had any recommendations for the rate of requests sent to the API since I'd like to not get blocked from nba.com, but also retrieve the data in a reasonable amount of time (this looks on the order of ~1000 or games for 2019, but haven't finished filtering out G League and 2K games)
Setting up Read the Docs and converting the existing markdown files to be compatible with Read the Docs. Not entirely sure of how much needs to go into this so I will need to spend some time looking into it.
Using the playergamelog endpoints, I can retrieve all games for a specific player. However, to reduce repetition and resource use, I want to only retrieve a days worth of games for a player at a time.
To do this, I thought I could pass in values for the DateFrom and DateTo parameters to the PlayerGameLog function. See the endpoint link below:
If I pass in Dec 23,2018, 12/23/2018, 2018-12-23 or 12/23/2018 for the DateFrom and DateTo values. I get either an error or all the games for the player. There is no documentation for this parameter so it's unclear how to retrieve a players game for a specific day.
I want to get player's current team and his JERSER_NUM.
I can only find JERSERY_NUM in infographicfanduelplayer, but I also need to provide a game_id to get it.
Is there a easier way?
This issue is to track supporting requests-cache into this package.
if i put a request like the above one in google chrome i get a response if i put into Postman or python i get nothing?
leaguedashptstats in players tracking not mapped
Last_n_game default should be 0 not 10
Is anyone else missing the static object after installing through pip?
I'm on macOS version 10.13.6.
Btw, thanks for making this!!! I've been dying to use nba_py again.
In PlayByPlay.ipynb (in the docs/examples directory) I believe this line:
description = play['HOMEDESCRIPTION'] if not None else play['VISITORDESCRIPTION']
will always return play['HOMEDESCRIPTION']
because not None
is always true. I suspect it should be
description = play['HOMEDESCRIPTION'] if play['HOMEDESCRIPTION'] is not None else play['VISITORDESCRIPTION']
It's possible I'm mistaken, but the current logic doesn't seem right.
They are all free for Open Source projects like this one.
https://github.com/marketplace/category/continuous-integration
Hello,
It seems when issuing a curl or calling it through a java native library i am getting SSL errors for the endpoint (https://stats.nba.com:443/stats/teamgamelog?Season=2017-18&SeasonType=Regular+Season&TeamID=1610612754) . For e.g. curl runs into SSL_read: SSL_ERROR_SYSCALL, errno 60. Have you enabled 2-way SSL for this API call ?
Is there a reason why the player id column in commonplayerinfo.common_player_info is labeled as 'PERSON_ID', but in common_headline_stats it is labeled 'PLAYER_ID'? Obviously this is a really small issue and has an easy workaround, but it makes merging dataframes unnecessarily difficult, unless I am missing a reson why this would have to be..
Hi, thanks for making this API, it's super helpful. I'm a bit of a python noob (I do all of my development in Matlab), so please excuse me if this is a dumb question.
I've been using the nba_api a bunch in Spyder and it works great, however when I went to run a script in Anaconda prompt it didn't seem like nba_api had been installed. I got an error on my first call to nba_api that it did not exist.
Any idea what's going on here and how to fix it?
Thanks,
Is it possible to add a field in the static player list to index players by active/non-active?
It seems that v1.05 is in debug mode or something. Intend or not?
Hi Swar,
For the BoxScoreAdvancedV2/ BoxScoreFourFactorsV2 etc. , I want to obtain only the first quarter data so I set start_period = 1 and end_period = 2. However, no matter what values I set for these variables, it always returns the information for the entire game. Is there a way to simply obtain halftime/ quarterly data?
Thanks!
When I hit some of the endpoints it works for the first few calls, but then i get the following error every time i try:
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Has anyone else been getting this?
Using the commonallplayers endpoint, and parameter is_only_current_season=1 for both '2016-17' and '2017-18' seasons, both return only 108 unique players for each respective season.
CommonAllPlayers(league_id=LeagueID.default,season='2016-17',is_only_current_season=1)
I think setting is_only_current_season=0 works (which results I believe are in the Data file, and return over 4000 players), but i want to get each season individually.
Is this a known inconsistency with many of the endpoints? Or do I have a parameter set wrong?
Hi everyone,
Starting with the small issue:
https://github.com/swar/nba_api/blob/master/docs/nba_api/stats/static/players.md
The end points for get_active_players()
and get_inactive_players()
don't seem to work yet.
About use:
I see there are endpoints for both ShotChartDetail
and ShotChartLineupDetail
. What is the difference between the two? They look just about identical.
And is there a way to grab shot locations team by team rather than by just a single player?
Player search suggestion:
How about another search to grab a list of active players by a particular season? So for instance, we want to grab a list of players in the 2015-16 season. Right now, it feels like we need an outside source to scrape together this information.
My solution has been the basketball-reference site, but that itself requires a bit of cleaning to match with this API.
Thanks in advance!
Tried to use the new SynergyPlayTypes endpoint today, but it isn't returning a data set. It's behaving the same as it is on doc page
Adding the actual variable name to the url name in the documentation of the different endpoints would facilitate the usage of the package.
Hi Swar,
Thanks so much for creating all of this, it's really great.
I want to combine the league Game log data (from leaguegamelog.LeagueGameLog() ) with the respective four factors data from boxscorefourfactorsv2.BoxScoreFourFactorsV2().
I can access individual game information from boxscorefourfactorsv2.BoxScoreFourFactorsV2() by adding the Game ID paramater. Thus my solution has been to loop through the games in the league game log dataframe , find the game id and team id for each entry, and find the four features statistics in the data frame returned by boxscorefourfactorsv2.BoxScoreFourFactorsV2(game_id).get_data_frame().
The problem is after looping through anywhere from 3 to 50 entries in the league game log, the program freezes and I get a connection error (specifically : ConnectionError: ('Connection aborted.', OSError("(60, 'ETIMEDOUT')",)) )
It would be great to get some help on this or an explanation of what's happenning, and if you know any workaround.
Thanks!
The section would contain links to repos/papers/blog posts of projects that use the nba_api. The hard part is finding those things.
I use :
stats = shotchartdetail.ShotChartDetail(team_id=1610612745,player_id=201935,
game_id_nullable = '0041800221',context_measure_simple = 'FGA')
data_sets = stats.data_sets
for data_set in data_sets:
df = data_set.get_data_frame()
print(df)
to get playoff data, but get null
Empty DataFrame
Columns: [GRID_TYPE, GAME_ID, GAME_EVENT_ID, PLAYER_ID, PLAYER_NAME, TEAM_ID, TEAM_NAME, PERIOD, MINUTES_REMAINING, SECONDS_REMAINING, EVENT_TYPE, ACTION_TYPE, SHOT_TYPE, SHOT_ZONE_BASIC, SHOT_ZONE_AREA, SHOT_ZONE_RANGE, SHOT_DISTANCE, LOC_X, LOC_Y, SHOT_ATTEMPTED_FLAG, SHOT_MADE_FLAG, GAME_DATE, HTM, VTM]
Index: []
Does NBA provide shotcharts detail for playoff? if not, how can i get the play off shot chart detail? Could you help?
I wanted to use this package in a Jupyter notebook to more easily visualize data in-line and increase comprehension of the code. An issue that I ran into was that the module was unable to be imported into Jupyter notebooks when it was working just fine on other local Python environments. I came to a solution that I wanted to share in case anyone was having similar issues.
The issue occurs because the ipython interpreter runs in the same directory that you started the server in. The issue is solved by providing the interpreter with the path to where to module is contained (probably the site-packages folder).
import sys
sys.path.append('C:\Python27\Lib\site-packages')
from nba_api.stats.endpoints import whatever
Doing this allows for full functionality of the package in a Jupyter notebook. I hope this helps someone else!
你太用心了,但是整套代码感觉没有nba_py来的简洁明了
test2 = scoreboardv2.ScoreboardV2(game_date='11/21/2018')
data_sets = test2.data_sets
for i in test2.data_sets:
print(i.get_json( ))
How can I identify the type of every data set in easiest way? For example 'LastMeeting', 'LineScore', 'SeriesStandings' .
Is there away to do this? I can't seem to find a way. Preferably a way to filter by number of games or time range
Per #31 (comment)
I want to add Default Values to the documentation page so users do not need to fumble in figuring out what values they need to pass.
No timeline when I can finish this task, currently with a hand injury in my dominant hand so will need to wait a bit for this to heal up before I can commit time to this issue.
I am trying to find the average pace from each team like from this chart https://stats.nba.com/teams/advanced/?sort=W&dir=-1
I saw that there is a pace stat from the boxscoreadvancedv2 endpoint, but there is no documentation on the GameId parameterr that it requires.
Thanks to u/JBarreraGT on reddit. He gave me a source of information that we could use to add static information for games.
http://data.nba.com/data/10s/v2015/json/mobile_teams/nba/2018/league/00_full_schedule.json
This link looks like it contains the full schedule with GameIDs along with additional information.
Hi swar, thanks for making this it's great.
I keep having this problem where my scripts will occasionally get stuck at the API call, even though the same command completes when I run it manually in the python command window. Any idea why this is happening?
Secondarily, I tried to write some code to retry the command in this case when it gets stuck. Though the code works great for all my tests, for some reasons when this problem with the API happens it doesn't work, any idea why this is?
The code to retry after a time limit is below. It's pretty gross franken-code, that I cobbled together from StackOverflow (python is not my strength)
class TimeoutException(Exception):
def __init__(self, msg=''):
self.msg = msg
@contextmanager
def time_limit(seconds, msg=''):
timer = threading.Timer(seconds, lambda: _thread.interrupt_main())
timer.start()
try:
yield
except KeyboardInterrupt:
raise TimeoutException("Timed out for operation {}".format(msg))
finally:
# if the action ends in specified time, timer is canceled
timer.cancel()
## Example call to API which doesn't properly trigger my timer wrapping ##
successful=None
while successful is None:
try:
with time_limit(4,'sleep'):
cur_teamR=commonteamroster.CommonTeamRoster(team_id=nba_teams[jj]['id'], season=cur_seas).get_data_frames()
successful=3.1415
except:
print('Timeout pass')
pass
Hi! iI wanted to say thank you for making this API first of all. I've seen and gotten some great data from it so far.
I have a few issues with everything, and I don't know if I'm just missing bits in the documentation.
For example, here's one snippet of code I have for ShotChartDetail:
initial_data = ShotChartDetail(player_id = ghay_id, # id of player season_nullable = current_year, # which year team_id=0, # team id used if multiple players found context_measure_simple='FGA')
Now I know I completed the required parameters, but it looks inconsistent with what's written in the documentation for that endpoint.
In the documentation, the required parameters are then listed as such:
"required_parameters": [ "ContextMeasure", "DateFrom", "DateTo", "GameID", "GameSegment", "LastNGames", "Location", "Month", "OpponentTeamID", "Outcome", "Period", "PlayerID", "PlayerPosition", "RookieYear", "SeasonSegment", "SeasonType", "TeamID", "VsConference", "VsDivision" ],
So is there a more complete way for me to see what parameters are required for each endpoint?
Furthermore, I see people using the get_data_frames()
attribute on the objects afterwards to extract the data into a pandas dataframe which is great. I noticed there are plenty of other attributes when using PyCharm. For ShotChartDetails, it outputs two dataframes, so I am confused as to what each endpoint does output and what attributes are available for use.
Is there documentation or another guide to show the available attributes and its associated outputs?
Thanks!
Ben
I wanted to open an issue to quickly ask a question to the developers as I am coming from nba_py. I wish to use this package in a GUI that is actually turning into a class project ... however I built the GUI previously with nba_py adhering to an MVC architecture. The model I used would pull information on the fly and store whatever data the user wished. For example, the session would start clean (or could import previous player data). Upon the latter, if a user has 12 players loaded in a session looking at say 20 different categories, I would load all the information at startup time. (Through sending requests for each player.)
I know this may present an issue as I would be asking for lots of requests from stats.nba.com and I know they have been limiting requests/sec. Do the developers have preferences or suggestions on allowing/disallowing this behavior?
edit: typos, grammer, etc.
This is similar to #5 in the error (i.e. json.decoder.JSONDecodeError), but the fix is different. In my implementation of anaconda (Mac OS X, python 3.6.5), I needed to change nba_api/library/http.py line 38,
return json.loads(self._response)
to
return requests.get(self._url).json()
and then it works as expected. You could put this fix in valid_json(), probably, not sure what position is best.
Hello! Thanks for that API.
I have problem with getting shortcharts for current game, returning empty first dataframe. What i'm doing wrong?
from nba_api.stats.endpoints import shotchartlineupdetail x = shotchartlineupdetail.ShotChartLineupDetail(game_id_nullable ='0021700798') x.get_data_frames()[0]
When attempting to pull anything from the api, if I'm using my VPN, specifically Private Internet Access, I get this error. It hangs until I interrupt it.
from nba_api.stats.endpoints import leaguegamelog
season201819 = leaguegamelog.LeagueGameLog()
TypeError Traceback (most recent call last)
~/anaconda3/lib/python3.6/site-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
379 try: # Python 2.7, use buffering of HTTP responses
--> 380 httplib_response = conn.getresponse(buffering=True)
381 except TypeError: # Python 2.6 and older, Python 3
TypeError: getresponse() got an unexpected keyword argument 'buffering'
During handling of the above exception, another exception occurred:
KeyboardInterrupt Traceback (most recent call last)
etc.
The solution was simply to disconnect my VPN, but obviously I'd still like to use it when I'm doing work on public wifi.
from nba_api.stats.static import teams
from nba_api.stats.endpoints import leaguegamefinder
from nba_api.stats.library.parameters import *
nba_teams = teams.get_teams()
pacers = [team for team in nba_teams if team['abbreviation'] == 'IND'][0]
pacers_id = pacers['id']
gamefinder = leaguegamefinder.LeagueGameFinder(team_id_nullable=pacers_id,
season_nullable=Season.default,
season_type_nullable=SeasonType.regular)
games = gamefinder.get_data_frames()[0]
games.count() # Includes 5 Games beyond regular season
games.head(100) #Includes 5 SummerLeague Games
When I use playervsplayer,
variable = playervsplayer.PlayerVsplayer(arguments are same as under code).
But error said "function' object has no attribute 'PlayerVsPlayer' ".
So, I wrote variable = playervsplayer(player_id,opponent_team_id,vs_player_id,season).
=>TypeError: playervsplayer() got an unexpected keyword argument 'vs_player_id'.
Please tell me how to use this function.
thanks.
game_finder = leaguegamefinder.LeagueGameFinder()
games_df = game_finder.get_data_frames()[0]
games_df = games_df[games_df.SEASON_ID.str[-4:] != str(2018)]
len(games_df)
0
Am I doing something wrong/ is there any way to get the game data for previous years?
Edit:
games_df = game_finder.get_data_frames()[0]
was obviously the issue
I can only get MADE SHOOT using the endpoint ? Is that any way that I can get the MISSED SHOT too ? Thanks.
I've tried:
from nba_api.stats.endpoints import videoevents
game_id = 0021801220
event_id = 7
video_event = videoevents.VideoEvents(game_id, event_id)
video_specs = video_event.nba_response.get_dict()
print(video_specs)
which returns:
{'resource': 'videoevents', 'parameters': {'GameID': '0021801220', 'GameEventID': 7}, 'resultSets': {'Meta': {'videoUrls': [{'uuid': '81066fa9-93b2-76d4-5e78-7bbe89335bc4', 'dur': '0.040000', 'stt': 'thumbnail,thumb_nba14,desktop', 'stp': 'http://nba.cdn.turner.com/nba/big/nba/wsc/2019/04/11/81066fa9-93b2-76d4-5e78-7bbe89335bc4.nba_3_320x180.jpg', 'sth': 180, 'stw': 320, 'mtt': 'thumb_nba1,thumbnail,desktop', 'mtp': 'http://nba.cdn.turner.com/nba/big/nba/wsc/2019/04/11/81066fa9-93b2-76d4-5e78-7bbe89335bc4.nba_3_576x324.jpg', 'mth': 324, 'mtw': 576, 'ltt': 'thumbnail,thumb_nba12,desktop', 'ltp': 'http://nba.cdn.turner.com/nba/big/nba/wsc/2019/04/11/81066fa9-93b2-76d4-5e78-7bbe89335bc4.nba_3_1280x720.jpg', 'lth': 720, 'ltw': 1280}]}, 'playlist': [{'gi': '0021801220', 'ei': 7, 'y': 2019, 'm': '04', 'd': '10', 'gc': '2019-04-10/INDATL', 'p': 1, 'dsc': "Collison 14' Pullup Jump Shot (2 PTS) (O'Quinn 1 AST)", 'ha': 'ATL', 'va': 'IND', 'hpb': 0, 'hpa': 0, 'vpb': 0, 'vpa': 2, 'pta': 1}]}}
None of the url's in these results are actually videos, only .jpg thumbnails. Is it possible to retrieve the video url or construct it from other sources in nba_api
? For example, this:
shotchartdetail can get player's shot chart detail that include position(LOC_X and LOC_Y). But I want to get the team's shot detail by game id.
I find shotchartlineupdetail in your API, but don't know how to use. For example:
stats = shotchartlineupdetail.ShotChartLineupDetail(game_id_nullable='0021800422',team_id_nullable=1610612751)
Can't get ShotChartLineupDetail for team, could you help?
Thanks to an email from Aymen (no idea your GitHub username), he has identified a new endpoint: LeagueSeasonMatchups.
I plan on adding this Endpoint this weekend so stay tuned. Also more changes should be coming this weekend, I might hold off on the documentation website but make more progress on it this weekend.
Source: https://stats.nba.com/player/2544/matchups/?sort=POSS&dir=1
Example: https://stats.nba.com/stats/LeagueSeasonMatchups?DateFrom=&DateTo=&LeagueID=00&OffPlayerID=2544&Outcome=&PORound=0&PerMode=PerGame&Season=2018-19&SeasonType=Regular+Season
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.