Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit 3d8e5e9

Browse files
committed
First commit
0 parents  commit 3d8e5e9

15 files changed

+558
-0
lines changed

.gitignore

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# User values
2+
data_dumps/
3+
4+
# Byte-compiled / optimized / DLL files
5+
__pycache__/
6+
*.py[cod]
7+
*$py.class
8+
9+
# C extensions
10+
*.so
11+
12+
# Distribution / packaging
13+
.Python
14+
env/
15+
build/
16+
develop-eggs/
17+
dist/
18+
downloads/
19+
eggs/
20+
.eggs/
21+
lib/
22+
lib64/
23+
parts/
24+
sdist/
25+
var/
26+
*.egg-info/
27+
.installed.cfg
28+
*.egg
29+
30+
# PyInstaller
31+
# Usually these files are written by a python script from a template
32+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
33+
*.manifest
34+
*.spec
35+
36+
# Installer logs
37+
pip-log.txt
38+
pip-delete-this-directory.txt
39+
40+
# Unit test / coverage reports
41+
htmlcov/
42+
.tox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*,cover
49+
50+
# Translations
51+
*.mo
52+
*.pot
53+
54+
# Django stuff:
55+
*.log
56+
57+
# Sphinx documentation
58+
docs/_build/
59+
60+
# PyBuilder
61+
target/

DESCRIPTION.rst

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
nba_py
2+
=======================
3+
4+
A python facing API for stats.nba.com RESTful API, partially based off
5+
of https://github.com/bradleyfay/NBAStats.
6+
7+
All data returned is returned as a DataFrame from the pandas module

MANIFEST.in

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include DESCRIPTION.rst

README.rst

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#nba_py (Python API for stats.nba.com)
2+
3+
##Summary
4+
A python facing API for stats.nba.com (Still in heavy development)
5+
6+
###Version = 0.1a1
7+
8+
## Currently done
9+
### Main Package
10+
* stats.nba.com/scoreboard/
11+
12+
### Game
13+
* stats.nba.com/boxscore/
14+
* stats.nba.com/boxscorescoring/
15+
* stats.nba.com/boxscoreusage/
16+
* stats.nba.com/boxscoremisc/
17+
* stats.nba.com/boxscoreadvanced/
18+
* stats.nba.com/boxscorefourfactors/
19+
* stats.nba.com/playbyplay/
20+
21+
### Player
22+
* stats.nba.com/commonallplayers/
23+
* stats.nba.com/commonplayerinfo/
24+
* stats.nba.com/playerdashboardbygeneralsplits/
25+
26+
### Team
27+
* stats.nba.com/teaminfocommon/
28+
* ~stats.nba.com/teamdashboardbygeneralsplits/~

nba_py/__init__.py

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
from requests import get
2+
from pandas import DataFrame
3+
from datetime import datetime
4+
5+
# Constants
6+
TODAY = datetime.today()
7+
CURRENT_SEASON = '2015-16'
8+
BASE_URL = 'http://stats.nba.com/stats/{endpoint}/'
9+
NBA_ID = '00'
10+
11+
12+
def _api_scrape(json_inp, ndx):
13+
"""
14+
Internal method to streamline the getting of data from the json
15+
16+
Args:
17+
json_inp (json): json input from our caller
18+
ndx (int): index where the data is located in the api
19+
20+
Returns:
21+
DataFrame (pandas.DataFrame): data set from ndx within the API's json
22+
"""
23+
return DataFrame(json_inp['resultSets'][ndx]['rowSet'],
24+
columns=json_inp['resultSets'][ndx]['headers'])
25+
26+
27+
def _get_json(endpoint, params):
28+
"""
29+
Internal method to streamline our requests / json getting
30+
31+
Args:
32+
endpoint (str): endpoint to be called from the API
33+
params (dict): parameters to be passed to the API
34+
35+
Raises:
36+
HTTPError: if requests hits a status code != 200
37+
38+
Returns:
39+
json (json): json object for selected API call
40+
"""
41+
_get = get(BASE_URL.format(endpoint=endpoint), params=params)
42+
print _get.url
43+
_get.raise_for_status()
44+
return _get.json()
45+
46+
47+
class Scoreboard:
48+
"""
49+
Displays current games plus info for a given day
50+
"""
51+
52+
def __init__(self,
53+
month=TODAY.month,
54+
day=TODAY.day,
55+
year=TODAY.year,
56+
league_id=NBA_ID,
57+
offset=0):
58+
self._game_date = '{month:02d}/{day:02d}/{year}'.format(month=month,
59+
day=day,
60+
year=year)
61+
self.json = _get_json(endpoint='scoreboard',
62+
params={'LeagueID': league_id,
63+
'GameDate': self._game_date,
64+
'DayOffset': offset})
65+
66+
def game_header(self):
67+
return _api_scrape(self.json, 0)
68+
69+
def line_score(self):
70+
return _api_scrape(self.json, 1)
71+
72+
def series_standings(self):
73+
return _api_scrape(self.json, 2)
74+
75+
def last_meeting(self):
76+
return _api_scrape(self.json, 3)
77+
78+
def east_conf_standings_by_day(self):
79+
return _api_scrape(self.json, 4)
80+
81+
def west_conf_standings_by_day(self):
82+
return _api_scrape(self.json, 5)
83+
84+
def available(self):
85+
return _api_scrape(self.json, 6)

