Skip to content

Commit ac33f4d

Browse files
authored
v2.5.11: 新增【img2pdf】插件实现图片合并为pdf,可替代旧的【j2p】插件,功能更加强大; 优化文档; (#230)
1 parent 9def295 commit ac33f4d

File tree

7 files changed

+88
-15
lines changed

7 files changed

+88
-15
lines changed

assets/docs/sources/TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Plan For Update Content
1+
# 版本更新计划
22

33
| 版本范围 | 更新计划 |
44
|:--------:|:--------------------------------------:|

assets/docs/sources/option_file_syntax.md

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
# Option File Syntax
1+
# 配置文件指南
22

33
## 1. 配置前需知
44

55
* option有`默认值`,当你使用配置文件来创建option时,你配置文件中的值会覆盖`默认值`
66

77
因此,在配置option时,不需要配置全部的值,只需要配置特定部分即可。
8-
* 你可以使用下面的代码来得到option的默认值,你可以删除其中的大部分配置项,只保留你要覆盖的配置项
98

10-
* **下面的插件配置,kwargs参数支持引用环境变量,语法为 ${环境变量名}**
9+
* 你可以使用下面的代码来得到option的默认值,你可以删除其中的大部分配置项,只保留你要覆盖的配置项
1110

1211
```python
1312
from jmcomic import JmOption
@@ -17,16 +16,16 @@ JmOption.default().to_file('./option.yml') # 创建默认option,导出为optio
1716
## 2. option常规配置项
1817

1918
```yml
20-
# 开启jmcomic的日志输入,默认为true
21-
# 对日志有需求的可进一步参考文档
19+
# 开启jmcomic的日志输出,默认为true
20+
# 对日志有需求的可进一步参考文档 → https://jmcomic.readthedocs.io/en/latest/tutorial/11_log_custom/
2221
log: true
2322

2423
# 配置客户端相关
2524
client:
2625
# impl: 客户端实现类,不配置默认会使用JmModuleConfig.DEFAULT_CLIENT_IMPL
2726
# 可配置:
2827
# html - 表示网页端
29-
# api - 表示使用APP端
28+
# api - 表示APP端
3029
impl: html
3130

3231
# domain: 域名配置,默认是 [],表示运行时自动获取域名。
@@ -43,7 +42,7 @@ client:
4342

4443
# postman: 请求配置
4544
postman:
46-
meta_data:
45+
metadata:
4746
# proxies: 代理配置,默认是 system,表示使用系统代理。
4847
# 以下的写法都可以:
4948
# proxies: null # 不使用代理
@@ -108,6 +107,8 @@ dir_rule:
108107
109108
## 3. option插件配置项
110109
110+
* **插件配置中的kwargs参数支持引用环境变量,语法为 ${环境变量名}**
111+
111112
```yml
112113
# 插件的配置示例
113114
plugins:
@@ -136,14 +137,14 @@ plugins:
136137
proxy_client_key: photo_concurrent_fetcher_proxy # 代理类的client_key
137138
whitelist: [ api, ] # 白名单,当client.impl匹配白名单时才代理
138139

139-
- plugin: auto_set_browser_cookies # 自动获取浏览器cookies,详见插件类
140+
- plugin: auto_set_browser_cookies # 自动获取浏览器cookies,详见插件类代码→AutoSetBrowserCookiesPlugin
140141
kwargs:
141142
browser: chrome
142143
domain: 18comic.vip
143144

144145
# v2.5.0 引入的插件
145146
# 可以启动一个服务器,可以在浏览器上查看本子
146-
# 基于flask框架,需要安装额外库: pip install plugin_jm_server
147+
# 基于flask框架,需要安装额外库: [pip install plugin_jm_server]
147148
# 源码:https://github.com/hect0x7/plugin-jm-server
148149
- plugin: jm_server
149150
kwargs:
@@ -212,15 +213,26 @@ plugins:
212213
at_least_image_count: 3 # 至少要有多少张图,才下载此章节
213214

214215
after_photo:
216+
# 把章节的所有图片合并为一个pdf的插件
217+
# 使用前需要安装依赖库: [pip install img2pdf]
218+
- plugin: img2pdf
219+
kwargs:
220+
pdf_dir: D:/pdf/ # pdf存放文件夹
221+
filename_rule: Pid # pdf命名规则
222+
223+
# 请注意⚠
224+
# 下方的j2p插件的功能不如img2pdf插件,不建议使用。
225+
# 如有图片转pdf的需求,直接使用img2pdf即可,下面的内容请忽略。
226+
215227
- plugin: j2p # 图片合并插件,可以将下载下来的jpg图片合成为一个pdf插件
216228
# 请注意⚠ 该插件的使用前提是,下载下来的图片是jpg图片
217229
# 因此,使用该插件前,需要有如下配置:(下载图片格式转为jpg,上文有解释过此配置)
218230
# download:
219231
# image:
220232
# suffix: .jpg
221233
kwargs:
222-
pdf_dir: D:/pdf # pdf存放文件夹
234+
pdf_dir: D:/pdf/ # pdf存放文件夹
223235
filename_rule: Pid # pdf命名规则
224236
quality: 100 # pdf质量,0 - 100
225-
237+
226238
```

src/jmcomic/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# 被依赖方 <--- 使用方
33
# config <--- entity <--- toolkit <--- client <--- option <--- downloader
44

5-
__version__ = '2.5.10'
5+
__version__ = '2.5.11'
66

77
from .api import *
88
from .jm_plugin import *

src/jmcomic/jm_client_interface.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,3 +572,15 @@ def categories_filter_gen(self,
572572
}
573573

574574
yield from self.do_page_iter(params, page, self.categories_filter)
575+
576+
def is_given_type(self, ctype: Type['JmcomicClient']) -> bool:
577+
"""
578+
Client代理的此方法会被路由到内部client的方法
579+
即:ClientProxy(AClient()).is_given_type(AClient) is True
580+
但是: ClientProxy(AClient()).client_key != AClient.client_key
581+
"""
582+
if isinstance(self, ctype):
583+
return True
584+
if self.client_key == instance.client_key:
585+
return True
586+
return False

src/jmcomic/jm_plugin.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,49 @@ def generate_cmd():
757757
self.execute_deletion(paths)
758758

759759

760+
class Img2pdfPlugin(JmOptionPlugin):
761+
plugin_key = 'img2pdf'
762+
763+
def invoke(self,
764+
photo: JmPhotoDetail,
765+
downloader=None,
766+
pdf_dir=None,
767+
filename_rule='Pid',
768+
delete_original_file=False,
769+
**kwargs,
770+
):
771+
try:
772+
import img2pdf
773+
except ImportError:
774+
self.warning_lib_not_install('img2pdf')
775+
return
776+
777+
self.delete_original_file = delete_original_file
778+
779+
# 处理文件夹配置
780+
filename = DirRule.apply_rule_directly(None, photo, filename_rule)
781+
photo_dir = self.option.decide_image_save_dir(photo)
782+
783+
# 处理生成的pdf文件的路径
784+
if pdf_dir is None:
785+
pdf_dir = photo_dir
786+
else:
787+
pdf_dir = fix_filepath(pdf_dir, True)
788+
mkdir_if_not_exists(pdf_dir)
789+
790+
pdf_filepath = os.path.join(pdf_dir, f'{filename}.pdf')
791+
792+
# 调用 img2pdf 把 photo_dir 下的所有图片转为pdf
793+
all_img = files_of_dir(photo_dir)
794+
with open(pdf_filepath, 'wb') as f:
795+
f.write(img2pdf.convert(all_img))
796+
797+
# 执行删除
798+
self.log(f'Convert Successfully: JM{photo.id}{pdf_filepath}')
799+
all_img.append(self.option.decide_image_save_dir(photo, ensure_exists=False))
800+
self.execute_deletion(all_img)
801+
802+
760803
class JmServerPlugin(JmOptionPlugin):
761804
plugin_key = 'jm_server'
762805

tests/test_jmcomic/test_jm_api.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ def run_func_async(func):
6969
if len(exception_list) == 0:
7070
return
7171

72+
if self.client.is_given_type(JmApiClient):
73+
return
74+
7275
for e in exception_list:
7376
print(e)
7477

75-
# raise AssertionError(exception_list)
78+
raise AssertionError(exception_list)

tests/test_jmcomic/test_jm_custom.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ class MyClient(JmHtmlClient):
6060
# 2024-04-29
6161
# 禁漫的【永久網域】加了cf,GitHub Actions请求也会失败。
6262
traceback_print_exec()
63-
return
63+
if self.client.is_given_type(JmApiClient):
64+
return
65+
else:
66+
raise e
6467

6568
JmModuleConfig.DOMAIN_HTML_LIST = [html_domain]
6669

0 commit comments

Comments
 (0)