Skip to content

Commit 04aca79

Browse files
committed
Initial commit
1 parent e205016 commit 04aca79

25 files changed

+1571
-1
lines changed

.flake8

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[flake8]
2+
ignore = E203, E501, W503
3+
exclude = .git,__pycache__,docs,build,dist,logs,.vscode
4+
max-line-length = 88

.gitignore

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/logs
2+
/local
3+
*.ipynb
4+
/debug*.py
5+
/test*.py
6+
/examples*.py
7+
8+
# Byte-compiled / optimized / DLL files
9+
__pycache__/
10+
*.py[cod]
11+
*$py.class
12+
13+
# C extensions
14+
*.so
15+
16+
# Distribution / packaging
17+
.Python
18+
build/
19+
develop-eggs/
20+
dist/
21+
downloads/
22+
eggs/
23+
.eggs/
24+
lib/
25+
lib64/
26+
parts/
27+
sdist/
28+
var/
29+
wheels/
30+
*.egg-info/
31+
.installed.cfg
32+
*.egg
33+
MANIFEST
34+
35+
# PyInstaller
36+
# Usually these files are written by a python script from a template
37+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
38+
*.manifest
39+
*.spec
40+
41+
# Installer logs
42+
pip-log.txt
43+
pip-delete-this-directory.txt
44+
45+
# Unit test / coverage reports
46+
htmlcov/
47+
.tox/
48+
.coverage
49+
.coverage.*
50+
.cache
51+
nosetests.xml
52+
coverage.xml
53+
*.cover
54+
.hypothesis/
55+
.pytest_cache/
56+
57+
# Translations
58+
*.mo
59+
*.pot
60+
61+
# Django stuff:
62+
*.log
63+
local_settings.py
64+
db.sqlite3
65+
66+
# Flask stuff:
67+
instance/
68+
.webassets-cache
69+
70+
# Scrapy stuff:
71+
.scrapy
72+
73+
# Sphinx documentation
74+
docs/_build/
75+
76+
# PyBuilder
77+
target/
78+
79+
# Jupyter Notebook
80+
.ipynb
81+
.ipynb_checkpoints
82+
83+
# pyenv
84+
.python-version
85+
86+
# celery beat schedule file
87+
celerybeat-schedule
88+
89+
# SageMath parsed files
90+
*.sage.py
91+
92+
# Environments
93+
.env
94+
.venv
95+
env/
96+
venv/
97+
ENV/
98+
env.bak/
99+
venv.bak/
100+
101+
# Spyder project settings
102+
.spyderproject
103+
.spyproject
104+
105+
# Rope project settings
106+
.ropeproject
107+
108+
# mkdocs documentation
109+
/site
110+
111+
# mypy
112+
.mypy_cache/

.vscode/settings.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"python.linting.flake8Enabled": true,
3+
"python.linting.enabled": true,
4+
"python.testing.pytestArgs": [
5+
"."
6+
],
7+
"python.testing.unittestEnabled": false,
8+
"python.testing.pytestEnabled": true
9+
}

README.md

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,29 @@
11
# pynbaapi
2-
Python API Wrapper for NBA API
2+
3+
Python Wrapper for NBA API
4+
5+
Created by Todd Roberts
6+
7+
## Installation
8+
Install from pip:
9+
10+
```
11+
pip install pynbaapi
12+
```
13+
14+
or
15+
16+
```
17+
py -m pip install pynbaapi
18+
```
19+
20+
## Use
21+
See `examples.py` in the `examples` folder for some example uses.
22+
23+
There are other methods in the `pynba.nba.NBA()` object, and the `pynba.api.API()` object has a `from_url` method that can be used to retrieve arbitrary URLs (as long as the response is in nested json format, which many of the NBA API endpoints are not).
24+
25+
## Support
26+
I do not provide support related to NBA API. There is good documentation on the [swar/nba_api](https://github.com/swar/nba_api) repo, and this package was only created because the nba_api package does not (appear to) return API responses in recursive objects.
27+
28+
## Copyright Notice
29+
This package and its author are not affiliated with NBA or any NBA team. This API wrapper interfaces with NBA's API. Use of NBA data is subject to copyright by NBA.

examples/examples.py

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
)

pynbaapi/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .nba import NBA

pynbaapi/__version__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__version__ = "0.0.1"

pynbaapi/api/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)