Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions NBAapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
#import numpy as np
# import numpy as np

from . import plot
from . import plot_team
from . import player
from . import team
from . import team
from . import league
from . import shotchart
from . import shotchart
from . import draft
import pkg_resources

DATA_PATH = pkg_resources.resource_filename('NBAapi', 'data/')









DATA_PATH = pkg_resources.resource_filename('NBAapi', 'data/')
10 changes: 10 additions & 0 deletions NBAapi/credentials/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
DEFAULT_HEADERS = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:72.0) Gecko/20100101 Firefox/72.0',
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'fr,en-US;q=0.7,en;q=0.3',
'X-NewRelic-ID': 'VQECWF5UChAHUlNTBwgBVw==',
'x-nba-stats-origin': 'stats',
'x-nba-stats-token': 'true',
'Connection': 'keep-alive',
'Referer': 'https://stats.nba.com/events/?'
}
29 changes: 15 additions & 14 deletions NBAapi/draft.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import requests
import pandas as pd
from NBAapi.credentials import DEFAULT_HEADERS

def drafthistory(college='',leagueid='00',overallpick='',roundnum='',roundpick='',
season='',teamid=0,topx=''):
url = 'http://stats.nba.com/stats/drafthistory?'
def drafthistory(college='', leagueid='00', overallpick='', roundnum='', roundpick='',
season='', teamid=0, topx=''):
url = 'https://stats.nba.com/stats/drafthistory?'
api_param = {
'College' : college,
'LeagueID' : leagueid,
'OverallPick' : overallpick,
'RoundNum' : roundnum,
'RoundPick' : roundpick,
'Season' : season,
'TeamID' : teamid,
'TopX' : topx
'College': college,
'LeagueID': leagueid,
'OverallPick': overallpick,
'RoundNum': roundnum,
'RoundPick': roundpick,
'Season': season,
'TeamID': teamid,
'TopX': topx
}
u_a = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36"
response = requests.get(url,params=api_param,headers={"USER-AGENT":u_a})
response = requests.get(url, params=api_param,
headers=DEFAULT_HEADERS)
data = response.json()
return pd.DataFrame(data['resultSets'][0]['rowSet'],columns=data['resultSets'][0]['headers'])
return pd.DataFrame(data['resultSets'][0]['rowSet'], columns=data['resultSets'][0]['headers'])
7 changes: 4 additions & 3 deletions NBAapi/league.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import requests
import pandas as pd
from NBAapi.credentials import DEFAULT_HEADERS

def gamelog(counter = 1000,datefrom='',dateto='',direction='DESC',leagueid='00',
playerorteam='T',season='2015-16',seasontype='Regular Season',sorter='PTS'):
Expand All @@ -16,7 +17,7 @@ def gamelog(counter = 1000,datefrom='',dateto='',direction='DESC',leagueid='00',
'Sorter' : sorter,
}
u_a = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36"
response = requests.get(url,params=api_param,headers={"USER-AGENT":u_a})
response = requests.get(url,params=api_param,headers=DEFAULT_HEADERS)
data = response.json()
return pd.DataFrame(data['resultSets'][0]['rowSet'],columns=data['resultSets'][0]['headers'])

Expand Down Expand Up @@ -64,7 +65,7 @@ def hustlestatsteam(College='',Conference='',Country='',DateFrom='',DateTo='',Di
'Weight' : Weight
}
u_a = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36"
response = requests.get(url,params=api_param,headers={"USER-AGENT":u_a},timeout=1.0)
response = requests.get(url,params=api_param,headers=DEFAULT_HEADERS,timeout=1.0)
data = response.json()
return pd.DataFrame(data['resultSets'][0]['rowSet'],columns=data['resultSets'][0]['headers'])

Expand All @@ -76,6 +77,6 @@ def playbyplayv2(gameid,startperiod=0,endperiod=0):
'GameID' : gameid,
}
u_a = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36"
response = requests.get(url,params=api_param,headers={"USER-AGENT":u_a},timeout=1.0)
response = requests.get(url,params=api_param,headers=DEFAULT_HEADERS,timeout=1.0)
data = response.json()
return pd.DataFrame(data['resultSets'][0]['rowSet'],columns=data['resultSets'][0]['headers'])
15 changes: 8 additions & 7 deletions NBAapi/player.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import requests
import pandas as pd
from NBAapi.credentials import DEFAULT_HEADERS

def stats(college='',conference='',country='',datefrom='',dateto='',division='',
draftpick='',draftyear='',gamescope='',gamesegment='',height='',
Expand Down Expand Up @@ -47,7 +48,7 @@ def stats(college='',conference='',country='',datefrom='',dateto='',division='',
'Weight' : weight,
}
u_a = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36"
response = requests.get(url,params=api_param,headers={"USER-AGENT":u_a})
response = requests.get(url,params=api_param,headers=DEFAULT_HEADERS)
data = response.json()
return pd.DataFrame(data['resultSets'][0]['rowSet'],columns=data['resultSets'][0]['headers'])

Expand Down Expand Up @@ -93,7 +94,7 @@ def biostats(college='', conference='', country='', datefrom='', dateto='', divi
'Weight' : weight,
}
u_a = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36"
response = requests.get(url,params=api_param,headers={"USER-AGENT":u_a})
response = requests.get(url,params=api_param,headers=DEFAULT_HEADERS)
data = response.json()
return pd.DataFrame(data['resultSets'][0]['rowSet'],columns=data['resultSets'][0]['headers'])

Expand All @@ -105,7 +106,7 @@ def commonallplayers(currentseason=0,leagueid='00',season='2015-16'):
'Season' : season,
}
u_a = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36"
response = requests.get(url,params=api_param,headers={"USER-AGENT":u_a})
response = requests.get(url,params=api_param,headers=DEFAULT_HEADERS)
data = response.json()
return pd.DataFrame(data['resultSets'][0]['rowSet'],columns=data['resultSets'][0]['headers'])

