├── README.md └── main.py /README.md: -------------------------------------------------------------------------------- 1 | # NBA Stats Project Python 2 | 3 | This script retrieves NBA stats and data from the NBA API and displays them in the terminal. 4 | 5 | ## Requirements 6 | 7 | - Python 3.x 8 | - `requests` module (`pip install requests`) 9 | 10 | ## Usage 11 | 12 | 1. Clone the repository 13 | 2. Navigate to the project directory 14 | 3. Run the script using `python main.py` 15 | 4. The console will display team stats sorted by points per game. 16 | 17 | ## Code Explanation 18 | - `get_links()` function retrieves links to data from the NBA API 19 | - `get_scoreboard()` function retrieves current scores and game information and prints them to the console 20 | - `get_stats()` function retrieves team stats and prints them to the console, sorted by points per game 21 | - The script uses the `requests` module to make HTTP requests to the NBA API and the `pprint` module to format the output. 22 | 23 | ## Notes 24 | 25 | The NBA API is subject to change, which may cause this script to fail or produce unexpected output. 26 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from requests import get 2 | from pprint import PrettyPrinter 3 | 4 | BASE_URL = "https://data.nba.net" 5 | ALL_JSON = "/prod/v1/today.json" 6 | 7 | printer = PrettyPrinter() 8 | 9 | 10 | def get_links(): 11 | data = get(BASE_URL + ALL_JSON).json() 12 | links = data['links'] 13 | return links 14 | 15 | 16 | def get_scoreboard(): 17 | scoreboard = get_links()['currentScoreboard'] 18 | games = get(BASE_URL + scoreboard).json()['games'] 19 | 20 | for game in games: 21 | home_team = game['hTeam'] 22 | away_team = game['vTeam'] 23 | clock = game['clock'] 24 | period = game['period'] 25 | 26 | print("------------------------------------------") 27 | print(f"{home_team['triCode']} vs {away_team['triCode']}") 28 | print(f"{home_team['score']} - {away_team['score']}") 29 | print(f"{clock} - {period['current']}") 30 | 31 | 32 | def get_stats(): 33 | stats = get_links()['leagueTeamStatsLeaders'] 34 | teams = get( 35 | BASE_URL + stats).json()['league']['standard']['regularSeason']['teams'] 36 | 37 | teams = list(filter(lambda x: x['name'] != "Team", teams)) 38 | teams.sort(key=lambda x: int(x['ppg']['rank'])) 39 | 40 | for i, team in enumerate(teams): 41 | name = team['name'] 42 | nickname = team['nickname'] 43 | ppg = team['ppg']['avg'] 44 | print(f"{i + 1}. {name} - {nickname} - {ppg}") 45 | 46 | 47 | get_stats() --------------------------------------------------------------------------------