-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v2.2.9: 实现基于命令行的调用方式,更加简单易用,更新文档 (#133)
- Loading branch information
Showing
8 changed files
with
166 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
""" | ||
command-line usage | ||
for example, download album 123 456, photo 333: | ||
$ jmcomic 123 456 p333 --option="D:/option.yml" | ||
""" | ||
import os.path | ||
from typing import List | ||
|
||
|
||
def get_env(name, default): | ||
import os | ||
value = os.getenv(name, None) | ||
if value is None or value == '': | ||
return default | ||
|
||
return value | ||
|
||
|
||
class JmcomicUI: | ||
|
||
def __init__(self) -> None: | ||
self.option_path = None | ||
self.raw_id_list: List[str] = [] | ||
self.album_id_list: List[str] = [] | ||
self.photo_id_list: List[str] = [] | ||
|
||
def parse_arg(self): | ||
import argparse | ||
parser = argparse.ArgumentParser(prog='python -m jmcomic', description='JMComic Command Line Downloader') | ||
parser.add_argument( | ||
'id_list', | ||
nargs='*', | ||
help='input all album/photo ids that you want to download, separating them by spaces. ' | ||
'Need add a "p" prefix to indicate a photo id, such as `123 456 p333`.', | ||
default=[], | ||
) | ||
|
||
parser.add_argument( | ||
'--option', | ||
help='path to the option file, you can also specify it by env `JM_OPTION_PATH`', | ||
default=get_env('JM_OPTION_PATH', './option.yml'), | ||
) | ||
|
||
args = parser.parse_args() | ||
self.option_path = os.path.abspath(args.option) | ||
self.raw_id_list = args.id_list | ||
|
||
self.parse_raw_id() | ||
|
||
def parse_raw_id(self): | ||
|
||
def parse(text): | ||
from .jm_toolkit import JmcomicText | ||
|
||
try: | ||
return JmcomicText.parse_to_album_id(text) | ||
except Exception as e: | ||
print(e.args[0]) | ||
exit(1) | ||
|
||
for raw_id in self.raw_id_list: | ||
if raw_id.startswith('p'): | ||
self.photo_id_list.append(parse(raw_id[1:])) | ||
elif raw_id.startswith('a'): | ||
self.album_id_list.append(parse(raw_id[1:])) | ||
else: | ||
self.album_id_list.append(parse(raw_id)) | ||
|
||
def main(self): | ||
self.parse_arg() | ||
from .api import jm_debug | ||
jm_debug('command_line', | ||
f'start downloading...\n' | ||
f'- using option: [{self.option_path}]\n' | ||
f'to be downloaded: \n' | ||
f'- album: {self.album_id_list}\n' | ||
f'- photo: {self.photo_id_list}') | ||
self.run() | ||
|
||
def run(self): | ||
from .api import download_album, download_photo, create_option | ||
from common import MultiTaskLauncher | ||
|
||
option = create_option(self.option_path) | ||
|
||
if len(self.album_id_list) == 0: | ||
download_photo(self.photo_id_list, option) | ||
elif len(self.photo_id_list) == 0: | ||
download_album(self.album_id_list, option) | ||
else: | ||
# 同时下载album和photo | ||
launcher = MultiTaskLauncher() | ||
|
||
launcher.create_task( | ||
target=download_album, | ||
args=(self.album_id_list, option) | ||
) | ||
launcher.create_task( | ||
target=download_photo, | ||
args=(self.photo_id_list, option) | ||
) | ||
|
||
launcher.wait_finish() | ||
|
||
|
||
def main(): | ||
JmcomicUI().main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
""" | ||
使用命令行下载禁漫本子 | ||
1. 基本用法 | ||
# 下载album 123 456,下载photo 333。彼此之间使用空格间隔 | ||
``` | ||
jmcomic 123 456 p333 | ||
``` | ||
2. 自定义option | ||
2.1. 通过命令行 | ||
``` | ||
jmcomic 123 --option="D:/a.yml" | ||
``` | ||
2.2. 通过环境变量 | ||
设置环境: JM_OPTION_PATH = D:/a.yml | ||
命令行的命令不变 | ||
``` | ||
jmcomic 123 | ||
``` | ||
""" |