Expand All @@ -117,7 +118,7 @@ def careerstats(playerid,permode='PerGame',leagueid='00'):
'PlayerID' : playerid,
}
u_a = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36"
response = requests.get(url,params=api_param,headers={"USER-AGENT":u_a})
response = requests.get(url,params=api_param,headers=DEFAULT_HEADERS)
data = response.json()
return pd.DataFrame(data['resultSets'][0]['rowSet'],columns=data['resultSets'][0]['headers'])

Expand All @@ -132,7 +133,7 @@ def gamelog(playerid,DateFrom='',DateTo='',LeagueID='00',Season='2016-17',Season
'SeasonType' : SeasonType,
}
u_a = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36"
response = requests.get(url,params=api_param,headers={"USER-AGENT":u_a})
response = requests.get(url,params=api_param,headers=DEFAULT_HEADERS)
data = response.json()
return pd.DataFrame(data['resultSets'][0]['rowSet'],columns=data['resultSets'][0]['headers'])

Expand Down Expand Up @@ -163,7 +164,7 @@ def dashptreb(playerid,DateFrom='',DateTo='',GameSegment='',LastNGames='0',Leagu
'VsDivision' : VsDivision,
}
u_a = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36"
response = requests.get(url,params=api_param,headers={"USER-AGENT":u_a})
response = requests.get(url,params=api_param,headers=DEFAULT_HEADERS)
data = response.json()
return pd.DataFrame(data['resultSets'][0]['rowSet'],columns=data['resultSets'][0]['headers'])

Expand Down Expand Up @@ -209,7 +210,7 @@ def hustlestatsplayer(College='',Conference='',Country='',DateFrom='',DateTo='',
'Weight' : Weight,
}
u_a = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36"
response = requests.get(url,params=api_param,headers={"USER-AGENT":u_a})
response = requests.get(url,params=api_param,headers=DEFAULT_HEADERS)
data = response.json()
return pd.DataFrame(data['resultSets'][0]['rowSet'],columns=data['resultSets'][0]['headers'])

15 changes: 7 additions & 8 deletions NBAapi/plot.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#%%
from matplotlib.patches import Circle, Rectangle, Arc
import matplotlib.pyplot as plt
import numpy as np
from scipy import misc
import urllib, cStringIO
import urllib.request, urllib.parse, urllib.error, io
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
from matplotlib.colors import LinearSegmentedColormap
Expand Down Expand Up @@ -186,7 +185,7 @@ def text_in_zone(string,zone,box_alpha = 0.75,**kwargs):

def players_picture(player_id):
URL = "http://stats.nba.com/media/players/230x185/%d.png" %player_id
file = cStringIO.StringIO(urllib.urlopen(URL).read())
file = io.BytesIO(urllib.request.urlopen(URL).read())
return misc.imread(file)

def grantland_shotchart(shotchart,leagueavergae,ax=None,short_three=False,fg_range=[-9,9]):
Expand Down Expand Up @@ -221,7 +220,7 @@ def grantland_shotchart(shotchart,leagueavergae,ax=None,short_three=False,fg_ran
counts_norm[(counts>=1) & (counts<2)] = 0.3
patches=[]
colors=[]
for offc in xrange(verts.shape[0]):
for offc in range(verts.shape[0]):
if counts_norm[offc] != 0:
xc,yc = verts[offc][0],verts[offc][1]
b[:,0] = xy[:,0]*counts_norm[offc] + xc
Expand Down Expand Up @@ -269,9 +268,9 @@ def grantland_shotchart(shotchart,leagueavergae,ax=None,short_three=False,fg_ran
ax.add_collection(p)
p.set_clim([0, len(bins)-1])

pic = players_picture(shotchart.loc[0,'PLAYER_ID'])
ax.imshow(pic,extent=[15,25,30,37.8261])
ax.text(20,29,shotchart.loc[0,'PLAYER_NAME'],fontsize=16,horizontalalignment='center',verticalalignment='center')
# pic = players_picture(shotchart.loc[0,'PLAYER_ID'])
# ax.imshow(pic,extent=[15,25,30,37.8261])
# ax.text(20,29,shotchart.loc[0,'PLAYER_NAME'],fontsize=16,horizontalalignment='center',verticalalignment='center')

def shot_zone(X,Y):
'''
Expand Down Expand Up @@ -426,4 +425,4 @@ def shot_scatter(df,player_pic=True,ax=None,noise=True,**kwargs):
pic = players_picture(player_id)
ax.imshow(pic,extent=[15,25,30,37.8261])
ax.text(20,29,name,fontsize=16,horizontalalignment='center',verticalalignment='center')
ax.text(0,-7,'By: Doingthedishes',color='black',horizontalalignment='center',fontsize=20,fontweight='bold')
ax.text(0,-7,'By: Doingthedishes',color='black',horizontalalignment='center',fontsize=20,fontweight='bold')
Loading