jmcomic 2.7.2__tar.gz → 2.7.4__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.
Files changed (29) hide show
  1. {jmcomic-2.7.2/src/jmcomic.egg-info → jmcomic-2.7.4}/PKG-INFO +39 -13
  2. {jmcomic-2.7.2 → jmcomic-2.7.4}/README.md +38 -12
  3. {jmcomic-2.7.2 → jmcomic-2.7.4}/src/jmcomic/__init__.py +2 -3
  4. {jmcomic-2.7.2 → jmcomic-2.7.4}/src/jmcomic/api.py +83 -66
  5. {jmcomic-2.7.2 → jmcomic-2.7.4}/src/jmcomic/cli.py +41 -6
  6. {jmcomic-2.7.2 → jmcomic-2.7.4}/src/jmcomic/jm_async_client.py +42 -9
  7. {jmcomic-2.7.2 → jmcomic-2.7.4}/src/jmcomic/jm_async_downloader.py +49 -22
  8. {jmcomic-2.7.2 → jmcomic-2.7.4}/src/jmcomic/jm_client_impl.py +99 -11
  9. {jmcomic-2.7.2 → jmcomic-2.7.4}/src/jmcomic/jm_client_interface.py +236 -0
  10. {jmcomic-2.7.2 → jmcomic-2.7.4}/src/jmcomic/jm_config.py +59 -7
  11. {jmcomic-2.7.2 → jmcomic-2.7.4}/src/jmcomic/jm_downloader.py +194 -27
  12. {jmcomic-2.7.2 → jmcomic-2.7.4}/src/jmcomic/jm_entity.py +119 -9
  13. {jmcomic-2.7.2 → jmcomic-2.7.4}/src/jmcomic/jm_feature.py +22 -22
  14. {jmcomic-2.7.2 → jmcomic-2.7.4}/src/jmcomic/jm_plugin.py +477 -9
  15. jmcomic-2.7.4/src/jmcomic/jm_task_context.py +59 -0
  16. {jmcomic-2.7.2 → jmcomic-2.7.4}/src/jmcomic/jm_toolkit.py +220 -10
  17. {jmcomic-2.7.2 → jmcomic-2.7.4/src/jmcomic.egg-info}/PKG-INFO +39 -13
  18. {jmcomic-2.7.2 → jmcomic-2.7.4}/src/jmcomic.egg-info/SOURCES.txt +1 -1
  19. jmcomic-2.7.2/src/jmcomic/cl.py +0 -9
  20. {jmcomic-2.7.2 → jmcomic-2.7.4}/LICENSE +0 -0
  21. {jmcomic-2.7.2 → jmcomic-2.7.4}/pyproject.toml +0 -0
  22. {jmcomic-2.7.2 → jmcomic-2.7.4}/setup.cfg +0 -0
  23. {jmcomic-2.7.2 → jmcomic-2.7.4}/setup.py +0 -0
  24. {jmcomic-2.7.2 → jmcomic-2.7.4}/src/jmcomic/jm_exception.py +0 -0
  25. {jmcomic-2.7.2 → jmcomic-2.7.4}/src/jmcomic/jm_option.py +0 -0
  26. {jmcomic-2.7.2 → jmcomic-2.7.4}/src/jmcomic.egg-info/dependency_links.txt +0 -0
  27. {jmcomic-2.7.2 → jmcomic-2.7.4}/src/jmcomic.egg-info/entry_points.txt +0 -0
  28. {jmcomic-2.7.2 → jmcomic-2.7.4}/src/jmcomic.egg-info/requires.txt +0 -0
  29. {jmcomic-2.7.2 → jmcomic-2.7.4}/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.2
