Skip to content
Permalink
Browse files

First commit

  • Loading branch information...
seemethere committed Aug 25, 2015
0 parents commit 3d8e5e911e8fe7e48e1c694675475477f8f5ce3d
Showing with 558 additions and 0 deletions.
  1. +61 −0 .gitignore
  2. +7 −0 DESCRIPTION.rst
  3. +1 −0 MANIFEST.in
  4. +28 −0 README.rst
  5. +85 −0 nba_py/__init__.py
  6. +121 −0 nba_py/game.py
  7. +100 −0 nba_py/player.py
  8. +21 −0 nba_py/team.py
  9. +5 −0 setup.cfg
  10. +104 −0 setup.py
  11. 0 tests/__init__.py
  12. +5 −0 tests/test_nba_py.py
  13. +6 −0 tests/test_nba_py_game.py
  14. +10 −0 tests/test_nba_py_player.py
  15. +4 −0 tests/test_nba_py_team.py
@@ -0,0 +1,61 @@
# User values
data_dumps/

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover

# Translations
*.mo
*.pot

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/
@@ -0,0 +1,7 @@
nba_py
=======================

A python facing API for stats.nba.com RESTful API, partially based off
of https://github.com/bradleyfay/NBAStats.

All data returned is returned as a DataFrame from the pandas module
@@ -0,0 +1 @@
include DESCRIPTION.rst
@@ -0,0 +1,28 @@
#nba_py (Python API for stats.nba.com)

##Summary
A python facing API for stats.nba.com (Still in heavy development)

###Version = 0.1a1

## Currently done
### Main Package
* stats.nba.com/scoreboard/

### Game
* stats.nba.com/boxscore/
* stats.nba.com/boxscorescoring/
* stats.nba.com/boxscoreusage/
* stats.nba.com/boxscoremisc/
* stats.nba.com/boxscoreadvanced/
* stats.nba.com/boxscorefourfactors/
* stats.nba.com/playbyplay/

### Player
* stats.nba.com/commonallplayers/
* stats.nba.com/commonplayerinfo/
* stats.nba.com/playerdashboardbygeneralsplits/

### Team
* stats.nba.com/teaminfocommon/
* ~stats.nba.com/teamdashboardbygeneralsplits/~
@@ -0,0 +1,85 @@
from requests import get
from pandas import DataFrame
from datetime import datetime

# Constants
TODAY = datetime.today()
CURRENT_SEASON = '2015-16'
BASE_URL = 'http://stats.nba.com/stats/{endpoint}/'
NBA_ID = '00'


def _api_scrape(json_inp, ndx):
"""
Internal method to streamline the getting of data from the json
Args:
json_inp (json): json input from our caller
ndx (int): index where the data is located in the api
Returns:
DataFrame (pandas.DataFrame): data set from ndx within the API's json
"""
return DataFrame(json_inp['resultSets'][ndx]['rowSet'],
columns=json_inp['resultSets'][ndx]['headers'])


def _get_json(endpoint, params):
"""
Internal method to streamline our requests / json getting
Args:
endpoint (str): endpoint to be called from the API
params (dict): parameters to be passed to the API
Raises:
HTTPError: if requests hits a status code != 200
Returns:
json (json): json object for selected API call
"""
_get = get(BASE_URL.format(endpoint=endpoint), params=params)
print _get.url
_get.raise_for_status()
return _get.json()


class Scoreboard:
"""
Displays current games plus info for a given day
"""

def __init__(self,
month=TODAY.month,
day=TODAY.day,
year=TODAY.year,
league_id=NBA_ID,
offset=0):
self._game_date = '{month:02d}/{day:02d}/{year}'.format(month=month,
day=day,
year=year)
self.json = _get_json(endpoint='scoreboard',
params={'LeagueID': league_id,
'GameDate': self._game_date,
'DayOffset': offset})

def game_header(self):
return _api_scrape(self.json, 0)

def line_score(self):
return _api_scrape(self.json, 1)

def series_standings(self):
return _api_scrape(self.json, 2)

def last_meeting(self):
return _api_scrape(self.json, 3)

def east_conf_standings_by_day(self):
return _api_scrape(self.json, 4)

def west_conf_standings_by_day(self):
return _api_scrape(self.json, 5)

def available(self):
return _api_scrape(self.json, 6)
@@ -0,0 +1,121 @@
from nba_py import _api_scrape, _get_json


class Boxscore:

def __init__(self,
game_id,
range_type=0,
start_period=0,
end_period=0,
start_range=0,
end_range=0):
self.json = _get_json(endpoint='boxscore',
params={'GameID': game_id,
'RangeType': range_type,
'StartPeriod': start_period,
'EndPeriod': end_period,
'StartRange': start_range,
'EndRange': end_range})

def game_summary(self):
return _api_scrape(self.json, 0)

def line_score(self):
return _api_scrape(self.json, 1)

def season_series(self):
return _api_scrape(self.json, 2)

def last_meeting(self):
return _api_scrape(self.json, 3)

def player_stats(self):
return _api_scrape(self.json, 4)

def team_stats(self):
return _api_scrape(self.json, 5)

def other_stats(self):
return _api_scrape(self.json, 6)

def officials(self):
return _api_scrape(self.json, 7)

def game_info(self):
return _api_scrape(self.json, 8)

def inactive_players(self):
return _api_scrape(self.json, 9)

def available_video(self):
return _api_scrape(self.json, 10)

def player_track(self):
return _api_scrape(self.json, 11)

def player_track_team(self):
return _api_scrape(self.json, 12)


class BoxscoreScoring(Boxscore):

def sql_players_scoring(self):
return _api_scrape(self.json, 13)

def sql_team_scoring(self):
return _api_scrape(self.json, 14)


class BoxscoreUsage(Boxscore):

def sql_players_usage(self):
return _api_scrape(self.json, 13)

def sql_team_usage(self):
return _api_scrape(self.json, 14)


class BoxscoreMisc(Boxscore):

def sql_players_misc(self):
return _api_scrape(self.json, 13)

def sql_team_misc(self):
return _api_scrape(self.json, 14)


class BoxscoreAdvanced(Boxscore):

def sql_players_advanced(self):
return _api_scrape(self.json, 13)

def sql_team_advanced(self):
return _api_scrape(self.json, 14)


class BoxscoreFourFactors(Boxscore):

def sql_players_four_factors(self):
return _api_scrape(self.json, 13)

def sql_team_four_factors(self):
return _api_scrape(self.json, 14)


class PlayByPlay:

def __init__(self,
game_id,
start_period=0,
end_period=0):
self.json = _get_json(endpoint='playbyplay',
params={'GameID': game_id,
'StartPeriod': start_period,
'EndPeriod': end_period})

def info(self):
return _api_scrape(self.json, 0)

def available_video(self):
return _api_scrape(self.json, 1)

0 comments on commit 3d8e5e9

Please sign in to comment.
You can’t perform that action at this time.