Skip to content

Commit

Permalink
v2.5.10: 修正zip插件在压缩本子时未正确判断章节路径的问题,优化插件配置的文档 (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
hect0x7 authored Apr 9, 2024
1 parent dec2d54 commit 1e465f7
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 70 deletions.
26 changes: 19 additions & 7 deletions assets/docs/sources/option_file_syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,10 @@ plugins:
- plugin: subscribe_album_update # 自动订阅本子并下载、发送邮件通知的插件
kwargs:
download_if_has_update: true
email_notify: # 见下【发送qq邮件插件】
msg_from: x
msg_to: x
password: x
email_notify: # 参数说明见下【发送qq邮件插件】
msg_from: aaa@qq.com
msg_to: aaa@qq.com
password: dkjlakdjlkas
title: album update !!!
content: album update !!!
album_photo_dict:
Expand All @@ -179,7 +179,14 @@ plugins:
- plugin: zip # 压缩文件插件
kwargs:
level: photo # 按照章节,一个章节一个压缩文件
# level 也可以配成 album,表示一个本子对应一个压缩文件,该压缩文件会包含这个本子的所有章节

filename_rule: Ptitle # 压缩文件的命名规则
# 请注意⚠ [https://github.com/hect0x7/JMComic-Crawler-Python/issues/223#issuecomment-2045227527]
# filename_rule和level有对应关系
# 如果level=[photo], filename_rule只能写Pxxx
# 如果level=[album], filename_rule只能写Axxx

zip_dir: D:/jmcomic/zip/ # 压缩文件存放的文件夹
delete_original_file: true # 压缩成功后,删除所有原文件和文件夹

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

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

after_photo:
- plugin: j2p # jpg图片合成为一个pdf插件
- plugin: j2p # 图片合并插件,可以将下载下来的jpg图片合成为一个pdf插件
# 请注意⚠ 该插件的使用前提是,下载下来的图片是jpg图片
# 因此,使用该插件前,需要有如下配置:(下载图片格式转为jpg,上文有解释过此配置)
# download:
# image:
# suffix: .jpg
kwargs:
pdf_dir: D:/pdf # pdf存放文件夹
filename_rule: Pid # pdf命名规则
Expand Down
2 changes: 1 addition & 1 deletion src/jmcomic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# 被依赖方 <--- 使用方
# config <--- entity <--- toolkit <--- client <--- option <--- downloader

__version__ = '2.5.9'
__version__ = '2.5.10'

from .api import *
from .jm_plugin import *
Expand Down
2 changes: 1 addition & 1 deletion src/jmcomic/jm_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ def new_postman(cls, session=False, **kwargs):
'postman': {
'type': 'cffi',
'meta_data': {
'impersonate': 'chrome',
'impersonate': 'chrome110',
'headers': None,
'proxies': None,
}
Expand Down
22 changes: 0 additions & 22 deletions src/jmcomic/jm_option.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,28 +236,6 @@ def decide_image_batch_count(self, photo: JmPhotoDetail):
def decide_photo_batch_count(self, album: JmAlbumDetail):
return self.download.threading.photo

def decide_album_dir(self, album: JmAlbumDetail) -> str:
"""
该方法目前仅在 plugin-zip 中使用,不建议外部调用
"""
dir_layer = []
dir_rule = self.dir_rule
for rule in dir_rule.rule_dsl.split('_'):
if rule == 'Bd':
dir_layer.append(dir_rule.base_dir)
continue

if rule[0] == 'A':
name = dir_rule.apply_rule_directly(album, None, rule)
dir_layer.append(name)

if rule[0] == 'P':
break

from os.path import join
# noinspection PyTypeChecker
return join(*dir_layer)

# noinspection PyMethodMayBeStatic
def decide_image_filename(self, image: JmImageDetail) -> str:
"""
Expand Down
78 changes: 39 additions & 39 deletions src/jmcomic/jm_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,27 +302,19 @@ def invoke(self,

if level == 'album':
zip_path = self.get_zip_path(album, None, filename_rule, suffix, zip_dir)
dir_path = self.zip_album(album, photo_dict, zip_path)
if dir_path is not None:
# 要删除这个album文件夹
dir_zip_dict[dir_path] = zip_path
# 也要删除album下的photo文件夹
for d in files_of_dir(dir_path):
dir_zip_dict[d] = None
self.zip_album(album, photo_dict, zip_path, dir_zip_dict)

elif level == 'photo':
for photo, image_list in photo_dict.items():
zip_path = self.get_zip_path(None, photo, filename_rule, suffix, zip_dir)
dir_path = self.zip_photo(photo, image_list, zip_path)
if dir_path is not None:
dir_zip_dict[dir_path] = zip_path
self.zip_photo(photo, image_list, zip_path, dir_zip_dict)

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

self.after_zip(dir_zip_dict)

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

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

return self.do_zip(photo_dir,
zip_path,
all_filepath,
f'压缩章节[{photo.photo_id}]成功 → {zip_path}',
)
if len(all_filepath) == 0:
self.log('无下载文件,无需压缩', 'skip')
return None

from common import backup_dir_to_zip
backup_dir_to_zip(
photo_dir,
zip_path,
acceptor=lambda f: os.path.isdir(f) or self.unified_path(f) in all_filepath
).close()

self.log(f'压缩章节[{photo.photo_id}]成功 → {zip_path}', 'finish')
dir_zip_dict[self.unified_path(photo_dir)] = zip_path

@staticmethod
def unified_path(f):
return fix_filepath(f, os.path.isdir(f))

def zip_album(self, album, photo_dict: dict, zip_path) -> Optional[str]:
def zip_album(self, album, photo_dict: dict, zip_path, dir_zip_dict) -> Optional[str]:
"""
压缩album文件夹
:returns: album文件夹路径
"""
all_filepath: Set[str] = set()

def addpath(f):
all_filepath.update(set(f))

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

return self.do_zip(album_dir,
zip_path,
all_filepath,
msg=f'压缩本子[{album.album_id}]成功 → {zip_path}',
)

def do_zip(self, source_dir, zip_path, all_filepath, msg):
if len(all_filepath) == 0:
self.log('无下载文件,无需压缩', 'skip')
return None
return

# 该本子的所有章节的图片所在文件夹
photo_dir_list = [self.option.decide_image_save_dir(photo) for photo in photo_dict.keys()]

# 压缩文件对象
from common import backup_dir_to_zip
backup_dir_to_zip(
source_dir,
zip_path,
acceptor=lambda f: os.path.isdir(f) or self.unified_path(f) in all_filepath
).close()
import zipfile
zfile = zipfile.ZipFile(zip_path, 'w')

for photo_dir in photo_dir_list:
photo_dir = self.unified_path(photo_dir)
backup_dir_to_zip(
photo_dir,
zip_path,
zfile=zfile,
prefix=os.path.basename(photo_dir.rstrip('/')),
acceptor=lambda f: os.path.isdir(f) or self.unified_path(f) in all_filepath
)
dir_zip_dict[photo_dir] = zip_path

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

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

0 comments on commit 1e465f7

Please sign in to comment.