jmcomic 2.7.1__tar.gz → 2.7.3__tar.gz
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-2.7.1/src/jmcomic.egg-info → jmcomic-2.7.3}/PKG-INFO +26 -5
- {jmcomic-2.7.1 → jmcomic-2.7.3}/README.md +25 -4
- {jmcomic-2.7.1 → jmcomic-2.7.3}/src/jmcomic/__init__.py +2 -3
- {jmcomic-2.7.1 → jmcomic-2.7.3}/src/jmcomic/api.py +75 -51
- {jmcomic-2.7.1 → jmcomic-2.7.3}/src/jmcomic/cl.py +4 -2
- {jmcomic-2.7.1 → jmcomic-2.7.3}/src/jmcomic/jm_async_client.py +88 -32
- {jmcomic-2.7.1 → jmcomic-2.7.3}/src/jmcomic/jm_async_downloader.py +29 -18
- {jmcomic-2.7.1 → jmcomic-2.7.3}/src/jmcomic/jm_client_impl.py +59 -1
- {jmcomic-2.7.1 → jmcomic-2.7.3}/src/jmcomic/jm_client_interface.py +171 -0
- {jmcomic-2.7.1 → jmcomic-2.7.3}/src/jmcomic/jm_config.py +65 -38
- {jmcomic-2.7.1 → jmcomic-2.7.3}/src/jmcomic/jm_downloader.py +3 -0
- {jmcomic-2.7.1 → jmcomic-2.7.3}/src/jmcomic/jm_entity.py +96 -0
- {jmcomic-2.7.1 → jmcomic-2.7.3}/src/jmcomic/jm_feature.py +6 -0
- {jmcomic-2.7.1 → jmcomic-2.7.3}/src/jmcomic/jm_option.py +13 -2
- {jmcomic-2.7.1 → jmcomic-2.7.3}/src/jmcomic/jm_plugin.py +28 -9
- jmcomic-2.7.3/src/jmcomic/jm_task_context.py +59 -0
- {jmcomic-2.7.1 → jmcomic-2.7.3}/src/jmcomic/jm_toolkit.py +191 -0
- {jmcomic-2.7.1 → jmcomic-2.7.3/src/jmcomic.egg-info}/PKG-INFO +26 -5
- {jmcomic-2.7.1 → jmcomic-2.7.3}/src/jmcomic.egg-info/SOURCES.txt +1 -0
- {jmcomic-2.7.1 → jmcomic-2.7.3}/LICENSE +0 -0
- {jmcomic-2.7.1 → jmcomic-2.7.3}/pyproject.toml +0 -0
- {jmcomic-2.7.1 → jmcomic-2.7.3}/setup.cfg +0 -0
- {jmcomic-2.7.1 → jmcomic-2.7.3}/setup.py +0 -0
- {jmcomic-2.7.1 → jmcomic-2.7.3}/src/jmcomic/cli.py +0 -0
- {jmcomic-2.7.1 → jmcomic-2.7.3}/src/jmcomic/jm_exception.py +0 -0
- {jmcomic-2.7.1 → jmcomic-2.7.3}/src/jmcomic.egg-info/dependency_links.txt +0 -0
- {jmcomic-2.7.1 → jmcomic-2.7.3}/src/jmcomic.egg-info/entry_points.txt +0 -0
- {jmcomic-2.7.1 → jmcomic-2.7.3}/src/jmcomic.egg-info/requires.txt +0 -0
- {jmcomic-2.7.1 → jmcomic-2.7.3}/src/jmcomic.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: jmcomic
|
|
3
|
-
Version: 2.7.
|
|
3
|
+
Version: 2.7.3
|
|
4
4
|
Summary: Python API For JMComic (禁漫天堂)
|
|
5
5
|
Home-page: https://github.com/hect0x7/JMComic-Crawler-Python
|
|
6
6
|
Author: hect0x7
|
|
@@ -76,6 +76,7 @@ Dynamic: requires-python
|
|
|
76
76
|
[](https://github.com/hect0x7/JMComic-Crawler-Python/releases/latest)
|
|
77
77
|
[](https://pepy.tech/projects/jmcomic)
|
|
78
78
|
[](https://github.com/hect0x7/JMComic-Crawler-Python)
|
|
79
|
+
[](https://github.com/hect0x7/JMComic-Crawler-Python/network/dependents)
|
|
79
80
|
|
|
80
81
|
</div>
|
|
81
82
|
|
|
@@ -109,9 +110,10 @@ Dynamic: requires-python
|
|
|
109
110
|
|
|
110
111
|
- 登录
|
|
111
112
|
- 搜索本子(支持所有搜索项)
|
|
112
|
-
-
|
|
113
|
+
- 获取本子评论(包括回评、剧透标识)
|
|
113
114
|
- 分类/排行榜
|
|
114
|
-
-
|
|
115
|
+
- 获取本子/章节详情
|
|
116
|
+
- 图片下载解码
|
|
115
117
|
- 个人收藏夹
|
|
116
118
|
- 接口加解密(APP的接口)
|
|
117
119
|
|
|
@@ -277,15 +279,16 @@ jmv 350234 -y
|
|
|
277
279
|
|
|
278
280
|
- **绕过Cloudflare的反爬虫**
|
|
279
281
|
- **实现禁漫APP接口最新的加解密算法 (1.6.3)**
|
|
282
|
+
- 支持**Async**和**Sync**两套 API
|
|
280
283
|
- 用法多样:
|
|
281
284
|
|
|
282
285
|
- GitHub
|
|
283
286
|
Actions:网页上直接输入本子id就能下载([教程:使用GitHub Actions下载禁漫本子](./assets/docs/sources/tutorial/1_github_actions.md))
|
|
284
287
|
- 命令行:无需写Python代码,简单易用([教程:使用命令行下载禁漫本子](./assets/docs/sources/tutorial/2_command_line.md))
|
|
285
288
|
- Python代码:最本质、最强大的使用方式,需要你有一定的python编程基础
|
|
286
|
-
- **支持 Async 和 Sync 两套 API**
|
|
287
289
|
- 支持**网页端**和**移动端**两种客户端实现,可通过配置切换(**移动端不限ip兼容性好,网页端限制ip地区但效率高**)
|
|
288
290
|
- 支持**自动重试和域名切换**机制
|
|
291
|
+
- 支持按下载任务维度结构化收集日志
|
|
289
292
|
- **可配置性强**
|
|
290
293
|
|
|
291
294
|
- 不配置也能使用,十分方便
|
|
@@ -294,7 +297,7 @@ jmv 350234 -y
|
|
|
294
297
|
等
|
|
295
298
|
- **可扩展性强**
|
|
296
299
|
|
|
297
|
-
-
|
|
300
|
+
- 支持自定义本子/章节/图片下载前后事件的回调函数
|
|
298
301
|
- 支持自定义类:`Downloader(负责调度)` `Option(负责配置)` `Client(负责请求)` `实体类`等
|
|
299
302
|
- 支持自定义日志、异常监听器
|
|
300
303
|
- **支持Plugin插件,可以方便地扩展功能,以及使用别人的插件,目前核心内置插件有**:
|
|
@@ -333,3 +336,21 @@ jmv 350234 -y
|
|
|
333
336
|
<img alt="Repo Card" src="https://github-readme-stats.vercel.app/api/pin/?username=tonquer&repo=JMComic-qt" />
|
|
334
337
|
</picture>
|
|
335
338
|
</a>
|
|
339
|
+
|
|
340
|
+
## used by 项目
|
|
341
|
+
|
|
342
|
+
<a href="https://github.com/hect0x7/JMComic-Crawler-Python/network/dependents">
|
|
343
|
+
<picture>
|
|
344
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/hect0x7/hect0x7/used-by/showcase/zh-CN-dark.svg" />
|
|
345
|
+
<source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/hect0x7/hect0x7/used-by/showcase/zh-CN-light.svg" />
|
|
346
|
+
<img width="100%" alt="使用 jmcomic 的项目" src="https://raw.githubusercontent.com/hect0x7/hect0x7/used-by/showcase/zh-CN-light.svg" />
|
|
347
|
+
</picture>
|
|
348
|
+
</a>
|
|
349
|
+
|
|
350
|
+
## Star History
|
|
351
|
+
|
|
352
|
+
<picture>
|
|
353
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/hect0x7/hect0x7/output/profile/star-history-dark.svg" />
|
|
354
|
+
<source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/hect0x7/hect0x7/output/profile/star-history.svg" />
|
|
355
|
+
<img width="100%" alt="jmcomic 生态 Star History" src="https://raw.githubusercontent.com/hect0x7/hect0x7/output/profile/star-history.svg" />
|
|
356
|
+
</picture>
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
[](https://github.com/hect0x7/JMComic-Crawler-Python/releases/latest)
|
|
20
20
|
[](https://pepy.tech/projects/jmcomic)
|
|
21
21
|
[](https://github.com/hect0x7/JMComic-Crawler-Python)
|
|
22
|
+
[](https://github.com/hect0x7/JMComic-Crawler-Python/network/dependents)
|
|
22
23
|
|
|
23
24
|
</div>
|
|
24
25
|
|
|
@@ -52,9 +53,10 @@
|
|
|
52
53
|
|
|
53
54
|
- 登录
|
|
54
55
|
- 搜索本子(支持所有搜索项)
|
|
55
|
-
-
|
|
56
|
+
- 获取本子评论(包括回评、剧透标识)
|
|
56
57
|
- 分类/排行榜
|
|
57
|
-
-
|
|
58
|
+
- 获取本子/章节详情
|
|
59
|
+
- 图片下载解码
|
|
58
60
|
- 个人收藏夹
|
|
59
61
|
- 接口加解密(APP的接口)
|
|
60
62
|
|
|
@@ -220,15 +222,16 @@ jmv 350234 -y
|
|
|
220
222
|
|
|
221
223
|
- **绕过Cloudflare的反爬虫**
|
|
222
224
|
- **实现禁漫APP接口最新的加解密算法 (1.6.3)**
|
|
225
|
+
- 支持**Async**和**Sync**两套 API
|
|
223
226
|
- 用法多样:
|
|
224
227
|
|
|
225
228
|
- GitHub
|
|
226
229
|
Actions:网页上直接输入本子id就能下载([教程:使用GitHub Actions下载禁漫本子](./assets/docs/sources/tutorial/1_github_actions.md))
|
|
227
230
|
- 命令行:无需写Python代码,简单易用([教程:使用命令行下载禁漫本子](./assets/docs/sources/tutorial/2_command_line.md))
|
|
228
231
|
- Python代码:最本质、最强大的使用方式,需要你有一定的python编程基础
|
|
229
|
-
- **支持 Async 和 Sync 两套 API**
|
|
230
232
|
- 支持**网页端**和**移动端**两种客户端实现,可通过配置切换(**移动端不限ip兼容性好,网页端限制ip地区但效率高**)
|
|
231
233
|
- 支持**自动重试和域名切换**机制
|
|
234
|
+
- 支持按下载任务维度结构化收集日志
|
|
232
235
|
- **可配置性强**
|
|
233
236
|
|
|
234
237
|
- 不配置也能使用,十分方便
|
|
@@ -237,7 +240,7 @@ jmv 350234 -y
|
|
|
237
240
|
等
|
|
238
241
|
- **可扩展性强**
|
|
239
242
|
|
|
240
|
-
-
|
|
243
|
+
- 支持自定义本子/章节/图片下载前后事件的回调函数
|
|
241
244
|
- 支持自定义类:`Downloader(负责调度)` `Option(负责配置)` `Client(负责请求)` `实体类`等
|
|
242
245
|
- 支持自定义日志、异常监听器
|
|
243
246
|
- **支持Plugin插件,可以方便地扩展功能,以及使用别人的插件,目前核心内置插件有**:
|
|
@@ -276,3 +279,21 @@ jmv 350234 -y
|
|
|
276
279
|
<img alt="Repo Card" src="https://github-readme-stats.vercel.app/api/pin/?username=tonquer&repo=JMComic-qt" />
|
|
277
280
|
</picture>
|
|
278
281
|
</a>
|
|
282
|
+
|
|
283
|
+
## used by 项目
|
|
284
|
+
|
|
285
|
+
<a href="https://github.com/hect0x7/JMComic-Crawler-Python/network/dependents">
|
|
286
|
+
<picture>
|
|
287
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/hect0x7/hect0x7/used-by/showcase/zh-CN-dark.svg" />
|
|
288
|
+
<source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/hect0x7/hect0x7/used-by/showcase/zh-CN-light.svg" />
|
|
289
|
+
<img width="100%" alt="使用 jmcomic 的项目" src="https://raw.githubusercontent.com/hect0x7/hect0x7/used-by/showcase/zh-CN-light.svg" />
|
|
290
|
+
</picture>
|
|
291
|
+
</a>
|
|
292
|
+
|
|
293
|
+
## Star History
|
|
294
|
+
|
|
295
|
+
<picture>
|
|
296
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/hect0x7/hect0x7/output/profile/star-history-dark.svg" />
|
|
297
|
+
<source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/hect0x7/hect0x7/output/profile/star-history.svg" />
|
|
298
|
+
<img width="100%" alt="jmcomic 生态 Star History" src="https://raw.githubusercontent.com/hect0x7/hect0x7/output/profile/star-history.svg" />
|
|
299
|
+
</picture>
|
|
@@ -2,15 +2,14 @@
|
|
|
2
2
|
# 被依赖方 <--- 使用方
|
|
3
3
|
# config <--- entity <--- toolkit <--- client <--- option <--- downloader
|
|
4
4
|
|
|
5
|
-
__version__ = '2.7.
|
|
5
|
+
__version__ = '2.7.3'
|
|
6
6
|
|
|
7
|
+
from .jm_task_context import *
|
|
7
8
|
from .api import *
|
|
8
9
|
from .jm_plugin import *
|
|
9
10
|
from .jm_feature import *
|
|
10
11
|
from .jm_async_client import AsyncJmApiClient
|
|
11
12
|
from .jm_async_downloader import JmAsyncDownloader
|
|
12
|
-
from .jm_client_interface import AsyncJmcomicClient
|
|
13
|
-
from .api import download_album_async, download_photo_async, download_batch_async
|
|
14
13
|
|
|
15
14
|
# 下面进行注册组件(客户端、插件)
|
|
16
15
|
gb = dict(filter(lambda pair: isinstance(pair[1], type), globals().items()))
|
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
import asyncio
|
|
2
2
|
|
|
3
3
|
from .jm_downloader import *
|
|
4
|
+
from .jm_task_context import bind_jm_task_context, jm_task_context
|
|
4
5
|
|
|
5
6
|
__DOWNLOAD_API_RET = DownloadResult
|
|
6
7
|
|
|
7
8
|
|
|
9
|
+
def _download_type(download_api) -> str:
|
|
10
|
+
name = getattr(download_api, '__name__', download_api.__class__.__name__)
|
|
11
|
+
if name.endswith('_async'):
|
|
12
|
+
name = name[:-6]
|
|
13
|
+
if name.startswith('download_'):
|
|
14
|
+
name = name[9:]
|
|
15
|
+
return name
|
|
16
|
+
|
|
17
|
+
|
|
8
18
|
def download_batch(download_api,
|
|
9
19
|
jm_id_iter: Union[Iterable, Generator],
|
|
10
20
|
option=None,
|
|
@@ -30,21 +40,24 @@ def download_batch(download_api,
|
|
|
30
40
|
|
|
31
41
|
result = BatchResult()
|
|
32
42
|
|
|
43
|
+
download_type = _download_type(download_api)
|
|
44
|
+
|
|
33
45
|
def _safe_download(aid):
|
|
34
46
|
"""batch 内部的单任务包装:确保异常被收集而非静默丢失"""
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
47
|
+
with jm_task_context(download_type=download_type, jm_id=str(aid)):
|
|
48
|
+
try:
|
|
49
|
+
ret = download_api(aid, option, downloader, **kwargs)
|
|
50
|
+
result.add(ret)
|
|
51
|
+
except Exception as e:
|
|
52
|
+
jm_log('batch.failed', f'批量下载失败: [{aid}], 异常: [{e}]', e)
|
|
53
|
+
result.failed[str(aid)] = e
|
|
41
54
|
|
|
42
55
|
multi_thread_launcher(
|
|
43
56
|
iter_objs=set(
|
|
44
57
|
JmcomicText.parse_to_jm_id(jmid)
|
|
45
58
|
for jmid in jm_id_iter
|
|
46
59
|
),
|
|
47
|
-
apply_each_obj_func=_safe_download,
|
|
60
|
+
apply_each_obj_func=bind_jm_task_context(_safe_download),
|
|
48
61
|
wait_finish=True
|
|
49
62
|
)
|
|
50
63
|
|
|
@@ -54,7 +67,7 @@ def download_batch(download_api,
|
|
|
54
67
|
def download_album(jm_album_id,
|
|
55
68
|
option=None,
|
|
56
69
|
downloader=None,
|
|
57
|
-
|
|
70
|
+
*,
|
|
58
71
|
check_exception=True,
|
|
59
72
|
extra=None,
|
|
60
73
|
) -> Union[__DOWNLOAD_API_RET, Set[__DOWNLOAD_API_RET]]:
|
|
@@ -66,8 +79,9 @@ def download_album(jm_album_id,
|
|
|
66
79
|
:param jm_album_id: 本子的禁漫车号
|
|
67
80
|
:param option: 下载选项
|
|
68
81
|
:param downloader: 下载器类
|
|
69
|
-
:param
|
|
70
|
-
|
|
82
|
+
:param check_exception: 仅当 jm_album_id 是单个 ID 时生效。为 True 时检查 downloader 中的部分下载失败,
|
|
83
|
+
并上抛 PartialDownloadFailedException。多 ID 调用会转交 download_batch,此参数不生效;
|
|
84
|
+
请检查 BatchResult.failed,或自行封装 download_batch 实现所需的批量异常策略。
|
|
71
85
|
:param extra: 下载特性(Feature),下载时动态挂载的附加行为上下文。会自动根据上下文(如 album/photo 来源)自适应参数行为。支持单个 Feature、FeatureChain、或列表
|
|
72
86
|
:return: 对于的本子实体类,下载器(如果是上述的批量情况,返回值为download_batch的返回值)
|
|
73
87
|
"""
|
|
@@ -75,41 +89,42 @@ def download_album(jm_album_id,
|
|
|
75
89
|
if not isinstance(jm_album_id, (str, int)):
|
|
76
90
|
return download_batch(download_album, jm_album_id, option, downloader, extra=extra)
|
|
77
91
|
|
|
78
|
-
with
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
92
|
+
with jm_task_context(download_type='album', jm_id=str(jm_album_id)):
|
|
93
|
+
with new_downloader(option, downloader) as dler:
|
|
94
|
+
# 注册 Feature 及来源,由 downloader 在 after_album 钩子中自动执行
|
|
95
|
+
dler.add_features(extra, 'download_album')
|
|
96
|
+
album = dler.download_album(jm_album_id)
|
|
82
97
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
dler.raise_if_has_exception()
|
|
87
|
-
return DownloadResult(album, dler)
|
|
98
|
+
if check_exception:
|
|
99
|
+
dler.raise_if_has_exception()
|
|
100
|
+
return DownloadResult(album, dler)
|
|
88
101
|
|
|
89
102
|
|
|
90
103
|
def download_photo(jm_photo_id,
|
|
91
104
|
option=None,
|
|
92
105
|
downloader=None,
|
|
93
|
-
|
|
106
|
+
*,
|
|
94
107
|
check_exception=True,
|
|
95
108
|
extra=None,
|
|
96
109
|
):
|
|
97
110
|
"""
|
|
98
|
-
下载一个章节(photo),参数同 download_album
|
|
111
|
+
下载一个章节(photo),参数同 download_album。
|
|
112
|
+
|
|
113
|
+
check_exception 仅当 jm_photo_id 是单个 ID 时生效。多 ID 场景请检查
|
|
114
|
+
BatchResult.failed,或自行封装 download_batch 处理批量异常。
|
|
99
115
|
"""
|
|
100
116
|
if not isinstance(jm_photo_id, (str, int)):
|
|
101
117
|
return download_batch(download_photo, jm_photo_id, option, downloader, extra=extra)
|
|
102
118
|
|
|
103
|
-
with
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
119
|
+
with jm_task_context(download_type='photo', jm_id=str(jm_photo_id)):
|
|
120
|
+
with new_downloader(option, downloader) as dler:
|
|
121
|
+
# 注册 Feature 及来源,由 downloader 在 after_photo 钩子中自动执行
|
|
122
|
+
dler.add_features(extra, 'download_photo')
|
|
123
|
+
photo = dler.download_photo(jm_photo_id)
|
|
107
124
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
dler.raise_if_has_exception()
|
|
112
|
-
return DownloadResult(photo, dler)
|
|
125
|
+
if check_exception:
|
|
126
|
+
dler.raise_if_has_exception()
|
|
127
|
+
return DownloadResult(photo, dler)
|
|
113
128
|
|
|
114
129
|
|
|
115
130
|
def new_downloader(option=None, downloader=None) -> JmDownloader:
|
|
@@ -159,7 +174,7 @@ def new_async_downloader(option=None, downloader=None):
|
|
|
159
174
|
async def download_album_async(jm_album_id,
|
|
160
175
|
option=None,
|
|
161
176
|
downloader=None,
|
|
162
|
-
|
|
177
|
+
*,
|
|
163
178
|
check_exception=True,
|
|
164
179
|
extra=None,
|
|
165
180
|
):
|
|
@@ -167,7 +182,9 @@ async def download_album_async(jm_album_id,
|
|
|
167
182
|
异步下载一个本子(album),包含其所有的章节(photo)。
|
|
168
183
|
|
|
169
184
|
- 支持批量下载(当 jm_album_id 为可迭代对象时)
|
|
170
|
-
- 返回 (album, downloader)
|
|
185
|
+
- 返回 (album, downloader) 元组,其中 downloader 的网络和线程池资源已关闭,仅用于读取下载结果
|
|
186
|
+
- check_exception 仅当 jm_album_id 是单个 ID 时生效。多 ID 场景请检查 BatchResult.failed,
|
|
187
|
+
或自行封装 download_batch_async 处理批量异常
|
|
171
188
|
"""
|
|
172
189
|
if not isinstance(jm_album_id, (str, int)):
|
|
173
190
|
return await download_batch_async(download_album_async,
|
|
@@ -177,27 +194,29 @@ async def download_album_async(jm_album_id,
|
|
|
177
194
|
extra=extra
|
|
178
195
|
)
|
|
179
196
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
197
|
+
with jm_task_context(download_type='album', jm_id=str(jm_album_id)):
|
|
198
|
+
async with new_async_downloader(option, downloader) as dler:
|
|
199
|
+
dler.add_features(extra, 'download_album')
|
|
200
|
+
album = await dler.download_album(jm_album_id)
|
|
183
201
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
if check_exception:
|
|
187
|
-
dler.raise_if_has_exception()
|
|
202
|
+
if check_exception:
|
|
203
|
+
dler.raise_if_has_exception()
|
|
188
204
|
|
|
189
|
-
|
|
205
|
+
return DownloadResult(album, dler)
|
|
190
206
|
|
|
191
207
|
|
|
192
208
|
async def download_photo_async(jm_photo_id,
|
|
193
209
|
option=None,
|
|
194
210
|
downloader=None,
|
|
195
|
-
|
|
211
|
+
*,
|
|
196
212
|
check_exception=True,
|
|
197
213
|
extra=None,
|
|
198
214
|
):
|
|
199
215
|
"""
|
|
200
216
|
异步下载一个章节(photo)。
|
|
217
|
+
返回的 downloader 已关闭网络和线程池资源,仅用于读取下载结果。
|
|
218
|
+
check_exception 仅当 jm_photo_id 是单个 ID 时生效。多 ID 场景请检查
|
|
219
|
+
BatchResult.failed,或自行封装 download_batch_async 处理批量异常。
|
|
201
220
|
"""
|
|
202
221
|
if not isinstance(jm_photo_id, (str, int)):
|
|
203
222
|
return await download_batch_async(download_photo_async,
|
|
@@ -207,16 +226,15 @@ async def download_photo_async(jm_photo_id,
|
|
|
207
226
|
extra=extra
|
|
208
227
|
)
|
|
209
228
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
229
|
+
with jm_task_context(download_type='photo', jm_id=str(jm_photo_id)):
|
|
230
|
+
async with new_async_downloader(option, downloader) as dler:
|
|
231
|
+
dler.add_features(extra, 'download_photo')
|
|
232
|
+
photo = await dler.download_photo(jm_photo_id)
|
|
213
233
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
if check_exception:
|
|
217
|
-
dler.raise_if_has_exception()
|
|
234
|
+
if check_exception:
|
|
235
|
+
dler.raise_if_has_exception()
|
|
218
236
|
|
|
219
|
-
|
|
237
|
+
return DownloadResult(photo, dler)
|
|
220
238
|
|
|
221
239
|
|
|
222
240
|
async def download_batch_async(download_api,
|
|
@@ -234,9 +252,14 @@ async def download_batch_async(download_api,
|
|
|
234
252
|
option = JmModuleConfig.option_class().default()
|
|
235
253
|
|
|
236
254
|
jm_ids = list(dict.fromkeys(JmcomicText.parse_to_jm_id(jmid) for jmid in jm_id_iter))
|
|
255
|
+
download_type = _download_type(download_api)
|
|
256
|
+
|
|
257
|
+
async def _download_one(jmid):
|
|
258
|
+
with jm_task_context(download_type=download_type, jm_id=str(jmid)):
|
|
259
|
+
return await download_api(jmid, option, downloader, **kwargs)
|
|
237
260
|
|
|
238
261
|
results = await asyncio.gather(
|
|
239
|
-
*(
|
|
262
|
+
*(_download_one(jmid) for jmid in jm_ids),
|
|
240
263
|
return_exceptions=True,
|
|
241
264
|
)
|
|
242
265
|
|
|
@@ -244,7 +267,8 @@ async def download_batch_async(download_api,
|
|
|
244
267
|
result = BatchResult()
|
|
245
268
|
for jmid, r in zip(jm_ids, results):
|
|
246
269
|
if isinstance(r, BaseException):
|
|
247
|
-
|
|
270
|
+
with jm_task_context(download_type=download_type, jm_id=str(jmid)):
|
|
271
|
+
jm_log('async.batch.failed', f'批量下载失败: [{jmid}], 异常: [{r}]', r)
|
|
248
272
|
result.failed[str(jmid)] = r
|
|
249
273
|
else:
|
|
250
274
|
result.add(r)
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import warnings
|
|
2
|
+
|
|
2
3
|
from .cli import *
|
|
3
4
|
|
|
5
|
+
|
|
4
6
|
warnings.warn(
|
|
5
7
|
"The 'jmcomic.cl' module is deprecated and renamed to 'jmcomic.cli'. "
|
|
6
|
-
"Please update your imports. It will be removed in
|
|
8
|
+
"Please update your imports. It will be removed in version 2.7.4.",
|
|
7
9
|
DeprecationWarning,
|
|
8
|
-
stacklevel=2
|
|
10
|
+
stacklevel=2,
|
|
9
11
|
)
|
|
@@ -7,6 +7,7 @@ from __future__ import annotations
|
|
|
7
7
|
|
|
8
8
|
import asyncio
|
|
9
9
|
import json
|
|
10
|
+
from typing import Sequence
|
|
10
11
|
from urllib.parse import urlencode
|
|
11
12
|
|
|
12
13
|
from curl_cffi.requests import AsyncSession
|
|
@@ -17,7 +18,7 @@ from .jm_client_interface import (
|
|
|
17
18
|
)
|
|
18
19
|
from .jm_entity import (
|
|
19
20
|
JmAlbumDetail, JmPhotoDetail, JmSearchPage, JmCategoryPage,
|
|
20
|
-
JmFavoritePage, DetailType
|
|
21
|
+
JmFavoritePage, DetailType, JmAlbumCommentPage
|
|
21
22
|
)
|
|
22
23
|
from .jm_config import JmModuleConfig, JmMagicConstants, time_stamp, jm_log
|
|
23
24
|
from .jm_toolkit import (
|
|
@@ -46,17 +47,21 @@ class AsyncJmApiClient(AsyncJmcomicClient):
|
|
|
46
47
|
API_CHAPTER = '/chapter'
|
|
47
48
|
API_SCRAMBLE = '/chapter_view_template'
|
|
48
49
|
API_FAVORITE = '/favorite'
|
|
50
|
+
API_FORUM = '/forum'
|
|
49
51
|
|
|
50
52
|
# 缓存未命中标记
|
|
51
53
|
_SENTINEL = object()
|
|
52
54
|
|
|
53
55
|
# 类级别初始化标记与锁,防止并发更新域名
|
|
54
|
-
|
|
56
|
+
_has_setup_domain = False
|
|
55
57
|
_setup_lock = asyncio.Lock()
|
|
56
58
|
|
|
57
|
-
def __init__(self, option: JmOption, max_clients=None, **kwargs):
|
|
59
|
+
def __init__(self, option: JmOption, max_clients=None, domain_list=None, **kwargs):
|
|
60
|
+
if 'domain_retry_strategy' in kwargs:
|
|
61
|
+
raise TypeError('Async client does not support domain_retry_strategy')
|
|
62
|
+
|
|
58
63
|
self.option = option
|
|
59
|
-
self._domain_list = self._resolve_domain_list()
|
|
64
|
+
self._domain_list = self._resolve_domain_list(domain_list)
|
|
60
65
|
retry_times = option.client.get('retry_times')
|
|
61
66
|
self._retry_times = retry_times if retry_times is not None else 5
|
|
62
67
|
self._timeout = option.client.get('timeout', 30) or 30
|
|
@@ -84,22 +89,41 @@ class AsyncJmApiClient(AsyncJmcomicClient):
|
|
|
84
89
|
# 域名管理
|
|
85
90
|
# ======================================================================
|
|
86
91
|
|
|
87
|
-
|
|
92
|
+
@staticmethod
|
|
93
|
+
def _normalize_domain_list(domain_list) -> list[str]:
|
|
94
|
+
if isinstance(domain_list, str):
|
|
95
|
+
return [domain.strip() for domain in domain_list.splitlines() if domain.strip()]
|
|
96
|
+
|
|
97
|
+
if isinstance(domain_list, Sequence):
|
|
98
|
+
result = []
|
|
99
|
+
for domain in domain_list:
|
|
100
|
+
if not isinstance(domain, str):
|
|
101
|
+
raise TypeError(f'domain must be str, got {type(domain)}')
|
|
102
|
+
domain = domain.strip()
|
|
103
|
+
if domain:
|
|
104
|
+
result.append(domain)
|
|
105
|
+
return result
|
|
106
|
+
|
|
107
|
+
raise TypeError('domain_list must be str, Sequence[str], or None')
|
|
108
|
+
|
|
109
|
+
def _resolve_domain_list(self, domain_list=None) -> list[str]:
|
|
88
110
|
"""解析并返回可用的 API 域名列表"""
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
111
|
+
if domain_list is not None:
|
|
112
|
+
resolved = self._normalize_domain_list(domain_list)
|
|
113
|
+
if resolved:
|
|
114
|
+
return resolved
|
|
115
|
+
|
|
92
116
|
domain = self.option.client.domain
|
|
93
117
|
if hasattr(domain, 'get'):
|
|
94
118
|
domain_list = domain.get('api', [])
|
|
95
|
-
elif isinstance(domain, list):
|
|
96
|
-
domain_list = domain
|
|
97
|
-
elif isinstance(domain, str):
|
|
98
|
-
domain_list = [d.strip() for d in domain.split('\n') if d.strip()]
|
|
99
119
|
else:
|
|
100
|
-
domain_list =
|
|
101
|
-
|
|
102
|
-
|
|
120
|
+
domain_list = domain
|
|
121
|
+
|
|
122
|
+
if domain_list is not None:
|
|
123
|
+
resolved = self._normalize_domain_list(domain_list)
|
|
124
|
+
if resolved:
|
|
125
|
+
return resolved
|
|
126
|
+
|
|
103
127
|
return list(JmModuleConfig.DOMAIN_API_LIST)
|
|
104
128
|
|
|
105
129
|
def get_domain_list(self) -> list[str]:
|
|
@@ -108,6 +132,20 @@ class AsyncJmApiClient(AsyncJmcomicClient):
|
|
|
108
132
|
def set_domain_list(self, domain_list: list[str]):
|
|
109
133
|
self._domain_list = domain_list
|
|
110
134
|
|
|
135
|
+
def update_old_api_domain(self, new_server_list: list[str]):
|
|
136
|
+
if not new_server_list:
|
|
137
|
+
return
|
|
138
|
+
|
|
139
|
+
if sorted(self._domain_list) != sorted(JmModuleConfig.DOMAIN_API_LIST):
|
|
140
|
+
return
|
|
141
|
+
|
|
142
|
+
old_server_list = self._domain_list
|
|
143
|
+
self._domain_list = list(new_server_list)
|
|
144
|
+
jm_log(
|
|
145
|
+
'api.update_domain.replace',
|
|
146
|
+
f'替换异步客户端的内置API域名:(old){old_server_list} ---→ (new){self._domain_list}',
|
|
147
|
+
)
|
|
148
|
+
|
|
111
149
|
# ======================================================================
|
|
112
150
|
# 缓存
|
|
113
151
|
# ======================================================================
|
|
@@ -595,6 +633,24 @@ class AsyncJmApiClient(AsyncJmcomicClient):
|
|
|
595
633
|
)
|
|
596
634
|
return JmPageTool.parse_api_to_favorite_page(resp.model_data)
|
|
597
635
|
|
|
636
|
+
async def album_pagination(self,
|
|
637
|
+
jm_id: str,
|
|
638
|
+
page=1,
|
|
639
|
+
series=1,
|
|
640
|
+
with_ad_wcm=1,
|
|
641
|
+
need_total=True,
|
|
642
|
+
) -> JmAlbumCommentPage:
|
|
643
|
+
"""获取本子评论分页,返回 ``list`` 和 ``total``。"""
|
|
644
|
+
resp = await self.req_api(
|
|
645
|
+
self.API_FORUM,
|
|
646
|
+
params={
|
|
647
|
+
'mode': 'all',
|
|
648
|
+
'page': page,
|
|
649
|
+
'aid': JmcomicText.parse_to_jm_id(jm_id),
|
|
650
|
+
},
|
|
651
|
+
)
|
|
652
|
+
return JmPageTool.parse_api_to_album_comment_page(resp.model_data)
|
|
653
|
+
|
|
598
654
|
async def add_favorite_album(self, album_id, folder_id='0'):
|
|
599
655
|
"""
|
|
600
656
|
将指定图集加入用户的收藏夹。
|
|
@@ -615,7 +671,7 @@ class AsyncJmApiClient(AsyncJmcomicClient):
|
|
|
615
671
|
comment_id=None,
|
|
616
672
|
**kwargs,
|
|
617
673
|
) -> JmAlbumCommentResp:
|
|
618
|
-
"""
|
|
674
|
+
"""提交本子评论内容"""
|
|
619
675
|
# 移动端 API 没有评论接口,此方法仅为接口完整性保留
|
|
620
676
|
raise NotImplementedError('移动端 API 不支持评论功能,请使用网页端 JmHtmlClient')
|
|
621
677
|
|
|
@@ -659,8 +715,7 @@ class AsyncJmApiClient(AsyncJmcomicClient):
|
|
|
659
715
|
return
|
|
660
716
|
|
|
661
717
|
if JmModuleConfig.DOMAIN_API_UPDATED_LIST is not None:
|
|
662
|
-
|
|
663
|
-
self._domain_list = list(JmModuleConfig.DOMAIN_API_UPDATED_LIST)
|
|
718
|
+
self.update_old_api_domain(JmModuleConfig.DOMAIN_API_UPDATED_LIST)
|
|
664
719
|
return
|
|
665
720
|
|
|
666
721
|
# 尝试从域名服务器获取最新域名
|
|
@@ -683,8 +738,7 @@ class AsyncJmApiClient(AsyncJmcomicClient):
|
|
|
683
738
|
jm_log('api.update_domain.success',
|
|
684
739
|
f'获取到最新的API域名: {new_server_list}')
|
|
685
740
|
JmModuleConfig.DOMAIN_API_UPDATED_LIST = new_server_list
|
|
686
|
-
|
|
687
|
-
self._domain_list = new_server_list
|
|
741
|
+
self.update_old_api_domain(new_server_list)
|
|
688
742
|
return
|
|
689
743
|
except Exception as e:
|
|
690
744
|
jm_log('api.update_domain.error', f'通过[{url}]自动更新API域名失败: {e}')
|
|
@@ -708,20 +762,22 @@ class AsyncJmApiClient(AsyncJmcomicClient):
|
|
|
708
762
|
|
|
709
763
|
cls = self.__class__
|
|
710
764
|
async with cls._setup_lock:
|
|
711
|
-
if
|
|
765
|
+
if self._has_setup:
|
|
766
|
+
return
|
|
767
|
+
|
|
768
|
+
if not cls._has_setup_domain:
|
|
712
769
|
await self.auto_update_domain()
|
|
713
|
-
|
|
714
|
-
await self.ensure_have_cookies()
|
|
715
|
-
cls._has_setup_domain_and_cookies = True
|
|
770
|
+
cls._has_setup_domain = True
|
|
716
771
|
else:
|
|
717
|
-
#
|
|
718
|
-
if JmModuleConfig.
|
|
719
|
-
self.
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
772
|
+
# 即使已经初始化过域名,也需要将已保存的全局 DOMAIN 赋值到当前 client
|
|
773
|
+
if JmModuleConfig.FLAG_API_CLIENT_AUTO_UPDATE_DOMAIN:
|
|
774
|
+
self.update_old_api_domain(JmModuleConfig.DOMAIN_API_UPDATED_LIST)
|
|
775
|
+
|
|
776
|
+
# Cookie 属于 session 状态,每个 client 都需要独立确认。
|
|
777
|
+
if JmModuleConfig.FLAG_API_CLIENT_REQUIRE_COOKIES:
|
|
778
|
+
await self.ensure_have_cookies()
|
|
779
|
+
|
|
780
|
+
self._has_setup = True
|
|
725
781
|
|
|
726
782
|
# ======================================================================
|
|
727
783
|
# 生命周期
|