Skip to content

Commit 1e465f7

Browse files
authored
v2.5.10: 修正zip插件在压缩本子时未正确判断章节路径的问题,优化插件配置的文档 (#225)
1 parent dec2d54 commit 1e465f7

File tree

5 files changed

+60
-70
lines changed

5 files changed

+60
-70
lines changed

assets/docs/sources/option_file_syntax.md

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,10 @@ plugins:
166166
- plugin: subscribe_album_update # 自动订阅本子并下载、发送邮件通知的插件
167167
kwargs:
168168
download_if_has_update: true
169-
email_notify: # 见下【发送qq邮件插件】
170-
msg_from: x
171-
msg_to: x
172-
password: x
169+
email_notify: # 参数说明见下【发送qq邮件插件】
170+
171+
172+
password: dkjlakdjlkas
173173
title: album update !!!
174174
content: album update !!!
175175
album_photo_dict:
@@ -179,7 +179,14 @@ plugins:
179179
- plugin: zip # 压缩文件插件
180180
kwargs:
181181
level: photo # 按照章节,一个章节一个压缩文件
182+
# level 也可以配成 album,表示一个本子对应一个压缩文件,该压缩文件会包含这个本子的所有章节
183+
182184
filename_rule: Ptitle # 压缩文件的命名规则
185+
# 请注意⚠ [https://github.com/hect0x7/JMComic-Crawler-Python/issues/223#issuecomment-2045227527]
186+
# filename_rule和level有对应关系
187+
# 如果level=[photo], filename_rule只能写Pxxx
188+
# 如果level=[album], filename_rule只能写Axxx
189+
183190
zip_dir: D:/jmcomic/zip/ # 压缩文件存放的文件夹
184191
delete_original_file: true # 压缩成功后,删除所有原文件和文件夹
185192

@@ -195,17 +202,22 @@ plugins:
195202
- plugin: favorite_folder_export # 导出收藏夹插件
196203
log: false
197204
kwargs:
198-
zip_enable: true # 对收藏夹进行压缩
205+
zip_enable: true # 对导出文件进行压缩
199206
zip_filepath: ${JM_DOWNLOAD_DIR}/export.zip # 压缩文件路径
200207
zip_password: ${ZIP_PASSWORD} # 压缩密码
201208

202209
before_photo:
203210
- plugin: skip_photo_with_few_images # 跳过下载章节图片数量过少的章节。一些韩漫的章节是公告,没有实际内容,就可以用该插件来跳过下载这些章节。
204211
kwargs:
205-
at_least_image_count: 3
212+
at_least_image_count: 3 # 至少要有多少张图,才下载此章节
206213

207214
after_photo:
208-
- plugin: j2p # jpg图片合成为一个pdf插件
215+
- plugin: j2p # 图片合并插件,可以将下载下来的jpg图片合成为一个pdf插件
216+
# 请注意⚠ 该插件的使用前提是,下载下来的图片是jpg图片
217+
# 因此,使用该插件前,需要有如下配置:(下载图片格式转为jpg,上文有解释过此配置)
218+
# download:
219+
# image:
220+
# suffix: .jpg
209221
kwargs:
210222
pdf_dir: D:/pdf # pdf存放文件夹
211223
filename_rule: Pid # pdf命名规则

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.9'
5+
__version__ = '2.5.10'
66

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

src/jmcomic/jm_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ def new_postman(cls, session=False, **kwargs):
368368
'postman': {
369369
'type': 'cffi',
370370
'meta_data': {
371-
'impersonate': 'chrome',
371+
'impersonate': 'chrome110',
372372
'headers': None,
373373
'proxies': None,
374374
}

src/jmcomic/jm_option.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -236,28 +236,6 @@ def decide_image_batch_count(self, photo: JmPhotoDetail):
236236
def decide_photo_batch_count(self, album: JmAlbumDetail):
237237
return self.download.threading.photo
238238

239-
def decide_album_dir(self, album: JmAlbumDetail) -> str:
240-
"""
241-
该方法目前仅在 plugin-zip 中使用,不建议外部调用
242-
"""
243-
dir_layer = []
244-
dir_rule = self.dir_rule
245-
for rule in dir_rule.rule_dsl.split('_'):
246-
if rule == 'Bd':
247-
dir_layer.append(dir_rule.base_dir)
248-
continue
249-
250-
if rule[0] == 'A':
251-
name = dir_rule.apply_rule_directly(album, None, rule)
252-
dir_layer.append(name)
253-
254-
if rule[0] == 'P':
255-
break
256-
257-
from os.path import join
258-
# noinspection PyTypeChecker
259-
return join(*dir_layer)
260-
261239
# noinspection PyMethodMayBeStatic
262240
def decide_image_filename(self, image: JmImageDetail) -> str:
263241
"""

src/jmcomic/jm_plugin.py

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -302,27 +302,19 @@ def invoke(self,
302302

303303
if level == 'album':
304304
zip_path = self.get_zip_path(album, None, filename_rule, suffix, zip_dir)
305-
dir_path = self.zip_album(album, photo_dict, zip_path)
306-
if dir_path is not None:
307-
# 要删除这个album文件夹
308-
dir_zip_dict[dir_path] = zip_path
309-
# 也要删除album下的photo文件夹
310-
for d in files_of_dir(dir_path):
311-
dir_zip_dict[d] = None
305+
self.zip_album(album, photo_dict, zip_path, dir_zip_dict)
312306

313307
elif level == 'photo':
314308
for photo, image_list in photo_dict.items():
315309
zip_path = self.get_zip_path(None, photo, filename_rule, suffix, zip_dir)
316-
dir_path = self.zip_photo(photo, image_list, zip_path)
317-
if dir_path is not None:
318-
dir_zip_dict[dir_path] = zip_path
310+
self.zip_photo(photo, image_list, zip_path, dir_zip_dict)
319311

320312
else:
321313
ExceptionTool.raises(f'Not Implemented Zip Level: {level}')
322314

323315
self.after_zip(dir_zip_dict)
324316

325-
def zip_photo(self, photo, image_list: list, zip_path: str) -> Optional[str]:
317+
def zip_photo(self, photo, image_list: list, zip_path: str, dir_zip_dict) -> Optional[str]:
326318
"""
327319
压缩photo文件夹
328320
:returns: photo文件夹路径
@@ -333,50 +325,58 @@ def zip_photo(self, photo, image_list: list, zip_path: str) -> Optional[str]:
333325

334326
all_filepath = set(map(lambda t: self.unified_path(t[0]), image_list))
335327

336-
return self.do_zip(photo_dir,
337-
zip_path,
338-
all_filepath,
339-
f'压缩章节[{photo.photo_id}]成功 → {zip_path}',
340-
)
328+
if len(all_filepath) == 0:
329+
self.log('无下载文件,无需压缩', 'skip')
330+
return None
331+
332+
from common import backup_dir_to_zip
333+
backup_dir_to_zip(
334+
photo_dir,
335+
zip_path,
336+
acceptor=lambda f: os.path.isdir(f) or self.unified_path(f) in all_filepath
337+
).close()
338+
339+
self.log(f'压缩章节[{photo.photo_id}]成功 → {zip_path}', 'finish')
340+
dir_zip_dict[self.unified_path(photo_dir)] = zip_path
341341

342342
@staticmethod
343343
def unified_path(f):
344344
return fix_filepath(f, os.path.isdir(f))
345345

346-
def zip_album(self, album, photo_dict: dict, zip_path) -> Optional[str]:
346+
def zip_album(self, album, photo_dict: dict, zip_path, dir_zip_dict) -> Optional[str]:
347347
"""
348348
压缩album文件夹
349349
:returns: album文件夹路径
350350
"""
351-
all_filepath: Set[str] = set()
352-
353-
def addpath(f):
354-
all_filepath.update(set(f))
355351

356-
album_dir = self.option.decide_album_dir(album)
357-
# addpath(self.option.decide_image_save_dir(photo) for photo in photo_dict.keys())
358-
addpath(path for ls in photo_dict.values() for path, _ in ls)
352+
# 所有下载了的图片文件的路径
353+
all_filepath: Set[str] = set(path for ls in photo_dict.values() for path, _ in ls)
359354

360-
return self.do_zip(album_dir,
361-
zip_path,
362-
all_filepath,
363-
msg=f'压缩本子[{album.album_id}]成功 → {zip_path}',
364-
)
365-
366-
def do_zip(self, source_dir, zip_path, all_filepath, msg):
367355
if len(all_filepath) == 0:
368356
self.log('无下载文件,无需压缩', 'skip')
369-
return None
357+
return
370358

359+
# 该本子的所有章节的图片所在文件夹
360+
photo_dir_list = [self.option.decide_image_save_dir(photo) for photo in photo_dict.keys()]
361+
362+
# 压缩文件对象
371363
from common import backup_dir_to_zip
372-
backup_dir_to_zip(
373-
source_dir,
374-
zip_path,
375-
acceptor=lambda f: os.path.isdir(f) or self.unified_path(f) in all_filepath
376-
).close()
364+
import zipfile
365+
zfile = zipfile.ZipFile(zip_path, 'w')
366+
367+
for photo_dir in photo_dir_list:
368+
photo_dir = self.unified_path(photo_dir)
369+
backup_dir_to_zip(
370+
photo_dir,
371+
zip_path,
372+
zfile=zfile,
373+
prefix=os.path.basename(photo_dir.rstrip('/')),
374+
acceptor=lambda f: os.path.isdir(f) or self.unified_path(f) in all_filepath
375+
)
376+
dir_zip_dict[photo_dir] = zip_path
377377

378-
self.log(msg, 'finish')
379-
return self.unified_path(source_dir)
378+
zfile.close()
379+
self.log(f'压缩本子[{album.album_id}]成功 → {zip_path}', 'finish')
380380

381381
def after_zip(self, dir_zip_dict: Dict[str, Optional[str]]):
382382
# 删除所有原文件

0 commit comments

Comments
 (0)