forked from cha63506/NBAStats-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Nba.py
99 lines (81 loc) · 2.98 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import json
import PlayerInfo as info
import pandas as p
import requests as rq
import urllib.request
import shutil
import os
#from java.util import HashMap
map = {}
pic_dir = os.environ['HOME'] + "/NBAStats/Pics/"
p.options.display.max_columns = 50
class Nba():
def __init__(self):
with open('players.json') as f:
self.data = json.load(f)
def get_data(self):
return self.data
def parse_data(self, data):
for i in range(len(data)):
first_name = data[i]['firstName']
last_name = data[i]['lastName']
player_id = data[i]['playerId']
name = first_name + " " + last_name
map[name] = player_id;
def find_id(self, player_name):
for key, value in map.items():
if key == player_name:
return value
def get_player_bio(self, info, id):
url = "http://stats.nba.com/stats/commonplayerinfo?"
api_params = {'PlayerID' : id,'LeagueID' : '00'}
x = rq.get(url, params=api_params).json()
headers = x['resultSets'][0]['headers']
results = x['resultSets'][0]['rowSet']
info.set_birthday(results[0][headers.index('BIRTHDATE')])
info.set_school(results[0][headers.index('SCHOOL')])
info.set_height(results[0][headers.index('HEIGHT')])
info.set_weight(results[0][headers.index('WEIGHT')])
info.set_years_pro(results[0][headers.index('SEASON_EXP')])
info.set_team(results[0][headers.index('TEAM_NAME')])
def get_player_stats(self, info, id):
url = "http://stats.nba.com/stats/playercareerstats?"
api_params = {'PlayerID' : id,'LeagueID' : '00', 'PerMode' : 'PerGame'}
x = rq.get(url, params=api_params).json()
headers = x['resultSets'][1]['headers']
results = x['resultSets'][1]['rowSet']
info.set_ppg(results[0][headers.index('PTS')])
info.set_ast(results[0][headers.index('AST')])
info.set_reb(results[0][headers.index('REB')])
info.set_stl(results[0][headers.index('STL')])
def get_player_pic(self, id):
url = "http://stats.nbsa.com/media/players/230x185/"+str(id)+".png"
img = str(id) + ".png"
return urllib.request.urlretrieve(url, img)[0]
def check_pic(pic):
if not os.path.exists(pic_dir + pic):
shutil.move(pic, pic_dir)
else:
os.remove(pic)
def gen_html(df):
f = open('report.html', 'w')
head = """<html>
<head>Report</head>
"""
body = "<body>" + df.to_html() + "</body>"
message = head + body + "\n</html>"
f.write(message)
f.close()
if __name__ == "__main__":
nba = Nba()
data = nba.get_data()
nba.parse_data(data)
player_name = input("Enter Player Name: ")
info = info.PlayerInfo(player_name)
id = nba.find_id(player_name)
df = nba.get_player_bio(info, id)
nba.get_player_stats(info, id)
info.print_data()
pic = nba.get_player_pic(id)
check_pic(pic)
gen_html(df)