|
| 1 | +import logging |
| 2 | +import pynbaapi |
| 3 | + |
| 4 | +# Set up logging |
| 5 | +logger = logging.getLogger("pynbaapi") |
| 6 | +logger.setLevel(logging.DEBUG) |
| 7 | +rootLogger = logging.getLogger() |
| 8 | +rootLogger.setLevel(logging.DEBUG) |
| 9 | +ch = logging.StreamHandler() |
| 10 | +formatter = logging.Formatter( |
| 11 | + "%(asctime)s - %(levelname)8s - %(name)s(%(thread)s) - %(message)s" |
| 12 | +) |
| 13 | +ch.setFormatter(formatter) |
| 14 | +rootLogger.addHandler(ch) |
| 15 | + |
| 16 | +# Initiate the NBA API object with a user-agent |
| 17 | +nba = pynbaapi.nba.NBA( |
| 18 | + f"{pynbaapi.constants.APP_NAME} Examples/{pynbaapi.__version__.__version__}" |
| 19 | +) |
| 20 | + |
| 21 | +# Get a list of basic team about all teams |
| 22 | +# List will contain objects with attributes: |
| 23 | +# team_city, team_id, team_name, team_slug, team_tricode |
| 24 | +# As far as I can tell, team_tricode is the same as |
| 25 | +# team_abbreviation in other endpoints |
| 26 | +# NOTE: this method retrieves the full season's schedule and extracts team info |
| 27 | +# so it is a bit slow the first time. The data is cached for subsequent calls. |
| 28 | +all_teams_basic_info = nba.all_teams() |
| 29 | + |
| 30 | +# Get team_id for 76ers from the list of all team basic info |
| 31 | +# Result: 1610612755 (int) |
| 32 | +sixers_id = next(x.team_id for x in all_teams_basic_info if x.team_tricode == "PHI") |
| 33 | + |
| 34 | +# Alternately, find the team based on abbreviation/tricode, city, or name |
| 35 | +# and get the id from there |
| 36 | +# Result: 1610612755 (int) |
| 37 | +sixers_id = nba.find_team("PHI")[0].team_id |
| 38 | + |
| 39 | +# Get more details about the team |
| 40 | +# Response will be an object with the following data attributes: |
| 41 | +# available_seasons: list of seasons the team has played - these have an extra |
| 42 | +# character prefixed on them and I'm not sure what it means |
| 43 | +# team_info: wins, losses, division/conf name and rank, etc. |
| 44 | +# team_season_ranks: values per game and league ranks for ast, pts, reb, opp_pts |
| 45 | +sixers_details = nba.team(sixers_id) |
| 46 | + |
| 47 | +# Get history about the team |
| 48 | +# Response will be an object with the following data attributes: |
| 49 | +# awards_championships/conf/div: list of years the team won |
| 50 | +# background: basic info about the team including arena, dleague affiliation, |
| 51 | +# GM, owner, head coach, and year founded |
| 52 | +# history: list of city/names the team has had |
| 53 | +# hof_players, retired numbers, social_sites: self-explanatory lists |
| 54 | +sixers_history = nba.team_history(sixers_id) |
| 55 | + |
| 56 | +# Get the Sixers schedule for the 2021 season, |
| 57 | +# get the details of the game on 10/20/2021, |
| 58 | +# and extract the opponent name |
| 59 | +sixers_schedule = nba.schedule(season="2021", team_id=sixers_id) |
| 60 | +sixers_game_102021 = next( |
| 61 | + x.games[0] |
| 62 | + for x in sixers_schedule.league_schedule.game_dates |
| 63 | + if x.game_date.startswith("10/20/2021") |
| 64 | +) |
| 65 | +sixers_opponent_102021 = ( |
| 66 | + sixers_game_102021.away_team.team_name |
| 67 | + if sixers_game_102021.away_team.team_id != sixers_id |
| 68 | + else sixers_game_102021.home_team.team_name |
| 69 | +) |
| 70 | + |
| 71 | +# Get a scoreboard of games from 10/24/2021 |
| 72 | +# Response will be an object with a scoreboard attribute |
| 73 | +# containing a list of game objects (scoreboard.scoreboard.games) |
| 74 | +# Each game object contains attributes such as game_id, game_time_utc, |
| 75 | +# game_status, period, away_team & home_team (team objects), and team_leaders |
| 76 | +scoreboard = nba.scoreboard(game_date="2021-10-24") |
| 77 | + |
| 78 | +# Find the Sixers game in the scoreboard, |
| 79 | +# extract the status and the final score |
| 80 | +sixers_game = next( |
| 81 | + x |
| 82 | + for x in scoreboard.scoreboard.games |
| 83 | + if sixers_id in [x.away_team.team_id, x.home_team.team_id] |
| 84 | +) |
| 85 | +sixers_game_status = sixers_game.game_status_text |
| 86 | +sixers_game_final_score = f"{sixers_game.away_team.team_name} ({sixers_game.away_team.score}) @ ({sixers_game.home_team.score}) {sixers_game.home_team.team_name}" |
| 87 | + |
| 88 | +# Get the boxscore summary from the Sixers game on 10/24/21 |
| 89 | +# and get the attendance |
| 90 | +sixers_game_box = nba.boxscore(sixers_game.game_id) |
| 91 | +sixers_game_attendance = sixers_game_box.box_score_summary.attendance |
| 92 | + |
| 93 | +# Get the play-by-play data for the Sixers game on 10/24/21 |
| 94 | +# and get the total count of fouls for each team |
| 95 | +sixers_game_pbp = nba.play_by_play(sixers_game.game_id) |
| 96 | +sixers_game_sixers_fouls = sum( |
| 97 | + 1 |
| 98 | + for x in sixers_game_pbp.game.actions |
| 99 | + if x.action_type == "Foul" and x.team_tricode == "PHI" |
| 100 | +) |
| 101 | +sixers_game_thunder_fouls = sum( |
| 102 | + 1 |
| 103 | + for x in sixers_game_pbp.game.actions |
| 104 | + if x.action_type == "Foul" and x.team_tricode == "OKC" |
| 105 | +) |
0 commit comments