-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnba.py
52 lines (42 loc) · 1.9 KB
/
nba.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import requests
import json
from random import randint
class NBA():
def __init__(self, teamID):
self.teamList = self.getTeams()
self.team = teamID or self.getRandomTeam()
self.season = "2016-17" # has to be manually changed for every season
def makeRequest(self, url):
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0'}
r = requests.get(url=url, headers=headers)
if r.status_code == 400:
print('NBA we have a problem')
response = r.text
return json.loads(response)
def getTeams(self):
teams = []
with open('teams.txt') as f:
for line in f:
teams.append(line.strip())
return teams
def getRandomTeam(self):
return self.teamList[randint(0, len(self.teamList)-1)]
def getTeam(self):
url = 'http://stats.nba.com/stats/teaminfocommon?LeagueID=00&SeasonType=Regular+Season&TeamID={}&season={}'.format(self.team, self.season)
data = self.makeRequest(url)
city = data['resultSets'][0]['rowSet'][0][2]
nickname = data['resultSets'][0]['rowSet'][0][3]
return city + ' ' + nickname
def getRoster(self):
url = 'http://stats.nba.com/stats/commonteamroster?LeagueID=00&Season={}&TeamID={}'.format(self.season, self.team)
data = self.makeRequest(url)
return data["resultSets"][0]["rowSet"]
class Player():
def __init__(self, playerID):
self.playerID = playerID
self.season = '2016-17'
def getShotData(self):
url = "http://stats.nba.com/stats/shotchartdetail?Period=0&VsConference=&LeagueID=00&LastNGames=0&TeamID=0&Position=&Location=&Outcome=&ContextMeasure=FGA&DateFrom=&StartPeriod=&DateTo=&OpponentTeamID=0&ContextFilter=&RangeType=&Season={}&AheadBehind=&PlayerID={}&EndRange=&VsDivision=&PointDiff=&RookieYear=&GameSegment=&Month=0&ClutchTime=&StartRange=&EndPeriod=&SeasonType=Regular+Season&SeasonSegment=&GameID=&PlayerPosition=".format(self.season, self.playerID)
nba = NBA(0)
data = nba.makeRequest(url)
return data