|
| 1 | +""" |
| 2 | +command-line usage |
| 3 | +
|
| 4 | +for example, download album 123 456, photo 333: |
| 5 | +
|
| 6 | +$ jmcomic 123 456 p333 --option="D:/option.yml" |
| 7 | +
|
| 8 | +
|
| 9 | +""" |
| 10 | +import os.path |
| 11 | +from typing import List |
| 12 | + |
| 13 | + |
| 14 | +def get_env(name, default): |
| 15 | + import os |
| 16 | + value = os.getenv(name, None) |
| 17 | + if value is None or value == '': |
| 18 | + return default |
| 19 | + |
| 20 | + return value |
| 21 | + |
| 22 | + |
| 23 | +class JmcomicUI: |
| 24 | + |
| 25 | + def __init__(self) -> None: |
| 26 | + self.option_path = None |
| 27 | + self.raw_id_list: List[str] = [] |
| 28 | + self.album_id_list: List[str] = [] |
| 29 | + self.photo_id_list: List[str] = [] |
| 30 | + |
| 31 | + def parse_arg(self): |
| 32 | + import argparse |
| 33 | + parser = argparse.ArgumentParser(prog='python -m jmcomic', description='JMComic Command Line Downloader') |
| 34 | + parser.add_argument( |
| 35 | + 'id_list', |
| 36 | + nargs='*', |
| 37 | + help='input all album/photo ids that you want to download, separating them by spaces. ' |
| 38 | + 'Need add a "p" prefix to indicate a photo id, such as `123 456 p333`.', |
| 39 | + default=[], |
| 40 | + ) |
| 41 | + |
| 42 | + parser.add_argument( |
| 43 | + '--option', |
| 44 | + help='path to the option file, you can also specify it by env `JM_OPTION_PATH`', |
| 45 | + default=get_env('JM_OPTION_PATH', './option.yml'), |
| 46 | + ) |
| 47 | + |
| 48 | + args = parser.parse_args() |
| 49 | + self.option_path = os.path.abspath(args.option) |
| 50 | + self.raw_id_list = args.id_list |
| 51 | + |
| 52 | + self.parse_raw_id() |
| 53 | + |
| 54 | + def parse_raw_id(self): |
| 55 | + |
| 56 | + def parse(text): |
| 57 | + from .jm_toolkit import JmcomicText |
| 58 | + |
| 59 | + try: |
| 60 | + return JmcomicText.parse_to_album_id(text) |
| 61 | + except Exception as e: |
| 62 | + print(e.args[0]) |
| 63 | + exit(1) |
| 64 | + |
| 65 | + for raw_id in self.raw_id_list: |
| 66 | + if raw_id.startswith('p'): |
| 67 | + self.photo_id_list.append(parse(raw_id[1:])) |
| 68 | + elif raw_id.startswith('a'): |
| 69 | + self.album_id_list.append(parse(raw_id[1:])) |
| 70 | + else: |
| 71 | + self.album_id_list.append(parse(raw_id)) |
| 72 | + |
| 73 | + def main(self): |
| 74 | + self.parse_arg() |
| 75 | + from .api import jm_debug |
| 76 | + jm_debug('command_line', |
| 77 | + f'start downloading...\n' |
| 78 | + f'- using option: [{self.option_path}]\n' |
| 79 | + f'to be downloaded: \n' |
| 80 | + f'- album: {self.album_id_list}\n' |
| 81 | + f'- photo: {self.photo_id_list}') |
| 82 | + self.run() |
| 83 | + |
| 84 | + def run(self): |
| 85 | + from .api import download_album, download_photo, create_option |
| 86 | + from common import MultiTaskLauncher |
| 87 | + |
| 88 | + option = create_option(self.option_path) |
| 89 | + |
| 90 | + if len(self.album_id_list) == 0: |
| 91 | + download_photo(self.photo_id_list, option) |
| 92 | + elif len(self.photo_id_list) == 0: |
| 93 | + download_album(self.album_id_list, option) |
| 94 | + else: |
| 95 | + # 同时下载album和photo |
| 96 | + launcher = MultiTaskLauncher() |
| 97 | + |
| 98 | + launcher.create_task( |
| 99 | + target=download_album, |
| 100 | + args=(self.album_id_list, option) |
| 101 | + ) |
| 102 | + launcher.create_task( |
| 103 | + target=download_photo, |
| 104 | + args=(self.photo_id_list, option) |
| 105 | + ) |
| 106 | + |
| 107 | + launcher.wait_finish() |
| 108 | + |
| 109 | + |
| 110 | +def main(): |
| 111 | + JmcomicUI().main() |
0 commit comments