|
7 | 7 | import sys
|
8 | 8 | import time
|
9 | 9 | import urllib.parse
|
10 |
| -from typing import List, Optional |
| 10 | +from typing import Any, List, Optional |
11 | 11 |
|
12 | 12 | import peewee
|
13 | 13 |
|
@@ -255,6 +255,7 @@ def rmdir_p(path: pathlib.Path, *, root: pathlib.Path = None) -> None:
|
255 | 255 | def process_entry(
|
256 | 256 | m3u8_url: str,
|
257 | 257 | output: pathlib.Path,
|
| 258 | + *, |
258 | 259 | force: bool = False,
|
259 | 260 | exist_ok: bool = False,
|
260 | 261 | workdir: pathlib.Path = None,
|
@@ -376,6 +377,65 @@ def process_entry(
|
376 | 377 | return 0
|
377 | 378 |
|
378 | 379 |
|
| 380 | +def process_batch( |
| 381 | + manifest: pathlib.Path, |
| 382 | + remove_manifest_on_success: bool = False, |
| 383 | + debug: bool = False, |
| 384 | + **processing_kwargs: Any, |
| 385 | +) -> int: |
| 386 | + target_dir = manifest.parent |
| 387 | + try: |
| 388 | + entries = [] |
| 389 | + with manifest.open(encoding="utf-8") as fp: |
| 390 | + manifest_content = fp.read() |
| 391 | + except OSError: |
| 392 | + logger.critical("cannot open batch mode manifest", exc_info=debug) |
| 393 | + if debug: |
| 394 | + raise |
| 395 | + return 1 |
| 396 | + except UnicodeDecodeError: |
| 397 | + logger.critical( |
| 398 | + "cannot decode batch mode manifest as utf-8; " |
| 399 | + "see https://git.io/caterpillar-encoding", |
| 400 | + exc_info=debug, |
| 401 | + ) |
| 402 | + if debug: |
| 403 | + raise |
| 404 | + return 1 |
| 405 | + |
| 406 | + for line in manifest_content.splitlines(): |
| 407 | + try: |
| 408 | + m3u8_url, filename = line.strip().split("\t") |
| 409 | + output = target_dir.joinpath(filename) |
| 410 | + entries.append((m3u8_url, output)) |
| 411 | + except Exception: |
| 412 | + logger.critical( |
| 413 | + "malformed line in batch mode manifest: %s", line, exc_info=debug |
| 414 | + ) |
| 415 | + if debug: |
| 416 | + raise |
| 417 | + return 1 |
| 418 | + |
| 419 | + retvals = [] |
| 420 | + count = len(entries) |
| 421 | + for i, (m3u8_url, output) in enumerate(entries): |
| 422 | + sys.stderr.write( |
| 423 | + f'[{i + 1}/{count}] Downloading {m3u8_url} into "{output}"...\n' |
| 424 | + ) |
| 425 | + retvals.append(process_entry(m3u8_url, output, **processing_kwargs)) |
| 426 | + sys.stderr.write("\n") |
| 427 | + retval = int(any(retvals)) |
| 428 | + if retval == 0 and remove_manifest_on_success: |
| 429 | + try: |
| 430 | + manifest.unlink() |
| 431 | + except OSError: |
| 432 | + logger.error("cannot remove batch mode manifest") |
| 433 | + if debug: |
| 434 | + raise |
| 435 | + return 1 |
| 436 | + return retval |
| 437 | + |
| 438 | + |
379 | 439 | def main() -> int:
|
380 | 440 | user_config_options = [] if USER_CONFIG_DISABLED else load_user_config()
|
381 | 441 |
|
@@ -545,63 +605,19 @@ def main() -> int:
|
545 | 605 | logger.critical("ffmpeg not found")
|
546 | 606 | return 1
|
547 | 607 |
|
548 |
| - if not args.batch: |
549 |
| - return process_entry(args.m3u8_url, args.output, **kwargs) |
550 |
| - else: |
551 |
| - manifest = pathlib.Path(args.m3u8_url).resolve() |
552 |
| - target_dir = manifest.parent |
553 |
| - try: |
554 |
| - entries = [] |
555 |
| - with manifest.open(encoding="utf-8") as fp: |
556 |
| - manifest_content = fp.read() |
557 |
| - except OSError: |
558 |
| - logger.critical("cannot open batch mode manifest", exc_info=args.debug) |
559 |
| - if args.debug: |
560 |
| - raise |
561 |
| - return 1 |
562 |
| - except UnicodeDecodeError: |
563 |
| - logger.critical( |
564 |
| - "cannot decode batch mode manifest as utf-8; " |
565 |
| - "see https://git.io/caterpillar-encoding", |
566 |
| - exc_info=args.debug, |
567 |
| - ) |
568 |
| - if args.debug: |
569 |
| - raise |
570 |
| - return 1 |
571 |
| - |
572 |
| - for line in manifest_content.splitlines(): |
573 |
| - try: |
574 |
| - m3u8_url, filename = line.strip().split("\t") |
575 |
| - output = target_dir.joinpath(filename) |
576 |
| - entries.append((m3u8_url, output)) |
577 |
| - except Exception: |
578 |
| - logger.critical( |
579 |
| - "malformed line in batch mode manifest: %s", |
580 |
| - line, |
581 |
| - exc_info=args.debug, |
582 |
| - ) |
583 |
| - if args.debug: |
584 |
| - raise |
585 |
| - return 1 |
586 |
| - |
587 |
| - retvals = [] |
588 |
| - count = len(entries) |
589 |
| - for i, (m3u8_url, output) in enumerate(entries): |
590 |
| - sys.stderr.write( |
591 |
| - f'[{i + 1}/{count}] Downloading {m3u8_url} into "{output}"...\n' |
| 608 | + try: |
| 609 | + if not args.batch: |
| 610 | + return process_entry(args.m3u8_url, args.output, **kwargs) |
| 611 | + else: |
| 612 | + manifest = pathlib.Path(args.m3u8_url).resolve() |
| 613 | + return process_batch( |
| 614 | + manifest, |
| 615 | + remove_manifest_on_success=args.remove_manifest_on_success, |
| 616 | + debug=args.debug, |
| 617 | + **kwargs, |
592 | 618 | )
|
593 |
| - retvals.append(process_entry(m3u8_url, output, **kwargs)) |
594 |
| - sys.stderr.write("\n") |
595 |
| - retval = int(any(retvals)) |
596 |
| - if retval == 0 and args.remove_manifest_on_success: |
597 |
| - try: |
598 |
| - manifest.unlink() |
599 |
| - except OSError: |
600 |
| - logger.error("cannot remove batch mode manifest") |
601 |
| - if args.debug: |
602 |
| - raise |
603 |
| - return 1 |
604 |
| - return retval |
| 619 | + except KeyboardInterrupt: |
| 620 | + return 1 |
605 | 621 |
|
606 | 622 |
|
607 | 623 | if __name__ == "__main__":
|
|
0 commit comments