swar / nba_api

An API Client package to access the APIs for NBA.com
MIT License
2.43k stars 524 forks source link

Getting Games by Date #207

Open jdcarbon opened 3 years ago

jdcarbon commented 3 years ago

Is it possible to get all the games played on a specific date? I tried the following code but have not been able to get it to work.

from nba_api.stats.endpoints import leaguegamefinder

gamefinder = leaguegamefinder.LeagueGameFinder(
                            date_to_nullable ='2021-02-09',
                            date_from_nullable='2021-02-09'
                            )  

gamesdf = gamefinder.get_data_frames()[0]

I get the following error (which I assume means that the response is empty):

JSONDecodeError                           Traceback (most recent call last)
<ipython-input-59-659a767dbf01> in <module>
      1 from nba_api.stats.endpoints import leaguegamefinder
      2 
----> 3 gamefinder = leaguegamefinder.LeagueGameFinder(
      4                             date_to_nullable ='2021-02-09',
      5                             date_from_nullable='2021-02-09'

~\Anaconda3\lib\site-packages\nba_api\stats\endpoints\leaguegamefinder.py in __init__(self, player_or_team_abbreviation, conference_nullable, date_from_nullable, date_to_nullable, division_simple_nullable, draft_number_nullable, draft_round_nullable, draft_team_id_nullable, draft_year_nullable, eq_ast_nullable, eq_blk_nullable, eq_dd_nullable, eq_dreb_nullable, eq_fg3a_nullable, eq_fg3m_nullable, eq_fg3_pct_nullable, eq_fga_nullable, eq_fgm_nullable, eq_fg_pct_nullable, eq_fta_nullable, eq_ftm_nullable, eq_ft_pct_nullable, eq_minutes_nullable, eq_oreb_nullable, eq_pf_nullable, eq_pts_nullable, eq_reb_nullable, eq_stl_nullable, eq_td_nullable, eq_tov_nullable, game_id_nullable, gt_ast_nullable, gt_blk_nullable, gt_dd_nullable, gt_dreb_nullable, gt_fg3a_nullable, gt_fg3m_nullable, gt_fg3_pct_nullable, gt_fga_nullable, gt_fgm_nullable, gt_fg_pct_nullable, gt_fta_nullable, gt_ftm_nullable, gt_ft_pct_nullable, gt_minutes_nullable, gt_oreb_nullable, gt_pf_nullable, gt_pts_nullable, gt_reb_nullable, gt_stl_nullable, gt_td_nullable, gt_tov_nullable, league_id_nullable, location_nullable, lt_ast_nullable, lt_blk_nullable, lt_dd_nullable, lt_dreb_nullable, lt_fg3a_nullable, lt_fg3m_nullable, lt_fg3_pct_nullable, lt_fga_nullable, lt_fgm_nullable, lt_fg_pct_nullable, lt_fta_nullable, lt_ftm_nullable, lt_ft_pct_nullable, lt_minutes_nullable, lt_oreb_nullable, lt_pf_nullable, lt_pts_nullable, lt_reb_nullable, lt_stl_nullable, lt_td_nullable, lt_tov_nullable, outcome_nullable, po_round_nullable, player_id_nullable, rookie_year_nullable, season_nullable, season_segment_nullable, season_type_nullable, starter_bench_nullable, team_id_nullable, vs_conference_nullable, vs_division_nullable, vs_team_id_nullable, years_experience_nullable, proxy, headers, timeout, get_request)
    202         }
    203         if get_request:
--> 204             self.get_request()
    205 
    206     def get_request(self):

~\Anaconda3\lib\site-packages\nba_api\stats\endpoints\leaguegamefinder.py in get_request(self)
    212             timeout=self.timeout,
    213         )
--> 214         self.load_response()
    215 
    216     def load_response(self):

~\Anaconda3\lib\site-packages\nba_api\stats\endpoints\leaguegamefinder.py in load_response(self)
    215 
    216     def load_response(self):
--> 217         data_sets = self.nba_response.get_data_sets()
    218         self.data_sets = [Endpoint.DataSet(data=data_set) for data_set_name, data_set in data_sets.items()]
    219         self.league_game_finder_results = Endpoint.DataSet(data=data_sets['LeagueGameFinderResults'])

~\Anaconda3\lib\site-packages\nba_api\stats\library\http.py in get_data_sets(self)
     81 
     82     def get_data_sets(self):
---> 83         raw_dict = self.get_dict()
     84         if 'resultSets' in raw_dict:
     85             results = raw_dict['resultSets']

~\Anaconda3\lib\site-packages\nba_api\library\http.py in get_dict(self)
     39 
     40     def get_dict(self):
---> 41         return json.loads(self._response)
     42 
     43     def get_json(self):

~\Anaconda3\lib\json\__init__.py in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    355             parse_int is None and parse_float is None and
    356             parse_constant is None and object_pairs_hook is None and not kw):
--> 357         return _default_decoder.decode(s)
    358     if cls is None:
    359         cls = JSONDecoder

~\Anaconda3\lib\json\decoder.py in decode(self, s, _w)
    335 
    336         """
--> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338         end = _w(s, end).end()
    339         if end != len(s):

~\Anaconda3\lib\json\decoder.py in raw_decode(self, s, idx)
    353             obj, end = self.scan_once(s, idx)
    354         except StopIteration as err:
--> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
    356         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)
jql6 commented 3 years ago

I had the same issue with playergamelogs.PlayerGameLogs. For both the date_from_nullable and date_to_nullable parameters, you need to pass dates as strings in this format: mm/dd/yyyy. Here's some code that I used that was working for me:

from nba_api.stats.endpoints import playergamelogs

playergamelogs.PlayerGameLogs(
    season_nullable = '2020-21',
    date_from_nullable = "03/28/2021",                                                     
    date_to_nullable = "03/28/2021"
).player_game_logs.get_data_frame()
pklarich commented 2 years ago

I'm trying a similar task but when I use league_id_nullable="00" it is still including ALL leagues as opposed to just NBA. Any clue what the problem is here?

This is the request being used, where td is today's date.

lgf.LeagueGameFinder(
    league_id_nullable = '00',
    date_from_nullable='08/01/2020', 
    date_to_nullable=td, 
    season_type_nullable = 'Regular Season'
).get_data_frames()[0]
schwaboy commented 1 year ago

I managed to do this by going directly to the scoreboardv3 endpoint directly.

https://stats.nba.com/stats/scoreboardv3?GameDate={date}&LeagueID=00