nba_py/game.py

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
from nba_py import _api_scrape, _get_json
2+
3+
4+
class Boxscore:
5+
6+
def __init__(self,
7+
game_id,
8+
range_type=0,
9+
start_period=0,
10+
end_period=0,
11+
start_range=0,
12+
end_range=0):
13+
self.json = _get_json(endpoint='boxscore',
14+
params={'GameID': game_id,
15+
'RangeType': range_type,
16+
'StartPeriod': start_period,
17+
'EndPeriod': end_period,
18+
'StartRange': start_range,
19+
'EndRange': end_range})
20+
21+
def game_summary(self):
22+
return _api_scrape(self.json, 0)
23+
24+
def line_score(self):
25+
return _api_scrape(self.json, 1)
26+
27+
def season_series(self):
28+
return _api_scrape(self.json, 2)
29+
30+
def last_meeting(self):
31+
return _api_scrape(self.json, 3)
32+
33+
def player_stats(self):
34+
return _api_scrape(self.json, 4)
35+
36+
def team_stats(self):
37+
return _api_scrape(self.json, 5)
38+
39+
def other_stats(self):
40+
return _api_scrape(self.json, 6)
41+
42+
def officials(self):
43+
return _api_scrape(self.json, 7)
44+
45+
def game_info(self):
46+
return _api_scrape(self.json, 8)
47+
48+
def inactive_players(self):
49+
return _api_scrape(self.json, 9)
50+
51+
def available_video(self):
52+
return _api_scrape(self.json, 10)
53+
54+
def player_track(self):
55+
return _api_scrape(self.json, 11)
56+
57+
def player_track_team(self):
58+
return _api_scrape(self.json, 12)
59+
60+
61+
class BoxscoreScoring(Boxscore):
62+
63+
def sql_players_scoring(self):
64+
return _api_scrape(self.json, 13)
65+
66+
def sql_team_scoring(self):
67+
return _api_scrape(self.json, 14)
68+
69+
70+
class BoxscoreUsage(Boxscore):
71+
72+
def sql_players_usage(self):
73+
return _api_scrape(self.json, 13)
74+
75+
def sql_team_usage(self):
76+
return _api_scrape(self.json, 14)
77+
78+
79+
class BoxscoreMisc(Boxscore):
80+
81+
def sql_players_misc(self):
82+
return _api_scrape(self.json, 13)
83+
84+
def sql_team_misc(self):
85+
return _api_scrape(self.json, 14)
86+
87+
88+
class BoxscoreAdvanced(Boxscore):
89+
90+
def sql_players_advanced(self):
91+
return _api_scrape(self.json, 13)
92+
93+
def sql_team_advanced(self):
94+
return _api_scrape(self.json, 14)
95+
96+
97+
class BoxscoreFourFactors(Boxscore):
98+
99+
def sql_players_four_factors(self):
100+
return _api_scrape(self.json, 13)
101+
102+
def sql_team_four_factors(self):
103+
return _api_scrape(self.json, 14)
104+
105+
106+
class PlayByPlay:
107+
108+
def __init__(self,
109+
game_id,
110+
start_period=0,
111+
end_period=0):
112+
self.json = _get_json(endpoint='playbyplay',
113+
params={'GameID': game_id,
114+
'StartPeriod': start_period,
115+
'EndPeriod': end_period})
116+
117+
def info(self):
118+
return _api_scrape(self.json, 0)
119+
120+
def available_video(self):
121+
return _api_scrape(self.json, 1)

0 commit comments

Comments
 (0)