-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbox.py
470 lines (361 loc) · 13.1 KB
/
box.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import sys
import datetime
import time
import json
import argparse
import urllib.request
from constant import BOX_URL_TEMPLATE, TEAM_DICT, BoxColors
def download_box_json(year, gameid):
url = BOX_URL_TEMPLATE % (year, gameid)
url = url + "?" + str(int(time.time() * 1000.0))
response = urllib.request.urlopen(url)
data = response.read()
text = data.decode("utf-8")
return json.loads(text[15:])
def get_default_season():
today = datetime.datetime.now()
return today.year if today.month >= 7 else today.year - 1
def colorize(content, color):
return BoxColors.get_color_code(color) + content + BoxColors.ENDC
def print_blank_line(count=1):
print("\n" * count, end='')
def print_box(box):
quarters = ["1st", "2nd", "3rd", "4th"]
for i in range(box["score"]["periodTime"]["period"] - 4):
quarters.append("OT" + str(i + 1))
print(" " + " ".join(quarters) + " FINAL")
print_box_team(box, home=False)
print_box_team(box, home=True)
def print_box_team(box, home=True):
team = "home" if home else "visitor"
line = box["score"][team]["id"].ljust(6)
line += " ".join(list(map(lambda s: str(s).rjust(3), box["score"][team]["qScore"])))
line += " "
line += str(box["score"][team]["score"]).rjust(3)
if home:
print(colorize(line, BoxColors.YELLOW))
else:
print(colorize(line, BoxColors.GREEN))
def print_arena_stats(box):
print(colorize("ARENA STATS ", BoxColors.STATS_HEADER))
print("Arena: " + colorize(box["arena"], BoxColors.CYAN))
print(" " + colorize(box["location"], BoxColors.CYAN))
print("Attendance: " + colorize(str(box["attendance"]), BoxColors.CYAN))
print("Duration: " + colorize(box["duration"], BoxColors.CYAN))
def print_scoring_stats(box):
print(colorize("SCORING ", BoxColors.STATS_HEADER))
print("Lead Changes: " + colorize(str(box["score"]["stats"]["leadChanges"]), BoxColors.CYAN))
print("Times Tied: " + colorize(str(box["score"]["stats"]["tied"]), BoxColors.CYAN))
def print_frame(line, home=True):
if home:
print(colorize(line, BoxColors.HOME_FRAME))
else:
print(colorize(line, BoxColors.VISITOR_FRAME))
def print_top_frame(home=True):
print_frame("┌─────────────────────────────────────┐", home)
def print_divider(home=True):
print_frame("├─────────────────────────────────────┤", home)
def print_bottom_frame(home=True):
print_frame("└─────────────────────────────────────┘", home)
def print_with_side_frame(line, home=True):
frame = ""
if home:
frame = colorize("│", BoxColors.HOME_FRAME)
else:
frame = colorize("│", BoxColors.VISITOR_FRAME)
print(frame + line + frame)
def print_header_team(box, home):
team = "home" if home else "visitor"
line = get_team_name(box["score"][team]["id"])
if home and "hr" in box["stats"]["home"]:
line += (" (" + box["stats"]["home"]["hr"] + ")")
elif "vr" in box["stats"]["visitor"]:
line += (" (" + box["stats"]["visitor"]["vr"] + ")")
line = line.ljust(34) + "◢███████████████████"
line = colorize(line, BoxColors.YELLOW)
print_with_side_frame(line, home)
def print_header_players(home):
line = "PLAYERS MIN FGM-A 3PM-A FTM-A +/- OR DR TR AS PF ST TO BS BA PTS EFF"
if home:
line = colorize(line, BoxColors.HOME_HEADER)
else:
line = colorize(line, BoxColors.VISITOR_HEADER)
print_with_side_frame(line, home)
def print_header_total(home):
line = " MIN FGM-A 3PM-A FTM-A OR DR TR AS PF ST TO BS BA PTS "
if home:
line = colorize(line, BoxColors.HOME_HEADER)
else:
line = colorize(line, BoxColors.VISITOR_HEADER)
line = " " + line
print_with_side_frame(line, home)
def print_player_stats(box, home):
team = "home" if home else "visitor"
for player in box["stats"][team]["players"]:
if is_player_dnp(player):
content = get_player_name(player, True)
content += " "
content += (" " + colorize("DNP", BoxColors.DARK_GREEN) + " ")
content = content.ljust(90)
print_with_side_frame(content, home)
continue
content = " ".join([
get_player_name(player),
get_player_minutes(player),
get_player_fg(player),
get_player_3pt(player),
get_player_ft(player),
get_player_pm(player),
get_player_oreb(player),
get_player_dreb(player),
get_player_treb(player),
get_player_ast(player),
get_player_pf(player),
get_player_stl(player),
get_player_to(player),
get_player_bs(player),
get_player_ba(player),
get_player_pts(player),
get_player_eff(player)
])
print_with_side_frame(content, home)
def print_total_stats(box, home):
team = "home" if home else "visitor"
team = box["stats"][team]["team"]
content = ""
if home:
content = colorize("TOTAL", BoxColors.HOME_HEADER)
else:
content = colorize("TOTAL", BoxColors.VISITOR_HEADER)
content += " "
content += " ".join([
get_team_minutes(team),
get_team_fg(team),
get_team_3pt(team),
get_team_ft(team),
" ",
get_team_oreb(team),
get_team_dreb(team),
get_team_treb(team),
get_team_ast(team),
get_team_pf(team),
get_team_stl(team),
get_team_to(team),
get_team_bs(team),
get_team_ba(team),
get_team_pts(team)
])
content += " "
print_with_side_frame(content, home)
content = " "
content += " ".join([
get_team_fg_percentage(team),
get_team_3pt_percentage(team),
get_team_ft_percentage(team)
])
content += " "
print_with_side_frame(content, home)
def get_player_name(player, dnp=False):
name = ""
is_starter = True if player["spos"] != "" else False
name_limit = 9 if is_starter else 11
if player["fn"] != "" and len(player["ln"]) <= name_limit - 2:
name = player["fn"][0] + "." + player["ln"]
else:
name = player["ln"]
name = name.ljust(name_limit) if len(name) <= name_limit else name[:name_limit]
if is_starter:
name = colorize(name, BoxColors.WHITE) + " " + player["spos"][-1]
elif dnp:
name = colorize(name, BoxColors.DARK_GREEN)
return name
def get_player_minutes(player):
return str(player["min"]).zfill(2) + ":" + str(player["sec"]).zfill(2)
def get_player_fg(player):
fg = str(player["fgm"]) + "-" + str(player["fga"])
fg = fg.rjust(5)
if player["fga"] == 0:
return fg
fgp = player["fgm"] / player["fga"]
if player["fgm"] == player["fga"]:
fg = colorize(fg, BoxColors.RED)
elif fgp >= 0.67:
fg = colorize(fg, BoxColors.YELLOW)
elif fgp <= 0.33:
fg = colorize(fg, BoxColors.GREEN)
return fg
def get_player_3pt(player):
tp = str(player["tm"]) + "-" + str(player["ta"])
tp = tp.rjust(5)
if player["ta"] == 0:
return tp
tpp = player["tm"] / player["ta"]
if player["tm"] == player["ta"]:
tp = colorize(tp, BoxColors.RED)
elif tpp >= 0.6:
tp = colorize(tp, BoxColors.YELLOW)
elif tpp <= 0.25:
tp = colorize(tp, BoxColors.GREEN)
return tp
def get_player_ft(player):
ft = str(player["ftm"]) + "-" + str(player["fta"])
ft = ft.rjust(5)
if player["fta"] == 0:
return ft
ftp = player["ftm"] / player["fta"]
if player["ftm"] == player["fta"]:
ft = colorize(ft, BoxColors.RED)
elif ftp < 0.5:
ft = colorize(ft, BoxColors.YELLOW)
elif player["ftm"] == 0:
ft = colorize(ft, BoxColors.GREEN)
return ft
def get_player_pm(player):
sign = "+" if player["pm"] >= 0 else "-"
pm = str(abs(player["pm"])).rjust(2)
return sign + pm
def get_player_oreb(player):
return str(player["or"]).rjust(2)
def get_player_dreb(player):
return str(player["dr"]).rjust(2)
def get_player_treb(player):
treb = str(player["or"] + player["dr"])
treb = treb.rjust(2)
if player["or"] + player["dr"] >= 10:
treb = colorize(treb, BoxColors.RED)
return treb
def get_player_ast(player):
ast = str(player["a"])
ast = ast.rjust(2)
if player["a"] >= 10:
ast = colorize(ast, BoxColors.RED)
return ast
def get_player_pf(player):
pf = str(player["f"]).rjust(2)
if player["f"] >= 6:
pf = colorize(pf, BoxColors.GREEN)
return pf
def get_player_stl(player):
stl = str(player["s"]).rjust(2)
if player["s"] >= 4:
stl = colorize(stl, BoxColors.RED)
return stl
def get_player_to(player):
to = str(player["to"]).rjust(2)
if player["to"] == 0:
to = colorize(to, BoxColors.RED)
elif player["to"] >= 10:
to = colorize(to, BoxColors.GREEN)
return to
def get_player_bs(player):
bs = str(player["b"]).rjust(2)
if player["b"] >= 4:
bs = colorize(bs, BoxColors.RED)
return bs
def get_player_ba(player):
ba = str(player["ba"]).rjust(2)
if player["ba"] >= 4:
ba = colorize(ba, BoxColors.GREEN)
return ba
def get_player_pts(player):
pts = str(player["p"])
pts = pts.rjust(3)
if player["p"] >= 30:
pts = colorize(pts, BoxColors.RED)
elif player["p"] >= 20:
pts = colorize(pts, BoxColors.YELLOW)
return pts
def get_player_eff(player):
val = (player["p"] + player["or"] + player["dr"] + player["a"] + player["s"] + player["b"]) - (player["fga"] - player["fgm"]) - (player["fta"] - player["ftm"]) - player["to"]
eff = str(val).rjust(3)
if val >= 20:
eff = colorize(eff, BoxColors.RED)
elif val < 0:
eff = colorize(eff, BoxColors.GREEN)
return eff
def get_team_minutes(team):
return str(team["min"]).rjust(3)
def get_team_fg(team):
return (str(team["fgm"]) + "-" + str(team["fga"])).rjust(6)
def get_team_3pt(team):
tp = str(team["tm"]) + "-" + str(team["ta"])
return colorize(tp.rjust(5), BoxColors.GREEN)
def get_team_ft(team):
ft = str(team["ftm"]) + "-" + str(team["fta"])
return colorize(ft.rjust(5), BoxColors.CYAN)
def get_team_oreb(team):
oreb = str(team["or"])
return colorize(oreb.rjust(2), BoxColors.YELLOW)
def get_team_dreb(team):
dreb = str(team["dr"])
return colorize(dreb.rjust(2), BoxColors.YELLOW)
def get_team_treb(team):
treb = str(team["or"] + team["dr"])
return colorize(treb.rjust(2), BoxColors.YELLOW)
def get_team_ast(team):
ast = str(team["a"])
return colorize(ast.rjust(2), BoxColors.CYAN)
def get_team_pf(team):
return str(team["f"]).rjust(2)
def get_team_stl(team):
return str(team["s"]).rjust(2)
def get_team_to(team):
return str(team["to"]).rjust(2)
def get_team_bs(team):
return str(team["b"]).rjust(2)
def get_team_ba(team):
return str(team["ba"]).rjust(2)
def get_team_pts(team):
return str(team["p"]).rjust(3)
def get_team_fg_percentage(team):
p = team["fgm"] / team["fga"] * 100.0
return colorize(("%.1f" % (p)).ljust(4) + "%", BoxColors.RED)
def get_team_3pt_percentage(team):
p = team["tm"] / team["ta"] * 100.0
return colorize(("%.1f" % (p)).ljust(4) + "%", BoxColors.GREEN)
def get_team_ft_percentage(team):
p = team["ftm"] / team["fta"] * 100.0
return colorize(("%.1f" % (p)).ljust(4) + "%", BoxColors.YELLOW)
def get_team_name(team):
if team in TEAM_DICT:
return TEAM_DICT[team]
else:
return team
def is_player_dnp(player):
return player["min"] == 0 and player["sec"] == 0
def print_team_box(box, home=True):
print_top_frame(home)
print_header_team(box, home)
print_header_players(home)
print_divider(home)
print_player_stats(box, home)
print_divider(home)
print_header_total(home)
print_total_stats(box, home)
print_bottom_frame(home)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("game_id", help="Game ID from https://watch.nba.com")
parser.add_argument("-s", "--season", help="For 2016-17 season, use 2016", type=int)
parser.add_argument("-c", "--control", help="ANSI color control code", choices=["esc", "ctrlu"])
args = parser.parse_args()
year = get_default_season()
if args.season != None:
year = args.season
if args.control == "ctrlu":
BoxColors.set_control_code(BoxColors.CTRLU)
try:
box_json = download_box_json(year, args.game_id)
except:
print("Error: cannot download the box")
sys.exit()
print_blank_line(3)
print_box(box_json)
print_blank_line(3)
print_arena_stats(box_json)
print_blank_line(2)
print_scoring_stats(box_json)
print_blank_line(4)
print_team_box(box_json, home=False)
print_blank_line()
print_team_box(box_json, home=True)