jmcomic 0.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- jmcomic/__init__.py +29 -0
- jmcomic/api.py +131 -0
- jmcomic/cl.py +121 -0
- jmcomic/jm_client_impl.py +1217 -0
- jmcomic/jm_client_interface.py +609 -0
- jmcomic/jm_config.py +505 -0
- jmcomic/jm_downloader.py +350 -0
- jmcomic/jm_entity.py +695 -0
- jmcomic/jm_exception.py +191 -0
- jmcomic/jm_option.py +647 -0
- jmcomic/jm_plugin.py +1203 -0
- jmcomic/jm_toolkit.py +937 -0
- jmcomic-0.0.2.dist-info/METADATA +229 -0
- jmcomic-0.0.2.dist-info/RECORD +18 -0
- jmcomic-0.0.2.dist-info/WHEEL +5 -0
- jmcomic-0.0.2.dist-info/entry_points.txt +2 -0
- jmcomic-0.0.2.dist-info/licenses/LICENSE +21 -0
- jmcomic-0.0.2.dist-info/top_level.txt +1 -0
jmcomic/__init__.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# 模块依赖关系如下:
|
|
2
|
+
# 被依赖方 <--- 使用方
|
|
3
|
+
# config <--- entity <--- toolkit <--- client <--- option <--- downloader
|
|
4
|
+
|
|
5
|
+
__version__ = '0.0.2'
|
|
6
|
+
|
|
7
|
+
from .api import *
|
|
8
|
+
from .jm_plugin import *
|
|
9
|
+
|
|
10
|
+
# 下面进行注册组件(客户端、插件)
|
|
11
|
+
gb = dict(filter(lambda pair: isinstance(pair[1], type), globals().items()))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def register_jmcomic_component(variables: Dict[str, Any], method, valid_interface: type):
|
|
15
|
+
for v in variables.values():
|
|
16
|
+
if v != valid_interface and issubclass(v, valid_interface):
|
|
17
|
+
method(v)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
# 注册客户端
|
|
21
|
+
register_jmcomic_component(gb,
|
|
22
|
+
JmModuleConfig.register_client,
|
|
23
|
+
JmcomicClient,
|
|
24
|
+
)
|
|
25
|
+
# 注册插件
|
|
26
|
+
register_jmcomic_component(gb,
|
|
27
|
+
JmModuleConfig.register_plugin,
|
|
28
|
+
JmOptionPlugin,
|
|
29
|
+
)
|
jmcomic/api.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
from .jm_downloader import *
|
|
2
|
+
|
|
3
|
+
__DOWNLOAD_API_RET = Tuple[JmAlbumDetail, JmDownloader]
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def download_batch(download_api,
|
|
7
|
+
jm_id_iter: Union[Iterable, Generator],
|
|
8
|
+
option=None,
|
|
9
|
+
downloader=None,
|
|
10
|
+
) -> Set[__DOWNLOAD_API_RET]:
|
|
11
|
+
"""
|
|
12
|
+
批量下载 album / photo
|
|
13
|
+
|
|
14
|
+
一个album/photo,对应一个线程,对应一个option
|
|
15
|
+
|
|
16
|
+
:param download_api: 下载api
|
|
17
|
+
:param jm_id_iter: jmid (album_id, photo_id) 的迭代器
|
|
18
|
+
:param option: 下载选项,所有的jmid共用一个option
|
|
19
|
+
:param downloader: 下载器类
|
|
20
|
+
"""
|
|
21
|
+
from common import multi_thread_launcher
|
|
22
|
+
|
|
23
|
+
if option is None:
|
|
24
|
+
option = JmModuleConfig.option_class().default()
|
|
25
|
+
|
|
26
|
+
result = set()
|
|
27
|
+
|
|
28
|
+
def callback(*ret):
|
|
29
|
+
result.add(ret)
|
|
30
|
+
|
|
31
|
+
multi_thread_launcher(
|
|
32
|
+
iter_objs=set(
|
|
33
|
+
JmcomicText.parse_to_jm_id(jmid)
|
|
34
|
+
for jmid in jm_id_iter
|
|
35
|
+
),
|
|
36
|
+
apply_each_obj_func=lambda aid: download_api(aid,
|
|
37
|
+
option,
|
|
38
|
+
downloader,
|
|
39
|
+
callback=callback,
|
|
40
|
+
),
|
|
41
|
+
wait_finish=True
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
return result
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def download_album(jm_album_id,
|
|
48
|
+
option=None,
|
|
49
|
+
downloader=None,
|
|
50
|
+
callback=None,
|
|
51
|
+
check_exception=True,
|
|
52
|
+
) -> Union[__DOWNLOAD_API_RET, Set[__DOWNLOAD_API_RET]]:
|
|
53
|
+
"""
|
|
54
|
+
下载一个本子(album),包含其所有的章节(photo)
|
|
55
|
+
|
|
56
|
+
当jm_album_id不是str或int时,视为批量下载,相当于调用 download_batch(download_album, jm_album_id, option, downloader)
|
|
57
|
+
|
|
58
|
+
:param jm_album_id: 本子的禁漫车号
|
|
59
|
+
:param option: 下载选项
|
|
60
|
+
:param downloader: 下载器类
|
|
61
|
+
:param callback: 返回值回调函数,可以拿到 album 和 downloader
|
|
62
|
+
:param check_exception: 是否检查异常, 如果为True,会检查downloader是否有下载异常,并上抛PartialDownloadFailedException
|
|
63
|
+
:return: 对于的本子实体类,下载器(如果是上述的批量情况,返回值为download_batch的返回值)
|
|
64
|
+
"""
|
|
65
|
+
|
|
66
|
+
if not isinstance(jm_album_id, (str, int)):
|
|
67
|
+
return download_batch(download_album, jm_album_id, option, downloader)
|
|
68
|
+
|
|
69
|
+
with new_downloader(option, downloader) as dler:
|
|
70
|
+
album = dler.download_album(jm_album_id)
|
|
71
|
+
|
|
72
|
+
if callback is not None:
|
|
73
|
+
callback(album, dler)
|
|
74
|
+
if check_exception:
|
|
75
|
+
dler.raise_if_has_exception()
|
|
76
|
+
return album, dler
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def download_photo(jm_photo_id,
|
|
80
|
+
option=None,
|
|
81
|
+
downloader=None,
|
|
82
|
+
callback=None,
|
|
83
|
+
check_exception=True,
|
|
84
|
+
):
|
|
85
|
+
"""
|
|
86
|
+
下载一个章节(photo),参数同 download_album
|
|
87
|
+
"""
|
|
88
|
+
if not isinstance(jm_photo_id, (str, int)):
|
|
89
|
+
return download_batch(download_photo, jm_photo_id, option)
|
|
90
|
+
|
|
91
|
+
with new_downloader(option, downloader) as dler:
|
|
92
|
+
photo = dler.download_photo(jm_photo_id)
|
|
93
|
+
|
|
94
|
+
if callback is not None:
|
|
95
|
+
callback(photo, dler)
|
|
96
|
+
if check_exception:
|
|
97
|
+
dler.raise_if_has_exception()
|
|
98
|
+
return photo, dler
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def new_downloader(option=None, downloader=None) -> JmDownloader:
|
|
102
|
+
if option is None:
|
|
103
|
+
option = JmModuleConfig.option_class().default()
|
|
104
|
+
|
|
105
|
+
if downloader is None:
|
|
106
|
+
downloader = JmModuleConfig.downloader_class()
|
|
107
|
+
|
|
108
|
+
return downloader(option)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def create_option_by_file(filepath):
|
|
112
|
+
return JmModuleConfig.option_class().from_file(filepath)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def create_option_by_env(env_name='JM_OPTION_PATH'):
|
|
116
|
+
from .cl import get_env
|
|
117
|
+
|
|
118
|
+
filepath = get_env(env_name, None)
|
|
119
|
+
ExceptionTool.require_true(filepath is not None,
|
|
120
|
+
f'未配置环境变量: {env_name},请配置为option的文件路径')
|
|
121
|
+
return create_option_by_file(filepath)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def create_option_by_str(text: str, mode=None):
|
|
125
|
+
if mode is None:
|
|
126
|
+
mode = PackerUtil.mode_yml
|
|
127
|
+
data = PackerUtil.unpack_by_str(text, mode)[0]
|
|
128
|
+
return JmModuleConfig.option_class().construct(data)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
create_option = create_option_by_file
|
jmcomic/cl.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
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, Optional
|
|
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: Optional[str] = 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
|
+
type=str,
|
|
46
|
+
default=get_env('JM_OPTION_PATH', ''),
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
args = parser.parse_args()
|
|
50
|
+
option = args.option
|
|
51
|
+
if len(option) == 0 or option == "''":
|
|
52
|
+
self.option_path = None
|
|
53
|
+
else:
|
|
54
|
+
self.option_path = os.path.abspath(option)
|
|
55
|
+
|
|
56
|
+
self.raw_id_list = args.id_list
|
|
57
|
+
self.parse_raw_id()
|
|
58
|
+
|
|
59
|
+
def parse_raw_id(self):
|
|
60
|
+
|
|
61
|
+
def parse(text):
|
|
62
|
+
from .jm_toolkit import JmcomicText
|
|
63
|
+
|
|
64
|
+
try:
|
|
65
|
+
return JmcomicText.parse_to_jm_id(text)
|
|
66
|
+
except Exception as e:
|
|
67
|
+
print(e.args[0])
|
|
68
|
+
exit(1)
|
|
69
|
+
|
|
70
|
+
for raw_id in self.raw_id_list:
|
|
71
|
+
if raw_id.startswith('p'):
|
|
72
|
+
self.photo_id_list.append(parse(raw_id[1:]))
|
|
73
|
+
elif raw_id.startswith('a'):
|
|
74
|
+
self.album_id_list.append(parse(raw_id[1:]))
|
|
75
|
+
else:
|
|
76
|
+
self.album_id_list.append(parse(raw_id))
|
|
77
|
+
|
|
78
|
+
def main(self):
|
|
79
|
+
self.parse_arg()
|
|
80
|
+
from .api import jm_log
|
|
81
|
+
jm_log('command_line',
|
|
82
|
+
f'start downloading...\n'
|
|
83
|
+
f'- using option: [{self.option_path or "default"}]\n'
|
|
84
|
+
f'to be downloaded: \n'
|
|
85
|
+
f'- album: {self.album_id_list}\n'
|
|
86
|
+
f'- photo: {self.photo_id_list}')
|
|
87
|
+
|
|
88
|
+
from .api import create_option, JmOption
|
|
89
|
+
if self.option_path is not None:
|
|
90
|
+
option = create_option(self.option_path)
|
|
91
|
+
else:
|
|
92
|
+
option = JmOption.default()
|
|
93
|
+
|
|
94
|
+
self.run(option)
|
|
95
|
+
|
|
96
|
+
def run(self, option):
|
|
97
|
+
from .api import download_album, download_photo
|
|
98
|
+
from common import MultiTaskLauncher
|
|
99
|
+
|
|
100
|
+
if len(self.album_id_list) == 0:
|
|
101
|
+
download_photo(self.photo_id_list, option)
|
|
102
|
+
elif len(self.photo_id_list) == 0:
|
|
103
|
+
download_album(self.album_id_list, option)
|
|
104
|
+
else:
|
|
105
|
+
# 同时下载album和photo
|
|
106
|
+
launcher = MultiTaskLauncher()
|
|
107
|
+
|
|
108
|
+
launcher.create_task(
|
|
109
|
+
target=download_album,
|
|
110
|
+
args=(self.album_id_list, option)
|
|
111
|
+
)
|
|
112
|
+
launcher.create_task(
|
|
113
|
+
target=download_photo,
|
|
114
|
+
args=(self.photo_id_list, option)
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
launcher.wait_finish()
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def main():
|
|
121
|
+
JmcomicUI().main()
|