-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathCliManager.py
487 lines (402 loc) · 21.4 KB
/
CliManager.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
import platform
import shutil
import threading
import time
from enum import Enum
from typing import Optional
from service import ServiceLocator
from service.display.DisplayManager import ScreenTags
from service.youtube import YoutubeResultNav
if platform.system() == "Windows":
import msvcrt
import sys
def _getch():
return msvcrt.getwch()
else:
import tty
import termios
import sys
def _getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def getch():
ch = _getch()
if ch.encode() == b'\x03' or ch.encode() == b'\x1a':
sys.exit(0)
return ch
class CliDimensionsChangeHandlerThread(threading.Thread):
def __init__(self):
super().__init__()
self.stop_event = threading.Event()
self.lock = threading.Lock()
self._cli_cols = 0
self._cli_rows = 0
self._handlers = list()
def _check_cli_window_size(self):
cols, rows = shutil.get_terminal_size()
if self._cli_cols != cols or self._cli_rows != rows:
self._cli_cols = cols
self._cli_rows = rows
for handler in self._handlers:
handler(self._cli_cols, self._cli_rows)
def run(self):
while not self.stop_event.is_set():
self._check_cli_window_size()
time.sleep(0.2)
def stop(self):
self.stop_event.set()
def add_handler(self, handler):
with self.lock:
self._handlers.append(handler)
class CliInputs(Enum):
HOME = 72
CREATOR = 67
SEARCH = 83
VIDEO = 86
SPACE = 32
BACKSPACE = 8
ENTER = 13
ESC = 27
QUIT = 81
NAV_PREV = 80
NAV_NEXT = 78
NAV_TO_CREATOR = 75
LIKE = 76
DISLIKE = 68
UPDATE_SUBSCRIPTION = 85
def matches_input(self, keycode):
return ord(keycode.upper()) == self.value
class CliManager:
def __init__(self,
service_locator: ServiceLocator,
scaling_factor,
max_frame_width,
should_use_subtitles,
subtitles_lang):
self._service_locator = service_locator
self._scaling_factor = scaling_factor
self._max_frame_width = max_frame_width
self._should_use_subtitles = should_use_subtitles
self._subtitles_lang = subtitles_lang
self._current_video_id = None
self._current_video_creator_id = None
self._current_video_rating = 'none'
self._is_video_playing = False
self._current_creator_id = None
self._current_creator_subscription_status = False
self._cli_cols = 0
self._cli_rows = 0
self._cli_dimensions_change_thread = CliDimensionsChangeHandlerThread()
self._cli_dimensions_change_thread.daemon = True
self._cli_dimensions_change_thread.add_handler(self._cli_dimensions_on_change_handler)
self._cli_dimensions_change_thread.start()
def _cli_dimensions_on_change_handler(self, cli_cols, cli_rows):
self._cli_cols = cli_cols
self._cli_rows = cli_rows
self._service_locator.video_rendering_manager.update_cli_dimensions(cli_cols, cli_rows)
self._service_locator.display_manager.set_display_dimensions(cli_cols, cli_rows)
if self._service_locator.display_manager.current_screen_tag == ScreenTags.HOME:
self._navigate_to_other_video(
self._service_locator.youtube_connection_manager.get_home_result, YoutubeResultNav.CURRENT)
if self._service_locator.display_manager.current_screen_tag == ScreenTags.CREATOR:
self._navigate_to_other_video(
self._service_locator.youtube_connection_manager.get_creator_page_result, YoutubeResultNav.CURRENT)
if self._service_locator.display_manager.current_screen_tag == ScreenTags.SEARCH:
additional_screen_data = {
'query': self._service_locator.search_bar_manager.query
}
self._navigate_to_other_video(
self._service_locator.youtube_connection_manager.get_search_result,
YoutubeResultNav.CURRENT, additional_screen_data)
def run(self, video_url=None):
self._service_locator.auth_manager.set_on_credentials_expired(
self._handle_login_flow
)
if video_url is None:
self._handle_login_flow()
else:
if self._service_locator.auth_manager.is_authenticated():
self._service_locator.init_youtube_connection(self._service_locator.auth_manager.current_user)
self._service_locator.display_manager.set_screen_visibility(ScreenTags.HOME, True)
self._service_locator.display_manager.set_screen_visibility(ScreenTags.SEARCH, True)
self._service_locator.display_manager.set_screen_visibility(ScreenTags.VIDEO, True)
self._service_locator.display_manager.set_active_screen(ScreenTags.VIDEO)
self._display_current_screen({
'video_url': video_url
})
while True:
ch = getch()
self._on_ch_callback(ch)
def _handle_login_flow(self):
if not self._service_locator.auth_manager.is_authenticated():
self._service_locator.display_manager.issue_message_to_screen(
'Not authenticated. Log in to access full functionality.')
self._service_locator.auth_manager.authenticate()
if not self._service_locator.auth_manager.is_authenticated():
quit()
self._service_locator.init_youtube_connection(self._service_locator.auth_manager.current_user)
self._service_locator.search_bar_manager.reset_state()
self._service_locator.display_manager.reset_state()
self._service_locator.display_manager.set_screen_visibility(ScreenTags.HOME, True)
self._service_locator.display_manager.set_screen_visibility(ScreenTags.SEARCH, True)
screen_data = self._service_locator.youtube_connection_manager.get_home_result(YoutubeResultNav.FIRST)
self._service_locator.display_manager.set_active_screen(ScreenTags.HOME)
self._display_current_screen(screen_data)
def _display_current_screen(self, screen_data: Optional[dict] = None):
if self._service_locator.display_manager.current_screen_tag == ScreenTags.VIDEO:
if screen_data is None:
self._set_video_playing_state(True)
return
self._set_video_playing_state(False)
self._service_locator.display_manager.set_screen_visibility(ScreenTags.VIDEO, True)
self._service_locator.display_manager.set_active_screen(ScreenTags.VIDEO)
current_video_metadata = self._service_locator.video_stream_manager.get_video_metadata(
screen_data['video_url'], self._should_use_subtitles, self._subtitles_lang)
self._current_video_id = current_video_metadata['id']
self._current_video_creator_id = current_video_metadata['creator_id']
self._service_locator.video_subtitles_manager.set_subtitles_resource(
current_video_metadata['subtitles'], current_video_metadata['duration'])
if self._service_locator.auth_manager.is_authenticated():
self._current_video_rating = self._service_locator.youtube_connection_manager.get_video_rating(
self._current_video_id)
else:
self._current_video_rating = 'none'
self._service_locator.video_rendering_manager.init_state({
'display_callback': self._service_locator.display_manager.render_screen,
'subtitles_callback': self._service_locator.video_subtitles_manager.current_subtitle,
'target_fps': current_video_metadata['target_fps'],
'scaling_factor': self._scaling_factor,
'max_frame_width': self._max_frame_width,
'total_frames': current_video_metadata['target_fps'] * current_video_metadata['duration'],
'video_title': current_video_metadata['title'],
'video_view_count': current_video_metadata['view_count'],
'video_creator': current_video_metadata['creator'],
'rating': self._current_video_rating
})
self._set_video_playing_state(True)
self._service_locator.video_stream_manager.parse_video_stream(
stream_url=current_video_metadata['stream_url'],
frame_callback=self._service_locator.video_rendering_manager.render_frame)
else:
screen_data = screen_data if screen_data is not None else dict()
screen_data['screen_width'] = min(self._max_frame_width, self._cli_cols)
if 'thumbnail' in screen_data:
screen_data[
'rendered_thumbnail'] = (
self._service_locator.video_rendering_manager.ascii_converter.convert_frame_to_ascii(
screen_data['thumbnail'], self._cli_cols, self._scaling_factor, self._max_frame_width
))
self._set_video_playing_state(False)
self._service_locator.display_manager.render_screen(screen_data=screen_data)
def _set_video_playing_state(self, is_playing):
self._is_video_playing = is_playing
self._service_locator.video_rendering_manager.set_video_playing_state(is_playing)
self._service_locator.video_stream_manager.set_video_state(self._is_video_playing)
def _on_ch_callback(self, ch):
if self._service_locator.search_bar_manager.is_active:
self._on_key_pressed_search_query(ch)
elif CliInputs.HOME.matches_input(ch):
self._on_key_home_pressed()
elif CliInputs.CREATOR.matches_input(ch):
self._on_key_creator_pressed()
elif CliInputs.SEARCH.matches_input(ch):
self._on_key_search_pressed()
elif CliInputs.VIDEO.matches_input(ch):
self._on_key_video_pressed()
elif CliInputs.SPACE.matches_input(ch):
self._on_key_space_pressed()
elif CliInputs.ENTER.matches_input(ch):
self._on_key_enter_pressed()
elif CliInputs.NAV_PREV.matches_input(ch):
self._on_key_nav_prev_pressed()
elif CliInputs.NAV_NEXT.matches_input(ch):
self._on_key_nav_next_pressed()
elif CliInputs.NAV_TO_CREATOR.matches_input(ch):
self._on_key_nav_to_creator_pressed()
elif CliInputs.LIKE.matches_input(ch):
self._on_key_like_pressed()
elif CliInputs.DISLIKE.matches_input(ch):
self._on_key_dislike_pressed()
elif CliInputs.UPDATE_SUBSCRIPTION.matches_input(ch):
self._on_key_subscribe_pressed()
elif CliInputs.QUIT.matches_input(ch):
quit()
def _update_search_query_display_state(self, nav_state: YoutubeResultNav, nav_args=None):
if nav_args is None:
nav_args = dict()
screen_data = self._service_locator.youtube_connection_manager.get_search_result(nav_state, **nav_args)
screen_data = screen_data if screen_data is not None else dict()
screen_data |= {
'query': self._service_locator.search_bar_manager.query
}
self._display_current_screen(screen_data)
def _on_key_pressed_search_query(self, key):
if self._service_locator.display_manager.current_screen_tag == ScreenTags.SEARCH:
if CliInputs.BACKSPACE.matches_input(key):
self._service_locator.search_bar_manager.press_backspace()
self._update_search_query_display_state(YoutubeResultNav.CURRENT)
elif CliInputs.ENTER.matches_input(key):
self._service_locator.search_bar_manager.is_active = False
self._update_search_query_display_state(YoutubeResultNav.FIRST, {
'query': self._service_locator.search_bar_manager.query
})
elif CliInputs.ESC.matches_input(key):
self._service_locator.search_bar_manager.is_active = False
self._update_search_query_display_state(YoutubeResultNav.CURRENT)
else:
self._service_locator.search_bar_manager.append_to_query(key)
self._update_search_query_display_state(YoutubeResultNav.CURRENT)
def _on_key_home_pressed(self):
if not self._service_locator.auth_manager.is_authenticated():
self._handle_login_flow()
if self._service_locator.display_manager.current_screen_tag == ScreenTags.HOME:
return
if not self._service_locator.display_manager.can_navigate_to_screen(ScreenTags.HOME):
return
screen_data = self._service_locator.youtube_connection_manager.get_home_result(
YoutubeResultNav.CURRENT)
if screen_data is None:
screen_data = self._service_locator.youtube_connection_manager.get_home_result(
YoutubeResultNav.FIRST)
self._service_locator.display_manager.set_active_screen(ScreenTags.HOME)
self._display_current_screen(screen_data)
def _on_key_creator_pressed(self):
if not self._service_locator.auth_manager.is_authenticated():
self._handle_login_flow()
if self._service_locator.display_manager.current_screen_tag == ScreenTags.CREATOR:
return
if self._current_creator_id is None:
return
if not self._service_locator.display_manager.can_navigate_to_screen(ScreenTags.CREATOR):
return
screen_data = self._service_locator.youtube_connection_manager.get_creator_page_result(
YoutubeResultNav.CURRENT)
screen_data['is_subscribed'] = self._current_creator_subscription_status
self._service_locator.display_manager.set_active_screen(ScreenTags.CREATOR)
self._display_current_screen(screen_data)
def _on_key_search_pressed(self):
if not self._service_locator.auth_manager.is_authenticated():
self._handle_login_flow()
if self._service_locator.display_manager.current_screen_tag == ScreenTags.SEARCH:
self._service_locator.search_bar_manager.is_active = True
self._update_search_query_display_state(YoutubeResultNav.CURRENT)
return
if not self._service_locator.display_manager.can_navigate_to_screen(ScreenTags.SEARCH):
return
self._service_locator.display_manager.set_active_screen(ScreenTags.SEARCH)
self._update_search_query_display_state(YoutubeResultNav.CURRENT)
def _on_key_video_pressed(self):
if self._service_locator.display_manager.current_screen_tag == ScreenTags.VIDEO:
return
if not self._service_locator.display_manager.can_navigate_to_screen(ScreenTags.VIDEO):
return
self._service_locator.display_manager.set_active_screen(ScreenTags.VIDEO)
self._display_current_screen()
def _on_key_space_pressed(self):
if self._service_locator.display_manager.current_screen_tag == ScreenTags.VIDEO:
self._set_video_playing_state(not self._is_video_playing)
def _on_key_enter_pressed(self):
if self._service_locator.display_manager.current_screen_tag == ScreenTags.HOME:
self._navigate_to_current_video_screen(self._service_locator.youtube_connection_manager.get_home_result)
if self._service_locator.display_manager.current_screen_tag == ScreenTags.CREATOR:
self._navigate_to_current_video_screen(
self._service_locator.youtube_connection_manager.get_creator_page_result)
if self._service_locator.display_manager.current_screen_tag == ScreenTags.SEARCH:
self._navigate_to_current_video_screen(self._service_locator.youtube_connection_manager.get_search_result)
def _navigate_to_current_video_screen(self, get_video_data_callback):
current_video_data = get_video_data_callback(YoutubeResultNav.CURRENT)
self._service_locator.display_manager.set_screen_visibility(ScreenTags.VIDEO, True)
self._service_locator.display_manager.set_active_screen(ScreenTags.VIDEO)
self._display_current_screen({
'video_url': current_video_data['url']
})
def _navigate_to_other_video(self, get_video_data_callback, direction, additional_video_data=None):
current_video_data = get_video_data_callback(direction)
if current_video_data is None:
return
if additional_video_data is None:
self._display_current_screen(current_video_data)
else:
current_video_data |= additional_video_data
self._display_current_screen(current_video_data)
def _on_key_nav_prev_pressed(self):
if self._service_locator.display_manager.current_screen_tag == ScreenTags.HOME:
self._navigate_to_other_video(
self._service_locator.youtube_connection_manager.get_home_result, YoutubeResultNav.PREV)
if self._service_locator.display_manager.current_screen_tag == ScreenTags.CREATOR:
additional_screen_data = {
'is_subscribed': self._current_creator_subscription_status
}
self._navigate_to_other_video(self._service_locator.youtube_connection_manager.get_creator_page_result,
YoutubeResultNav.PREV, additional_screen_data)
if self._service_locator.display_manager.current_screen_tag == ScreenTags.SEARCH:
additional_screen_data = {
'query': self._service_locator.search_bar_manager.query
}
self._navigate_to_other_video(
self._service_locator.youtube_connection_manager.get_search_result,
YoutubeResultNav.PREV, additional_screen_data)
def _on_key_nav_next_pressed(self):
if self._service_locator.display_manager.current_screen_tag == ScreenTags.HOME:
self._navigate_to_other_video(
self._service_locator.youtube_connection_manager.get_home_result, YoutubeResultNav.NEXT)
if self._service_locator.display_manager.current_screen_tag == ScreenTags.CREATOR:
additional_screen_data = {
'is_subscribed': self._current_creator_subscription_status
}
self._navigate_to_other_video(self._service_locator.youtube_connection_manager.get_creator_page_result,
YoutubeResultNav.NEXT, additional_screen_data)
if self._service_locator.display_manager.current_screen_tag == ScreenTags.SEARCH:
additional_screen_data = {
'query': self._service_locator.search_bar_manager.query
}
self._navigate_to_other_video(
self._service_locator.youtube_connection_manager.get_search_result,
YoutubeResultNav.NEXT, additional_screen_data)
def _on_key_nav_to_creator_pressed(self):
if self._service_locator.display_manager.current_screen_tag == ScreenTags.VIDEO:
self._current_creator_id = self._current_video_creator_id
screen_data = self._service_locator.youtube_connection_manager.get_creator_page_result(
YoutubeResultNav.FIRST, self._current_creator_id)
self._current_creator_subscription_status = (
self._service_locator.youtube_connection_manager.is_subscribed_to_channel(
self._current_creator_id))
screen_data['is_subscribed'] = self._current_creator_subscription_status
self._service_locator.display_manager.set_screen_visibility(ScreenTags.CREATOR, True)
self._service_locator.display_manager.set_active_screen(ScreenTags.CREATOR)
self._display_current_screen(screen_data)
def _on_key_like_pressed(self):
if self._service_locator.display_manager.current_screen_tag == ScreenTags.VIDEO:
if self._current_video_rating == 'like':
rating = 'none'
else:
rating = 'like'
self._service_locator.youtube_connection_manager.change_rating_status(
self._current_video_id, rating)
self._current_video_rating = rating
self._service_locator.video_rendering_manager.update_video_rating(rating)
def _on_key_dislike_pressed(self):
if self._service_locator.display_manager.current_screen_tag == ScreenTags.VIDEO:
if self._current_video_rating == 'dislike':
rating = 'none'
else:
rating = 'dislike'
self._service_locator.youtube_connection_manager.change_rating_status(
self._current_video_id, rating)
self._current_video_rating = rating
self._service_locator.video_rendering_manager.update_video_rating(rating)
def _on_key_subscribe_pressed(self):
if self._service_locator.display_manager.current_screen_tag == ScreenTags.CREATOR:
self._current_creator_subscription_status = not self._current_creator_subscription_status
self._service_locator.youtube_connection_manager.change_subscription_status(
self._current_creator_id, self._current_creator_subscription_status)
screen_data = self._service_locator.youtube_connection_manager.get_creator_page_result(
YoutubeResultNav.CURRENT)
screen_data['is_subscribed'] = self._current_creator_subscription_status
self._display_current_screen(screen_data)