-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathNBash.py
491 lines (401 loc) · 18.1 KB
/
NBash.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
from asciimatics.effects import Effect
from asciimatics.scene import Scene
from asciimatics.screen import Screen
from asciimatics.exceptions import ResizeScreenError
from asciimatics.event import KeyboardEvent
import sys
from models.Team import Team
from models.Game import Game
# from Player import Player
import requests
from bs4 import BeautifulSoup
from utilities.helpers import get_header, GetElementsByClass, ClearScreen
from utilities.helpers import GetOneElementByClass, GetTextOfItem
base_url = 'https://nba.hupu.com/games'
line_height = 5
choice = ''
all_game_lists = None
games = None
class AllGameView(Effect):
def __init__(self, screen, **kwargs):
super(AllGameView, self).__init__(screen, **kwargs)
global all_game_lists
global games
all_game_lists = self._GetAllGamesList()
games = self._GetAllGames(all_game_lists)
self._num_of_games = len(games)
self._all_games_finished = False
def process_event(self, event):
global choice
if isinstance(event, KeyboardEvent):
key = event.key_code
if key in range(ord('a'), ord('a') + self._num_of_games) or\
key in range(ord('A'), ord('A') + self._num_of_games):
choice = chr(key)
return event
else:
return event
def reset(self):
ClearScreen(self._screen)
self._all_games_finished = False
return
def _update(self, frame_no):
global games
if (frame_no - 1) % 60 == 0:
if self._all_games_finished is False:
ClearScreen(self._screen)
all_game_lists = self._GetAllGamesList()
games = self._GetAllGames(all_game_lists)
self._all_games_finished = self._CheckAllGamesOver()
else:
pass
self._GoLive(int(frame_no / 60) + 1)
return
@property
def stop_frame(self):
return self._stop_frame
def _GoLive(self, progress_count):
global choice
instructions = '请输入查看的场次: '
copy_right = '本工具所有数据都来自于虎扑(https://www.hupu.com/)'
row_number = int(self._num_of_games / 3) + 1
if self._num_of_games % 3 == 0:
row_number = row_number - 1
self._DrawBoard(row_number, 3)
row = 0
col = 0
for game in games:
self._DrawOneGame(game, row * line_height + 1, col * 25 + 1)
col = (col + 1) % 3
if col == 0:
row = row + 1
if progress_count % 2 == 0:
self._screen.print_at('*********', 0, (row + 1) * line_height + 5)
else:
self._screen.print_at(' ', 0, (row + 1) * line_height + 5)
self._screen.print_at(
instructions + str(choice), 0, (row + 1) * line_height + 3)
self._screen.print_at(
copy_right, 0, self._screen.height - 1)
def _DrawBoard(self, row, col=3, line_height=5, line_width=25):
for i in range(0, row + 1):
self._screen.move(0, i * line_height)
self._screen.draw(line_width * col, i * line_height, char='-')
for i in range(0, col + 1):
self._screen.move(i * line_width, 0)
self._screen.draw(i * line_width, line_height * row + 1, char='|')
return
def _GetAllGamesList(self):
headers = {"User-Agent": get_header()}
base_r = requests.get(url=base_url, headers=headers)
base_content = BeautifulSoup(base_r.content, features='html.parser')
all_game_lists = base_content.find_all('div', {'class': 'list_box'})
return all_game_lists
def _GetAllGames(self, all_game_lists):
index = 'A'
games_list = []
for item in all_game_lists:
team_vs = GetOneElementByClass(item, 'div', 'team_vs')
team_details_div = GetElementsByClass(team_vs, 'div', 'txt')
team_score_a = GetOneElementByClass(
team_details_div[0], 'span', 'num')
team_score_b = GetOneElementByClass(
team_details_div[1], 'span', 'num')
team_score_a = GetTextOfItem(team_score_a, '0')
team_score_b = GetTextOfItem(team_score_b, '0')
team_name_a = GetTextOfItem(team_details_div[0].find('a'))
team_name_b = GetTextOfItem(team_details_div[1].find('a'))
team_a = Team(name=team_name_a, score=team_score_a)
team_b = Team(name=team_name_b, score=team_score_b)
team_vs_time = GetOneElementByClass(team_vs, 'span', 'b')
team_vs_time = GetTextOfItem(team_vs_time, ' 未开始')
games_list.append(Game(index, team_a, team_b, team_vs_time))
index = chr(ord(index) + 1)
return games_list
def _CheckAllGamesOver(self):
keywords = '已结束'
all_game_lists = games
if all(keywords in item.time
for item in all_game_lists if item.time is not None):
return True
else:
return False
def _DrawOneGame(self, game, row, col):
self._screen.print_at(str(game.index), int(25 / 2) + col, row)
row = row + 1
team_vs_names = ' {0} vs {1}'.format(
game.teamA.name, game.teamB.name)
self._screen.print_at(team_vs_names, col, row)
row = row + 1
team_vs_scores = ' {0} vs {1}'.format(
game.teamA.score, game.teamB.score)
prefix_blank = ' '
str_versus = ' vs '
self._screen.print_at(prefix_blank, col, row)
if int(game.teamA.score) > int(game.teamB.score):
self._screen.print_at(
game.teamA.score, col + len(prefix_blank), row,
colour=self._screen.COLOUR_GREEN)
col_len = len(prefix_blank) + len(game.teamA.score)
str_tmp = '{0}{1}'.format(str_versus, game.teamB.score)
self._screen.print_at(str_tmp, col + col_len, row)
elif int(game.teamA.score) < int(game.teamB.score):
str_tmp = '{0}{1}'.format(game.teamA.score, str_versus)
self._screen.print_at(str_tmp, col + len(prefix_blank), row)
col_len = len(prefix_blank) + len(str_tmp)
self._screen.print_at(game.teamB.score, col + col_len, row,
colour=self._screen.COLOUR_GREEN)
else:
self._screen.print_at(team_vs_scores, col, row)
row = row + 1
team_vs_time = ' {0}'.format(game.time.strip())
self._screen.print_at(team_vs_time, col, row)
row = row + 1
class GameDetailsView(Effect):
def __init__(self, screen, **kwargs):
super(GameDetailsView, self).__init__(screen, **kwargs)
self._one_game_finished = False
def reset(self):
ClearScreen(self._screen)
self._one_game_finished = False
pass
def _update(self, frame_no):
global choice
global games
if (frame_no - 1) % 60 == 0:
if self._one_game_finished is False:
ClearScreen(self._screen)
one_game_details_table =\
self._GetOneGameDetails(choice.upper())
if len(one_game_details_table) != 0:
if '结束' in games[ord(choice.upper()) - ord('A')].time:
self._one_game_finished = True
self._DrawOneGameDetails(
games[ord(choice.upper()) - ord('A')],
one_game_details_table, 0)
else:
self._screen.print_at('比赛未开始', 0, 0)
@property
def stop_frame(self):
pass
def _GetOneGameDetails(self, str_index):
global all_game_lists
table_details = {}
index = ord(str_index) - ord('A')
score_live = GetOneElementByClass(all_game_lists[index], 'a', 'd')
score_live_r = requests.get(score_live['href'])
score_live_content = BeautifulSoup(score_live_r.content,
features='html.parser')
score_live_content_core = GetOneElementByClass(
score_live_content, 'div', 'gamecenter_content_l')
score_live_content_core_table_away = score_live_content_core.find(
'table', {'id': 'J_away_content'}) # special one
score_live_content_core_table_home = score_live_content_core.find(
'table', {'id': 'J_home_content'}) # special one
if score_live_content_core_table_away is None or \
score_live_content_core_table_home is None:
return table_details
table_details_away = self._GetDetailTable(
score_live_content_core_table_away)
table_details_home = self._GetDetailTable(
score_live_content_core_table_home)
score_in_section_away, score_in_section_home = self._GetScoreInSection(
score_live_content)
time = ''
time_div = GetOneElementByClass(
score_live_content, 'div', 'team_num', '已结束')
if time_div is not '已结束':
time = time_div.get_text().strip()
teamA_name, teamB_name, teamA_score, teamB_score\
= self._GetTeamNamesAndScores(score_live_content)
table_details['away_player_details'] = table_details_away
table_details['home_player_details'] = table_details_home
table_details['away_section_scores'] = score_in_section_away
table_details['home_section_scores'] = score_in_section_home
table_details['time'] = time
table_details['teamA_name'] = teamA_name
table_details['teamB_name'] = teamB_name
table_details['teamA_score'] = teamA_score
table_details['teamB_score'] = teamB_score
return table_details
def _GetTeamNamesAndScores(self, live_content):
teamA_div = GetOneElementByClass(
live_content, 'div', 'team_a')
teamA_msg_div = GetOneElementByClass(
teamA_div, 'div', 'message')
teamA_name = teamA_msg_div.p.get_text().strip()
teamA_score = teamA_msg_div.h2.get_text().strip()
teamB_div = GetOneElementByClass(
live_content, 'div', 'team_b')
teamB_msg_div = GetOneElementByClass(
teamB_div, 'div', 'message')
teamB_name = teamB_msg_div.p.get_text().strip()
teamB_score = teamB_msg_div.h2.get_text().strip()
return teamA_name, teamB_name, teamA_score, teamB_score
def _GetScoreInSection(self, live_content):
score_in_section_table = GetOneElementByClass(
live_content, 'table', 'itinerary_table')
score_in_section_away_table = GetOneElementByClass(
score_in_section_table, 'tr', 'away_score')
score_in_section_home_table = GetOneElementByClass(
score_in_section_table, 'tr', 'home_score')
section_counts = len(score_in_section_away_table.find_all('td')[1:-1])
away_score_list = ['0' for n in range(section_counts)]
home_score_list = ['0' for n in range(section_counts)]
away_index = 0
home_index = 0
for item in score_in_section_away_table.find_all('td')[1:-1]:
away_score_list[away_index] = item.get_text().strip()
away_index += 1
for item in score_in_section_home_table.find_all('td')[1:-1]:
home_score_list[home_index] = item.get_text().strip()
home_index += 1
return away_score_list, home_score_list
def _GetDetailTable(self, content_table):
table_details = []
header_row_tr = []
try:
header_row_tr = content_table.find_all('tr')[0]
except Exception:
return table_details
table_header = []
for td_item in header_row_tr.find_all('td'):
table_header.append(td_item.get_text().strip())
table_details.append(table_header)
for item in content_table.find_all('tr')[1:-2]:
one_row = []
for index, td_item in enumerate(item.find_all('td')):
td_text = td_item.get_text().strip()
if index == 0 and len(td_text) > 10:
td_text = td_text[td_text.find('-') + 1:]
one_row.append(td_text)
table_details.append(one_row)
return table_details
def _DrawScoresInDetailsPage(self, time, nameA, nameB, scoreA, scoreB,
col_width_away_total, start_row):
game_info_a = '{0}:{1}'.format(nameA, scoreA)
self._screen.print_at(
game_info_a, int(col_width_away_total / 2), start_row)
game_info_b = '{0}:{1}'.format(nameB, scoreB)
self._screen.print_at(
game_info_b, int(col_width_away_total * 3 / 2), start_row)
self._screen.print_at(
time, col_width_away_total, start_row)
return
def _DrawScoreInSection(self, score_in_sections,
start_col_index, start_row):
section_index = 1
self._screen.print_at('节次', start_col_index - 5, start_row + 2)
self._screen.print_at('比分', start_col_index - 5, start_row + 3)
for item in score_in_sections:
self._screen.print_at(
str(section_index), start_col_index, start_row + 2)
self._screen.print_at(item, start_col_index, start_row + 3)
start_col_index += 3
section_index += 1
return
def _DrawOneGameDetailsCore(self, table_items, width_list,
col_width_total, row, col=0):
col_width_away_total = 0
valid_list_width = [x for x in width_list if x != 0]
for item in table_items:
col_index = 0
valid_index = 0
col_width_away_total = 0
for row_item in item:
if width_list[col_index] == 0:
pass
else:
col_coord = valid_index * valid_list_width[valid_index] + 1
if valid_index > 0:
col_coord = valid_list_width[0] + \
(valid_index - 1) * \
valid_list_width[valid_index] + 1
if col_width_total != 0:
col_coord += col_width_total
self._screen.print_at(row_item, col_coord, row)
col_width_away_total += valid_list_width[valid_index]
valid_index = valid_index + 1
col_index = col_index + 1
row = row + 1
col_width_away_total += 1
return col_width_away_total
def _DrawOneGameDetailsFullMode(self, game, details_table,
start_row, start_col=0):
col_len = len(details_table['away_player_details'][0])
if col_len == 0:
return
col_width_list = [18, 0, 5]
for i in range(len(col_width_list), col_len):
col_width_list.append(5)
col_width_away_total = self._DrawOneGameDetailsCore(
details_table['away_player_details'],
col_width_list, 0, start_row, start_col)
self._DrawOneGameDetailsCore(
details_table['home_player_details'],
col_width_list, col_width_away_total,
start_row, start_col)
self._DrawScoresInDetailsPage(details_table['time'],
details_table['teamA_name'],
details_table['teamB_name'],
details_table['teamA_score'],
details_table['teamB_score'],
col_width_away_total, 0)
self._DrawScoreInSection(details_table['away_section_scores'],
int(col_width_away_total / 2), 0)
self._DrawScoreInSection(details_table['home_section_scores'],
int(col_width_away_total * 3 / 2), 0)
return
def _DrawOneGameDetailsSimpleMode(self, game, details_table,
start_row, start_col=0):
col_len = len(details_table['away_player_details'][0])
if col_len == 0:
return
col_width_list = [18, 0, 0, 0, 0, 0, 0, 0]
for i in range(len(col_width_list), col_len):
col_width_list.append(5)
col_width_away_total = self._DrawOneGameDetailsCore(
details_table['away_player_details'],
col_width_list, 0, start_row, start_col)
self._DrawOneGameDetailsCore(
details_table['home_player_details'],
col_width_list, col_width_away_total,
start_row, start_col)
self._DrawScoresInDetailsPage(details_table['time'],
details_table['teamA_name'],
details_table['teamB_name'],
details_table['teamA_score'],
details_table['teamB_score'],
col_width_away_total, 0)
self._DrawScoreInSection(details_table['away_section_scores'],
int(col_width_away_total / 2), 0)
self._DrawScoreInSection(details_table['home_section_scores'],
int(col_width_away_total * 3 / 2), 0)
return
def _DrawOneGameDetails(self, game, details_table, start_row, start_col=0):
row_index = start_row + 6
col_index = start_col
if self._screen.width > 190:
self._DrawOneGameDetailsFullMode(
game, details_table, row_index, col_index)
else:
self._DrawOneGameDetailsSimpleMode(
game, details_table, row_index, col_index)
return
def demo(screen):
scenes = []
effects = [
AllGameView(screen)
]
scenes.append(Scene(effects, -1))
effects = [
GameDetailsView(screen)
]
scenes.append(Scene(effects, -1))
screen.play(scenes, stop_on_resize=True)
while True:
try:
Screen.wrapper(demo)
sys.exit(0)
except ResizeScreenError:
pass