3
+ Version: 2.7.4
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
  [![GitHub latest releases](https://img.shields.io/github/v/release/hect0x7/JMComic-Crawler-Python?color=blue&label=version)](https://github.com/hect0x7/JMComic-Crawler-Python/releases/latest)
77
77
  [![PyPI - Downloads](https://img.shields.io/pypi/dm/jmcomic?style=flat&color=hotpink)](https://pepy.tech/projects/jmcomic)
78
78
  [![Licence](https://img.shields.io/github/license/hect0x7/JMComic-Crawler-Python?color=red)](https://github.com/hect0x7/JMComic-Crawler-Python)
79
+ [![Used by](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fraw.githubusercontent.com%2Fhect0x7%2Fhect0x7%2Fused-by%2Fmanifest.json&query=%24.public_dependents&label=used%20by&color=6f42c1&style=flat)](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
 
@@ -177,13 +179,12 @@ jmcomic.download_album(123, option)
177
179
  ```
178
180
 
179
181
  ### 3. 使用命令行
182
+ > [!TIP]
180
183
  > 如果只想下载本子,使用命令行会比上述方式更加简单直接
181
184
  >
182
185
  > 例如,在windows上,直接按下 win+R 键,输入`jmcomic xxx`就可以下载本子。
183
186
 
184
- 示例:
185
-
186
- 下载本子123的命令
187
+ 示例:下载本子123的命令
187
188
 
188
189
  ```sh
189
190
  jmcomic 123
@@ -195,22 +196,28 @@ jmcomic 123 p456
195
196
 
196
197
  命令行模式也支持自定义option,你可以使用环境变量或者命令行参数:
197
198
 
198
- a. 通过命令行--option参数指定option文件路径
199
+ a. 通过命令行 --option 参数指定option文件路径
199
200
 
200
201
  ```sh
201
202
  jmcomic 123 --option="D:/a.yml"
202
203
  ```
203
204
 
204
- b. 配置环境变量 `JM_OPTION_PATH` 为option文件路径(推荐)
205
+ b. 命令行不改,而是配置环境变量 `JM_OPTION_PATH` 为option文件路径(推荐)
205
206
 
207
+ > [!TIP]
206
208
  > 请自行google配置环境变量的方式,或使用powershell命令: `setx JM_OPTION_PATH "D:/a.yml"` 重启后生效
207
209
 
208
- ```sh
209
- jmcomic 123
210
- ```
210
+
211
+ 另外,`jmcomic` 还提供了一个美观的进度条能力,默认是开启的,但是需要你手动安装一个额外依赖 `pip install rich`。效果如下:
212
+
213
+ ![jmcomic 命令行下载进度](./assets/docs/sources/images/download_progress_terminal.png)
214
+
215
+ 详细开启方法参考文档:[启用美观的下载进度条](assets/docs/sources/tutorial/15_download_progress.md)
216
+
211
217
 
212
218
  ### 4. 查看本子详情(jmv 命令)
213
219
 
220
+ > [!NOTE]
214
221
  > `jmv` 命令用于快速查看本子详情,不做下载。
215
222
  >
216
223
  > **适用场景**:在某些网站上看到一串*神秘车号*,想快速看看具体是啥本子。此时只需copy原文本,按下 win+R,输入`jmv [粘贴内容]`即可
@@ -277,15 +284,16 @@ jmv 350234 -y
277
284
 
278
285
  - **绕过Cloudflare的反爬虫**
279
286
  - **实现禁漫APP接口最新的加解密算法 (1.6.3)**
287
+ - 支持**Async**和**Sync**两套 API
280
288
  - 用法多样:
281
289
 
282
290
  - GitHub
283
291
  Actions:网页上直接输入本子id就能下载([教程:使用GitHub Actions下载禁漫本子](./assets/docs/sources/tutorial/1_github_actions.md))
284
292
  - 命令行:无需写Python代码,简单易用([教程:使用命令行下载禁漫本子](./assets/docs/sources/tutorial/2_command_line.md))
285
293
  - Python代码:最本质、最强大的使用方式,需要你有一定的python编程基础
286
- - **支持 Async 和 Sync 两套 API**
287
294
  - 支持**网页端**和**移动端**两种客户端实现,可通过配置切换(**移动端不限ip兼容性好,网页端限制ip地区但效率高**)
288
295
  - 支持**自动重试和域名切换**机制
296
+ - 支持按下载任务维度结构化收集日志
289
297
  - **可配置性强**
290
298
 
291
299
  - 不配置也能使用,十分方便
@@ -294,7 +302,7 @@ jmv 350234 -y
294
302
  等
295
303
  - **可扩展性强**
296
304
 
297
- - 支持自定义本子/章节/图片下载前后的回调函数
305
+ - 支持自定义本子/章节/图片下载前后事件的回调函数
298
306
  - 支持自定义类:`Downloader(负责调度)` `Option(负责配置)` `Client(负责请求)` `实体类`等
299
307
  - 支持自定义日志、异常监听器
300
308
  - **支持Plugin插件,可以方便地扩展功能,以及使用别人的插件,目前核心内置插件有**:
@@ -333,3 +341,21 @@ jmv 350234 -y
333
341
  <img alt="Repo Card" src="https://github-readme-stats.vercel.app/api/pin/?username=tonquer&repo=JMComic-qt" />
334
342
  </picture>
335
343
  </a>
344
+
345
+ ## used by 项目
346
+
347
+ <a href="https://github.com/hect0x7/JMComic-Crawler-Python/network/dependents">
348
+ <picture>
349
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/hect0x7/hect0x7/used-by/showcase/zh-CN-dark.svg" />
350
+ <source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/hect0x7/hect0x7/used-by/showcase/zh-CN-light.svg" />
351
+ <img width="100%" alt="使用 jmcomic 的项目" src="https://raw.githubusercontent.com/hect0x7/hect0x7/used-by/showcase/zh-CN-light.svg" />
352
+ </picture>
353
+ </a>
354
+
355
+ ## Star History
356
+
357
+ <picture>
358
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/hect0x7/hect0x7/output/profile/star-history-dark.svg" />
359
+ <source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/hect0x7/hect0x7/output/profile/star-history.svg" />
360
+ <img width="100%" alt="jmcomic 生态 Star History" src="https://raw.githubusercontent.com/hect0x7/hect0x7/output/profile/star-history.svg" />
361
+ </picture>
@@ -19,6 +19,7 @@
19
19
  [![GitHub latest releases](https://img.shields.io/github/v/release/hect0x7/JMComic-Crawler-Python?color=blue&label=version)](https://github.com/hect0x7/JMComic-Crawler-Python/releases/latest)
20
20
  [![PyPI - Downloads](https://img.shields.io/pypi/dm/jmcomic?style=flat&color=hotpink)](https://pepy.tech/projects/jmcomic)
21
21
  [![Licence](https://img.shields.io/github/license/hect0x7/JMComic-Crawler-Python?color=red)](https://github.com/hect0x7/JMComic-Crawler-Python)
22
+ [![Used by](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fraw.githubusercontent.com%2Fhect0x7%2Fhect0x7%2Fused-by%2Fmanifest.json&query=%24.public_dependents&label=used%20by&color=6f42c1&style=flat)](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
 
@@ -120,13 +122,12 @@ jmcomic.download_album(123, option)
120
122
  ```
121
123
 
122
124
  ### 3. 使用命令行
125
+ > [!TIP]
123
126
  > 如果只想下载本子,使用命令行会比上述方式更加简单直接
124
127
  >
125
128
  > 例如,在windows上,直接按下 win+R 键,输入`jmcomic xxx`就可以下载本子。
126
129
 
127
- 示例:
128
-
129
- 下载本子123的命令
130
+ 示例:下载本子123的命令
130
131
 
131
132
  ```sh
132
133
  jmcomic 123
@@ -138,22 +139,28 @@ jmcomic 123 p456
138
139
 
139
140
  命令行模式也支持自定义option,你可以使用环境变量或者命令行参数:
140
141
 
141
- a. 通过命令行--option参数指定option文件路径
142
+ a. 通过命令行 --option 参数指定option文件路径
142
143
 
143
144
  ```sh
144
145
  jmcomic 123 --option="D:/a.yml"
145
146
  ```
146
147
 
147
- b. 配置环境变量 `JM_OPTION_PATH` 为option文件路径(推荐)
148
+ b. 命令行不改,而是配置环境变量 `JM_OPTION_PATH` 为option文件路径(推荐)
148
149
 
150
+ > [!TIP]
149
151
  > 请自行google配置环境变量的方式,或使用powershell命令: `setx JM_OPTION_PATH "D:/a.yml"` 重启后生效
150
152
 
151
- ```sh
152
- jmcomic 123
153
- ```
153
+
154
+ 另外,`jmcomic` 还提供了一个美观的进度条能力,默认是开启的,但是需要你手动安装一个额外依赖 `pip install rich`。效果如下:
155
+
156
+ ![jmcomic 命令行下载进度](./assets/docs/sources/images/download_progress_terminal.png)
157
+
158
+ 详细开启方法参考文档:[启用美观的下载进度条](assets/docs/sources/tutorial/15_download_progress.md)
159
+
154
160
 
155
161
  ### 4. 查看本子详情(jmv 命令)
156
162
 
163
+ > [!NOTE]
157
164
  > `jmv` 命令用于快速查看本子详情,不做下载。
158
165
  >
159
166
  > **适用场景**:在某些网站上看到一串*神秘车号*,想快速看看具体是啥本子。此时只需copy原文本,按下 win+R,输入`jmv [粘贴内容]`即可
@@ -220,15 +227,16 @@ jmv 350234 -y
220
227
 
221
228
  - **绕过Cloudflare的反爬虫**
222
229
  - **实现禁漫APP接口最新的加解密算法 (1.6.3)**
230
+ - 支持**Async**和**Sync**两套 API
223
231
  - 用法多样:
224
232
 
225
233
  - GitHub
226
234
  Actions:网页上直接输入本子id就能下载([教程:使用GitHub Actions下载禁漫本子](./assets/docs/sources/tutorial/1_github_actions.md))
227
235
  - 命令行:无需写Python代码,简单易用([教程:使用命令行下载禁漫本子](./assets/docs/sources/tutorial/2_command_line.md))
228
236
  - Python代码:最本质、最强大的使用方式,需要你有一定的python编程基础
229
- - **支持 Async 和 Sync 两套 API**
230
237
  - 支持**网页端**和**移动端**两种客户端实现,可通过配置切换(**移动端不限ip兼容性好,网页端限制ip地区但效率高**)
231
238
  - 支持**自动重试和域名切换**机制
239
+ - 支持按下载任务维度结构化收集日志
232
240
  - **可配置性强**
233
241
 
234
242
  - 不配置也能使用,十分方便
@@ -237,7 +245,7 @@ jmv 350234 -y
237
245
  等
238
246
  - **可扩展性强**
239
247
 
240
- - 支持自定义本子/章节/图片下载前后的回调函数
248
+ - 支持自定义本子/章节/图片下载前后事件的回调函数
241
249
  - 支持自定义类:`Downloader(负责调度)` `Option(负责配置)` `Client(负责请求)` `实体类`等
242
250
  - 支持自定义日志、异常监听器
243
251
  - **支持Plugin插件,可以方便地扩展功能,以及使用别人的插件,目前核心内置插件有**:
@@ -276,3 +284,21 @@ jmv 350234 -y
276
284
  <img alt="Repo Card" src="https://github-readme-stats.vercel.app/api/pin/?username=tonquer&repo=JMComic-qt" />
277
285
  </picture>
278
286
  </a>
287
+
288
+ ## used by 项目
289
+
290
+ <a href="https://github.com/hect0x7/JMComic-Crawler-Python/network/dependents">
291
+ <picture>
292
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/hect0x7/hect0x7/used-by/showcase/zh-CN-dark.svg" />
293
+ <source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/hect0x7/hect0x7/used-by/showcase/zh-CN-light.svg" />
294
+ <img width="100%" alt="使用 jmcomic 的项目" src="https://raw.githubusercontent.com/hect0x7/hect0x7/used-by/showcase/zh-CN-light.svg" />
295
+ </picture>
296
+ </a>
297
+
298
+ ## Star History
299
+
300
+ <picture>
301
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/hect0x7/hect0x7/output/profile/star-history-dark.svg" />
302
+ <source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/hect0x7/hect0x7/output/profile/star-history.svg" />
303
+ <img width="100%" alt="jmcomic 生态 Star History" src="https://raw.githubusercontent.com/hect0x7/hect0x7/output/profile/star-history.svg" />
304
+ </picture>
@@ -2,15 +2,14 @@
2
2
  # 被依赖方 <--- 使用方
3
3
  # config <--- entity <--- toolkit <--- client <--- option <--- downloader
4
4
 
5
- __version__ = '2.7.2'
5
+ __version__ = '2.7.4'
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,26 +1,25 @@
1
1
  import asyncio
2
- import inspect
2
+ from time import perf_counter
3
3
 
4
4
  from .jm_downloader import *
5
+ from .jm_task_context import bind_jm_task_context, jm_task_context
5
6
 
6
7
  __DOWNLOAD_API_RET = DownloadResult
7
8
 
8
9
 
9
- async def _invoke_async_callback(callback, entity, downloader):
10
- if callback is None:
11
- return None
10
+ def _download_type(download_api) -> str:
11
+ name = getattr(download_api, '__name__', download_api.__class__.__name__)
12
+ if name.endswith('_async'):
13
+ name = name[:-6]
14
+ if name.startswith('download_'):
15
+ name = name[9:]
16
+ return name
12
17
 
13
- is_async_callback = (
14
- inspect.iscoroutinefunction(callback)
15
- or inspect.iscoroutinefunction(getattr(callback, '__call__', None))
16
- )
17
- if is_async_callback:
18
- return await callback(entity, downloader)
19
18
 
20
- result = await asyncio.to_thread(callback, entity, downloader)
21
- if inspect.isawaitable(result):
22
- return await result
23
- return result
19
+ def _finish_download_result(detail, dler, task_started_at):
20
+ manifest = dler.manifest_dict[detail]
21
+ manifest.duration = perf_counter() - task_started_at
22
+ return DownloadResult(detail, dler)
24
23
 
25
24
 
26
25
  def download_batch(download_api,
@@ -48,21 +47,24 @@ def download_batch(download_api,
48
47
 
49
48
  result = BatchResult()
50
49
 
50
+ download_type = _download_type(download_api)
51
+
51
52
  def _safe_download(aid):
52
53
  """batch 内部的单任务包装:确保异常被收集而非静默丢失"""
53
- try:
54
- ret = download_api(aid, option, downloader, **kwargs)
55
- result.add(ret)
56
- except Exception as e:
57
- jm_log('batch.failed', f'批量下载失败: [{aid}], 异常: [{e}]', e)
58
- result.failed[str(aid)] = e
54
+ with jm_task_context(download_type=download_type, jm_id=str(aid)):
55
+ try:
56
+ ret = download_api(aid, option, downloader, **kwargs)
57
+ result.add(ret)
58
+ except Exception as e:
59
+ jm_log('batch.failed', f'批量下载失败: [{aid}], 异常: [{e}]', e)
60
+ result.failed[str(aid)] = e
59
61
 
60
62
  multi_thread_launcher(
61
63
  iter_objs=set(
62
64
  JmcomicText.parse_to_jm_id(jmid)
63
65
  for jmid in jm_id_iter
64
66
  ),
65
- apply_each_obj_func=_safe_download,
67
+ apply_each_obj_func=bind_jm_task_context(_safe_download),
66
68
  wait_finish=True
67
69
  )
68
70
 
@@ -72,7 +74,7 @@ def download_batch(download_api,
72
74
  def download_album(jm_album_id,
73
75
  option=None,
74
76
  downloader=None,
75
- callback=None,
77
+ *,
76
78
  check_exception=True,
77
79
  extra=None,
78
80
  ) -> Union[__DOWNLOAD_API_RET, Set[__DOWNLOAD_API_RET]]:
@@ -84,8 +86,9 @@ def download_album(jm_album_id,
84
86
  :param jm_album_id: 本子的禁漫车号
85
87
  :param option: 下载选项
86
88
  :param downloader: 下载器类
87
- :param callback: 返回值回调函数,可以拿到 album 和 downloader
88
- :param check_exception: 是否检查异常, 如果为True,会检查downloader是否有下载异常,并上抛PartialDownloadFailedException
89
+ :param check_exception: 仅当 jm_album_id 是单个 ID 时生效。为 True 时检查 downloader 中的部分下载失败,
90
+ 并上抛 PartialDownloadFailedException。多 ID 调用会转交 download_batch,此参数不生效;
91
+ 请检查 BatchResult.failed,或自行封装 download_batch 实现所需的批量异常策略。
89
92
  :param extra: 下载特性(Feature),下载时动态挂载的附加行为上下文。会自动根据上下文(如 album/photo 来源)自适应参数行为。支持单个 Feature、FeatureChain、或列表
90
93
  :return: 对于的本子实体类,下载器(如果是上述的批量情况,返回值为download_batch的返回值)
91
94
  """
@@ -93,41 +96,46 @@ def download_album(jm_album_id,
93
96
  if not isinstance(jm_album_id, (str, int)):
94
97
  return download_batch(download_album, jm_album_id, option, downloader, extra=extra)
95
98
 
96
- with new_downloader(option, downloader) as dler:
97
- # 注册 Feature 及来源,由 downloader 在 after_album 钩子中自动执行
98
- dler.add_features(extra, 'download_album')
99
- album = dler.download_album(jm_album_id)
99
+ task_started_at = perf_counter()
100
+ with jm_task_context(download_type='album', jm_id=str(jm_album_id), task_started_at=task_started_at):
101
+ with new_downloader(option, downloader) as dler:
102
+ # 下载类型已记录在 TaskContext 中,Feature 会据此选择执行钩子
103
+ dler.add_features(extra)
104
+ album = dler.download_album(jm_album_id)
105
+
106
+ if check_exception:
107
+ dler.raise_if_has_exception()
100
108
 
101
- if callback is not None:
102
- callback(album, dler)
103
- if check_exception:
104
- dler.raise_if_has_exception()
105
- return DownloadResult(album, dler)
109
+ return _finish_download_result(album, dler, task_started_at)
106
110
 
107
111
 
108
112
  def download_photo(jm_photo_id,
109
113
  option=None,
110
114
  downloader=None,
111
- callback=None,
115
+ *,
112
116
  check_exception=True,
113
117
  extra=None,
114
118
  ):
115
119
  """
116
- 下载一个章节(photo),参数同 download_album
120
+ 下载一个章节(photo),参数同 download_album。
121
+
122
+ check_exception 仅当 jm_photo_id 是单个 ID 时生效。多 ID 场景请检查
123
+ BatchResult.failed,或自行封装 download_batch 处理批量异常。
117
124
  """
118
125
  if not isinstance(jm_photo_id, (str, int)):
119
126
  return download_batch(download_photo, jm_photo_id, option, downloader, extra=extra)
120
127
 
121
- with new_downloader(option, downloader) as dler:
122
- # 注册 Feature 及来源,由 downloader 在 after_photo 钩子中自动执行
123
- dler.add_features(extra, 'download_photo')
124
- photo = dler.download_photo(jm_photo_id)
128
+ task_started_at = perf_counter()
129
+ with jm_task_context(download_type='photo', jm_id=str(jm_photo_id), task_started_at=task_started_at):
130
+ with new_downloader(option, downloader) as dler:
131
+ # 下载类型已记录在 TaskContext 中,Feature 会据此选择执行钩子
132
+ dler.add_features(extra)
133
+ photo = dler.download_photo(jm_photo_id)
134
+
135
+ if check_exception:
136
+ dler.raise_if_has_exception()
125
137
 
126
- if callback is not None:
127
- callback(photo, dler)
128
- if check_exception:
129
- dler.raise_if_has_exception()
130
- return DownloadResult(photo, dler)
138
+ return _finish_download_result(photo, dler, task_started_at)
131
139
 
132
140
 
133
141
  def new_downloader(option=None, downloader=None) -> JmDownloader:
@@ -164,12 +172,11 @@ create_option = create_option_by_file
164
172
 
165
173
 
166
174
  def new_async_downloader(option=None, downloader=None):
167
- from .jm_async_downloader import JmAsyncDownloader
168
175
  if option is None:
169
176
  option = JmModuleConfig.option_class().default()
170
177
 
171
178
  if downloader is None:
172
- downloader = JmAsyncDownloader
179
+ downloader = JmModuleConfig.async_downloader_class()
173
180
 
174
181
  return downloader(option)
175
182
 
@@ -177,7 +184,7 @@ def new_async_downloader(option=None, downloader=None):
177
184
  async def download_album_async(jm_album_id,
178
185
  option=None,
179
186
  downloader=None,
180
- callback=None,
187
+ *,
181
188
  check_exception=True,
182
189
  extra=None,
183
190
  ):
@@ -185,8 +192,9 @@ async def download_album_async(jm_album_id,
185
192
  异步下载一个本子(album),包含其所有的章节(photo)。
186
193
 
187
194
  - 支持批量下载(当 jm_album_id 为可迭代对象时)
188
- - callback 支持同步函数和异步函数
189
195
  - 返回 (album, downloader) 元组,其中 downloader 的网络和线程池资源已关闭,仅用于读取下载结果
196
+ - check_exception 仅当 jm_album_id 是单个 ID 时生效。多 ID 场景请检查 BatchResult.failed,
197
+ 或自行封装 download_batch_async 处理批量异常
190
198
  """
191
199
  if not isinstance(jm_album_id, (str, int)):
192
200
  return await download_batch_async(download_album_async,
@@ -196,28 +204,30 @@ async def download_album_async(jm_album_id,
196
204
  extra=extra
197
205
  )
198
206
 
199
- async with new_async_downloader(option, downloader) as dler:
200
- dler.add_features(extra, 'download_album')
201
- album = await dler.download_album(jm_album_id)
207
+ task_started_at = perf_counter()
208
+ with jm_task_context(download_type='album', jm_id=str(jm_album_id), task_started_at=task_started_at):
209
+ async with new_async_downloader(option, downloader) as dler:
210
+ dler.add_features(extra)
211
+ album = await dler.download_album(jm_album_id)
202
212
 
203
- await _invoke_async_callback(callback, album, dler)
204
- if check_exception:
205
- dler.raise_if_has_exception()
213
+ if check_exception:
214
+ dler.raise_if_has_exception()
206
215
 
207
- return DownloadResult(album, dler)
216
+ return _finish_download_result(album, dler, task_started_at)
208
217
 
209
218
 
210
219
  async def download_photo_async(jm_photo_id,
211
220
  option=None,
212
221
  downloader=None,
213
- callback=None,
222
+ *,
214
223
  check_exception=True,
215
224
  extra=None,
216
225
  ):
217
226
  """
218
227
  异步下载一个章节(photo)。
219
- callback 支持同步函数和异步函数。
220
228
  返回的 downloader 已关闭网络和线程池资源,仅用于读取下载结果。
229
+ check_exception 仅当 jm_photo_id 是单个 ID 时生效。多 ID 场景请检查
230
+ BatchResult.failed,或自行封装 download_batch_async 处理批量异常。
221
231
  """
222
232
  if not isinstance(jm_photo_id, (str, int)):
223
233
  return await download_batch_async(download_photo_async,
@@ -227,15 +237,16 @@ async def download_photo_async(jm_photo_id,
227
237
  extra=extra
228
238
  )
229
239
 
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)
240
+ task_started_at = perf_counter()
241
+ with jm_task_context(download_type='photo', jm_id=str(jm_photo_id), task_started_at=task_started_at):
242
+ async with new_async_downloader(option, downloader) as dler:
243
+ dler.add_features(extra)
244
+ photo = await dler.download_photo(jm_photo_id)
233
245
 
234
- await _invoke_async_callback(callback, photo, dler)
235
- if check_exception:
236
- dler.raise_if_has_exception()
246
+ if check_exception:
247
+ dler.raise_if_has_exception()
237
248
 
238
- return DownloadResult(photo, dler)
249
+ return _finish_download_result(photo, dler, task_started_at)
239
250
 
240
251
 
241
252
  async def download_batch_async(download_api,
@@ -253,9 +264,14 @@ async def download_batch_async(download_api,
253
264
  option = JmModuleConfig.option_class().default()
254
265
 
255
266
  jm_ids = list(dict.fromkeys(JmcomicText.parse_to_jm_id(jmid) for jmid in jm_id_iter))
267
+ download_type = _download_type(download_api)
268
+
269
+ async def _download_one(jmid):
270
+ with jm_task_context(download_type=download_type, jm_id=str(jmid)):
271
+ return await download_api(jmid, option, downloader, **kwargs)
256
272
 
257
273
  results = await asyncio.gather(
258
- *(download_api(jmid, option, downloader, **kwargs) for jmid in jm_ids),
274
+ *(_download_one(jmid) for jmid in jm_ids),
259
275
  return_exceptions=True,
260
276
  )
261
277
 
@@ -263,7 +279,8 @@ async def download_batch_async(download_api,
263
279
  result = BatchResult()
264
280
  for jmid, r in zip(jm_ids, results):
265
281
  if isinstance(r, BaseException):
266
- jm_log('async.batch.failed', f'批量下载失败: [{jmid}], 异常: [{r}]', r)
282
+ with jm_task_context(download_type=download_type, jm_id=str(jmid)):
283
+ jm_log('async.batch.failed', f'批量下载失败: [{jmid}], 异常: [{r}]', r)
267
284
  result.failed[str(jmid)] = r
268
285
  else:
269
286
  result.add(r)
@@ -12,6 +12,7 @@ command-line usage
12
12
  $ jmv abc123141 --option="D:/option.yml"
13
13
 
14
14
  """
15
+ import importlib.util
15
16
  import os.path
16
17
  from typing import List, Optional
17
18
 
@@ -29,6 +30,7 @@ class JmcomicUI:
29
30
 
30
31
  def __init__(self) -> None:
31
32
  self.option_path: Optional[str] = None
33
+ self.progress_enabled = True
32
34
  self.raw_id_list: List[str] = []
33
35
  self.album_id_list: List[str] = []
34
36
  self.photo_id_list: List[str] = []
@@ -50,8 +52,15 @@ class JmcomicUI:
50
52
  type=str,
51
53
  default=get_env('JM_OPTION_PATH', ''),
52
54
  )
55
+ parser.add_argument(
56
+ '--no-progress',
57
+ action='store_false',
58
+ dest='progress_enabled',
59
+ help='do not automatically enable the download progress plugin',
60
+ )
53
61
 
54
62
  args = parser.parse_args()
63
+ self.progress_enabled = args.progress_enabled
55
64
  option = args.option
56
65
  if len(option) == 0 or option == "''":
57
66
  self.option_path = None
@@ -82,6 +91,16 @@ class JmcomicUI:
82
91
 
83
92
  def main(self):
84
93
  self.parse_arg()
94
+ from .api import create_option, JmOption
95
+ from .jm_task_context import jm_task_context
96
+ with jm_task_context(cli_no_progress=not self.progress_enabled):
97
+ if self.option_path is not None:
98
+ option = create_option(self.option_path)
99
+ else:
100
+ option = JmOption.default()
101
+
102
+ self.enable_download_progress(option)
103
+
85
104
  from .api import jm_log
86
105
  jm_log('command_line',
87
106
  f'start downloading...\n'
@@ -90,14 +109,30 @@ class JmcomicUI:
90
109
  f'- album: {self.album_id_list}\n'
91
110
  f'- photo: {self.photo_id_list}')
92
111
 
93
- from .api import create_option, JmOption
94
- if self.option_path is not None:
95
- option = create_option(self.option_path)
96
- else:
97
- option = JmOption.default()
98
-
99
112
  self.run(option)
100
113
 
114
+ def enable_download_progress(self, option):
115
+ option_enabled = self.option_has_download_progress(option)
116
+ if not self.progress_enabled:
117
+ return
118
+
119
+ if option_enabled:
120
+ return
121
+
122
+ if importlib.util.find_spec('rich') is None:
123
+ from .jm_config import jm_log
124
+ jm_log('command_line.progress',
125
+ '未安装 rich,继续使用普通日志。如需显示下载进度,请执行:pip install rich')
126
+ return
127
+
128
+ from .jm_plugin import DownloadProgressPlugin
129
+ DownloadProgressPlugin.build(option).invoke()
130
+
131
+ @staticmethod
132
+ def option_has_download_progress(option):
133
+ plugin_list = option.plugins.get('after_init', []) or []
134
+ return any(plugin.get('plugin') == 'download_progress' for plugin in plugin_list)
135
+
101
136
  def run(self, option):
102
137
  from .api import download_album, download_photo
103
138
  from common import MultiTaskLauncher