-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.py
471 lines (399 loc) · 15.1 KB
/
commands.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
#!/usr/bin/env python
import os
import re
import time
import html2text
import webbrowser
import config
import utils
import outputs
import notification
import players
import sources
player_module = getattr(players, config.ext_player)
player_module.compile_command(
flags=config.ext_player_flags,
fullscreen=config.ext_player_fullscreen
)
anime_source_module = getattr(sources, config.anime_source)
def set_quality(quality):
if quality.isnumeric():
config.video_quality = int(quality)
elif quality.isalnum():
if re.match(r"[0-9]+p", quality):
config.video_quality = int(quality[:-1])
else:
outputs.prompt_val("Invalid quality format", quality, "error")
else:
outputs.normal_info(f"Current quality: {quality}")
def toggle_fullscreen(value):
if value.lower() in ["on", "yes", "true"]:
config.ext_player_fullscreen = True
player_module.compile_command(
flags=config.ext_player_flags,
fullscreen=True
)
elif value.lower() in ["off", "no", "false"]:
config.ext_player_fullscreen = False
player_module.compile_command(
flags=config.ext_player_flags,
fullscreen=False
)
else:
outputs.prompt_val("Incorrect Argument", value, "error")
outputs.prompt_val("External Player command",
player_module.get_player_command())
def set_geometry(value):
if re.match(r"^([0-9]+-?)+$", value):
player_module.set_geometry(value)
player_module.compile_command(
flags=config.ext_player_flags,
fullscreen=config.ext_player_fullscreen
)
else:
outputs.prompt_val("Incorrect Argument", value, "error")
outputs.prompt_val("External Player command",
player_module.get_player_command())
def anime_log(args):
log_args = dict()
if len(args) == 0:
pass
elif args[0].isnumeric():
log_args["number"] = int(args[0])
else:
log_args["pattern"] = re.compile(args[0])
if len(args) == 2:
log_args["number"] = int(args[1])
logs = utils.read_log(**log_args)
ongoing = utils.read_log(logfile=config.ongoingfile)
if len(logs) == 0:
if len(args) == 0:
outputs.warning_info("No log entries found.")
else:
outputs.prompt_val("Log entries not found for arguments", args[0],
"error")
return
outputs.bold_info("Watched\t\tAnime Name")
for k, log in logs.items():
outputs.normal_info(utils.Log(log).show(), end=" ")
if k in ongoing:
outputs.warning_tag("TRACKED", end="")
outputs.normal_info()
def check_canon(args):
name, episodes = read_args(args, episodes=False)
eps = utils.canon_episodes(name, episodes)
outputs.normal_info(eps)
def play_anime(args):
name, episodes = read_args(args)
for e in episodes:
url = anime_source_module.get_episode_url(name, e)
stream_from_url(url, name, e)
def play_local_anime(args):
name, episodes = read_args(args)
for e in episodes:
path = utils.get_episode_path(name, e, check=True)
if not path:
outputs.prompt_val("File not found locally", f"{name}:ep-{e}",
"error")
continue
stream_from_url(path, name, e, local=True)
def watch_episode_in_web(args):
name, episodes = read_args(args, episodes=None)
if not episodes:
url = anime_source_module.get_anime_url(name)
webbrowser.open_new_tab(url)
return
for e in episodes:
url = anime_source_module.get_episode_url(name, e)
webbrowser.open_new_tab(url)
choice = input(f"did you watch the episode {e}? <Y/n>")
if not choice.strip() or choice.strip() in 'Yy':
utils.write_log(name, e)
utils.update_tracklist(name, e)
utils.write_cache(name)
utils.update_watchlater(name, e)
def update_log(args):
anime_name, episodes = read_args(args)
episodes = utils.compress_range(episodes)
utils.write_log(anime_name, episodes)
utils.update_tracklist(anime_name, episodes)
utils.write_cache(anime_name)
utils.update_watchlater(anime_name, episodes)
def edit_log(args):
anime_name, episodes = read_args(args)
utils.write_log(anime_name, utils.compress_range(episodes), append=False)
def continue_play(args, play_func=play_anime):
name, _ = read_args(args, episodes=False)
log = utils.Log(utils.read_log().get(name))
watch_later = utils.read_log(name, logfile=config.watchlaterfile)
if watch_later:
episodes = utils.extract_range(utils.Log(watch_later).eps)
else:
_, episodes = read_args(args)
outputs.prompt_val("Watched",
log._eps, "success", end='\t')
outputs.normal_info(log.last_updated_fmt)
if not log.eps:
last = 0
else:
last = int(re.split('-|,', log.eps)[-1])
to_play = utils.compress_range(filter(lambda e: e > last, episodes))
if to_play.strip():
play_func([name, to_play])
else:
unsave_anime(name)
def download_anime(args):
name, episodes = read_args(args)
for e in episodes:
url = anime_source_module.get_episode_url(name, e)
download_from_url(url, name, e)
def check_anime(args):
name, episodes = read_args(args)
unavail_eps = []
for e in episodes:
url = anime_source_module.get_episode_url(name, e)
outputs.normal_info("Testing:", url)
durl, ext = anime_source_module.get_direct_video_url(url)
if not durl:
raise SystemExit("Url for the file not found")
if not os.path.exists(
os.path.join(config.anime_dir, f"./{name}/ep{e:02d}.{ext}")):
unavail_eps.append(e)
if len(unavail_eps) == 0:
outputs.success_info(
"All episodes in given range are locally available")
else:
outputs.prompt_val("Missing episodes",
utils.compress_range(unavail_eps), "warning")
def read_args(args, episodes=True, verbose=True):
if len(args) == 0:
name = utils.read_cache()
elif len(args) == 1 and args[0].isnumeric():
name = utils.read_cache(int(args[0]))
if verbose:
outputs.prompt_val("Name", name)
elif "/" in args[0]:
name = args[0].strip("/").split("/")[-1]
else:
name = anime_source_module.process_anime_name(args[0].strip('"'))
if not anime_source_module.verify_anime_exists(name):
outputs.prompt_val("Anime with the name doesn't exist", args[0],
"error")
raise SystemExit
if not name:
outputs.error_info("Numbers choice invalid, or invalid context.")
raise SystemExit
if len(args) <= 1:
if episodes:
if verbose:
outputs.warning_info("Episodes range not given defaulting to all")
available_rng = anime_source_module.get_episodes_range(
anime_source_module.get_anime_url(name))
if verbose:
outputs.prompt_val("Available episodes", available_rng)
eps = utils.extract_range(available_rng)
else:
eps = None
elif len(args) == 2:
eps = utils.extract_range(args[1])
else:
outputs.error_info("Too many arguments.\n")
outputs.normal_info(__doc__)
raise SystemExit
return name, eps
def list_episodes(args):
name, _ = read_args(args, episodes=False)
available_rng = anime_source_module.get_episodes_range(anime_source_module.get_anime_url(name))
if len(args) == 2:
_, episodes = read_args(args)
eps = set(episodes)
avl_eps = set(utils.extract_range(available_rng))
res = eps.intersection(avl_eps)
available_rng = utils.compress_range(res)
outputs.prompt_val("Available episodes", available_rng)
log = utils.Log(utils.read_log(name))
outputs.prompt_val("Watched episodes", log.eps, "success", end=' ')
outputs.normal_info(log.last_updated_fmt)
utils.write_cache(name)
def list_local_episodes(args):
in_dict = utils.get_local_episodes(*args)
out_dict = dict()
for anime, eps in in_dict.items():
new_eps = utils.compress_range(utils.extract_range(eps))
if new_eps != "":
out_dict[anime] = new_eps
empties = set(in_dict).difference(set(out_dict))
if len(out_dict) == 0:
outputs.warning_info("No local entries found.")
else:
outputs.bold_info("Episodes\tAnime Name")
for k, v in out_dict.items():
outputs.normal_info(f"{v}\t\t{k}")
if len(empties) > 0:
outputs.warning_info("Directories without Episodes:")
outputs.warning_info(", ".join(empties))
def search_anime(args):
name = " ".join(args)
anime_source_module.search_anime(name)
def latest():
utils.display_anime_eps_list(anime_source_module.home_page())
def new():
utils.display_anime_eps_list(anime_source_module.new_page())
def import_from_mal(username):
# TODO : use MAL username to import his completed and other lists.
pass
def anime_info(args):
name, _ = read_args(args, episodes=False)
soup = utils.get_soup(anime_source_module.get_anime_url(name))
info = soup.find("div", {"class": "anime_info_body"})
h = html2text.HTML2Text()
h.ignore_links = True
for t in info.find_all("p", {"class": "type"}):
outputs.normal_info(h.handle(t.decode_contents()), end="")
def download_from_url(url, anime_name=None, episode=None):
if not anime_name or not episode:
anime_name, episode = anime_source_module.parse_url(url)
os.makedirs(os.path.join(config.anime_dir, f"./{anime_name}"),
exist_ok=True)
outputs.prompt_val("Downloading", url)
durl, ext = anime_source_module.get_direct_video_url(url)
if not durl:
outputs.error_info("Url for the file not found")
raise SystemExit
if ext == ".m3u8":
utils.download_m3u8(
durl, utils.get_episode_path(anime_name, episode, make=True))
else:
utils.download_file(
durl,
utils.get_episode_path(anime_name, episode, ext=ext, make=True))
def stream_from_url(url, anime_name=None, episode=None, *, local=False):
if not anime_name or not episode:
anime_name, episode = anime_source_module.parse_url(url)
if local:
durl = url
_, ext = os.path.splitext(durl)
else:
outputs.normal_info("Getting Streaming Link:", url)
durl, ext = anime_source_module.get_direct_video_url(url)
if not durl:
outputs.error_info("Url for the file not found")
raise SystemExit
outputs.prompt_val("Stream link", durl, "success")
try:
if config.ext_player_confirm:
choice = input(f"Open {config.ext_player} Player? <Y/n>")
if choice == "" or choice.lower() == "y":
pass
else:
raise SystemExit
else:
for i in range(11):
outputs.normal_info(
f"\rOpening External Player in: {1-i/10:1.1f} sec.",
end="")
time.sleep(0.1)
outputs.normal_info()
retval = player_module.play_media(
durl, title=f"Ganime:{anime_name}:ep-{episode}{ext}")
if retval:
utils.write_log(anime_name, episode)
utils.update_tracklist(anime_name, episode)
utils.write_cache(anime_name)
utils.update_watchlater(anime_name, episode)
return
except KeyboardInterrupt:
outputs.warning_info("\nInturrupted, Exiting...")
raise SystemExit
outputs.normal_info()
def save_anime(args):
"""Put the anime into watch later list."""
anime_name, eps = read_args(args)
watched = utils.read_log(anime_name)
if watched:
watched_eps = utils.extract_range(utils.Log(watched).eps)
else:
watched_eps = []
save_eps = set(eps).difference(set(watched_eps))
if not save_eps:
outputs.warning_info('Already watched the provided episodes.')
return
utils.write_log(anime_name,
utils.compress_range(save_eps),
append=True,
logfile=config.watchlaterfile)
def track_anime(args):
"""Put an anime into the track list"""
anime_name, episodes = read_args(args, episodes=False)
log = utils.read_log(anime_name)
if log is None:
outputs.warning_info(
"Log entry not found.")
if not episodes:
_, episodes = read_args(args)
episodes = utils.compress_range(episodes)
else:
episodes = utils.Log(log).eps
outputs.prompt_val("Watched", episodes, "success")
utils.write_log(anime_name,
episodes,
append=False,
logfile=config.ongoingfile)
def untrack_anime(anime_name):
"""Remove an anime from the track list"""
utils.remove_anime_from_log(anime_name, logfile=config.ongoingfile)
def unsave_anime(anime_name):
"""Remove an anime from the saved list"""
utils.remove_anime_from_log(anime_name, logfile=config.watchlaterfile)
def unlog_anime(anime_name):
utils.remove_anime_from_log(anime_name)
def list_tracked():
anime_list = utils.read_log(logfile=config.ongoingfile)
for anime, log in anime_list.items():
outputs.normal_info(utils.Log(log).show())
def list_saved_anime():
anime_list = utils.read_log(logfile=config.watchlaterfile)
for anime, log in anime_list.items():
outputs.normal_info(utils.Log(log).show())
def anime_updates(anime_name=""):
"""Check and display the updates on the tracked anime list."""
if anime_name == "":
anime_list = utils.read_log(logfile=config.ongoingfile)
else:
anime_list = {
anime_name:
utils.read_log(anime_name=anime_name, logfile=config.ongoingfile)
}
updates = {}
for anime, log in anime_list.items():
episodes = log.split()[2] # FIX: use log object
new_episodes = anime_source_module.get_episodes_range(
anime_source_module.get_anime_url(anime))
new = set(utils.extract_range(new_episodes)).difference(
set(utils.extract_range(episodes)))
if len(new) > 0:
updates[anime] = new
return updates
def get_updates(anime_name=""):
"""Check and display the updates on the tracked anime list."""
updates = anime_updates(anime_name)
if len(updates) == 0:
outputs.normal_info("No new episodes released.")
return
if anime_name == "":
outputs.prompt_val(
"anime(s) has new episodes.",
len(updates),
"success",
sep=" ",
reverse=True,
)
outputs.normal_info("-" * 50)
for anime, episodes in updates.items():
outputs.normal_info(anime, end="\n\t")
outputs.success_tag(len(episodes), end=" ")
outputs.prompt_val("new episodes", utils.compress_range(episodes),
"success")
def notify_update(anime_name=""):
updates = anime_updates(anime_name)
notification.episodes_update(updates)