swar / nba_api

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

[PROPSAL]: Game IDs for each game of a specific team #296

Open clemens-boisseree opened 1 year ago

clemens-boisseree commented 1 year ago

NBA API Version

V1.1.13

Issue

For some projects you need the game plan of a team (or all teams) with the respective Game-ID. For example, to retrieve the play-by-play data for each of these games, etc. There is no separate API interface from the NBA for each individual team, just one big JSON file that can be retrieved and filtered. I used the following simple code for this, perhaps an API endpoint can be created for the use case.

Code


import requests
import pandas as pd

#headers
headers = {
    'Host': 'cdn.nba.com',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0',
    'Accept': 'application/json, text/plain, */*',
    'Accept-Language': 'en-US,en;q=0.5',
    'Accept-Encoding': 'gzip, deflate, br',
    'x-nba-stats-origin': 'stats',
    'x-nba-stats-token': 'true',
    'Connection': 'keep-alive',
    'Origin': 'https://cdn.nba.com',
    'Referer': 'https://cdn.nba.com',
    'Pragma': 'no-cache',
    'Cache-Control': 'no-cache'
}

#get games from schedule
url = 'https://cdn.nba.com/static/json/staticData/scheduleLeagueV2.json'
r = requests.get(url, headers=headers)
schedule = r.json()
schedule = schedule['leagueSchedule']['gameDates']
games = []
for l in schedule:
    game = l['games']
    for l in game:
        list = [l['gameId'],l['gameStatus'],l['gameDateTimeUTC'],l['awayTeam']['teamId'],l['homeTeam']['teamId'],l['awayTeam']['teamName'],l['homeTeam']['teamName'],l['homeTeam']['teamTricode'],l['awayTeam']['teamTricode'] ]
        list = pd.DataFrame([list], columns =['gameId','gameStatus','gameDateTimeUTC','awayTeamID','homeTeamID','awayTeam','homeTeam','awayTeamTricode','homeTeamTricode'])
        games.append(list)
games = pd.concat([list for list in games])

#Since I live in Europe, I want to use the European Timeformat for the Game Date
games['gameDateTimeUTC'] = pd.to_datetime(games['gameDateTimeUTC'], format="%Y-%m-%dT%H:%M:%S.%f", errors = 'coerce').dt.tz_localize(None)

#Orlando Magic only, since there is no key for the Season Status, I filter pre season by date
magic_games = games.loc[((games['awayTeamID'] == 1610612753) | (games['homeTeamID'] == 1610612753)) & (games['gameDateTimeUTC'] >= '2022-10-17')]
gbalduzzi commented 1 year ago

Just a quick info based on this comment in your code:

since there is no key for the Season Status, I filter pre season by date

The third digit in the game id is actually a flag for season type. 1 is preason, 2 regular season, 3 all star weekend, 4 play off and 5 play-ins

0022200147
  ^ regular season game
0012200147
  ^ pre season game
...
clemens-boisseree commented 1 year ago

Nice, didn't know that. Thanks