mirror of
https://github.com/hect0x7/JMComic-Crawler-Python.git
synced 2025-09-26 22:31:30 +08:00
v2.2.6: 优化zip插件提高可扩展性,增加测试用例 (#129)
This commit is contained in:
parent
cd8952c7ba
commit
2d23306ade
4
.github/workflows/test.yml
vendored
4
.github/workflows/test.yml
vendored
@ -1,12 +1,14 @@
|
||||
name: 跑测试
|
||||
|
||||
on:
|
||||
schedule:
|
||||
- cron: "0 8 * * *"
|
||||
workflow_dispatch:
|
||||
push:
|
||||
branches: [ "dev" ]
|
||||
paths:
|
||||
- 'src/**/*.py'
|
||||
- 'test/**/*.py'
|
||||
- 'tests/**/*.py'
|
||||
- '.github/workflows/test.yml'
|
||||
- 'assets/config/option_test.yml'
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
# 被依赖方 <--- 使用方
|
||||
# config <--- entity <--- toolkit <--- client <--- option <--- downloader
|
||||
|
||||
__version__ = '2.2.5'
|
||||
__version__ = '2.2.6'
|
||||
|
||||
from .api import *
|
||||
from .jm_plugin import *
|
||||
|
@ -186,6 +186,7 @@ class FindUpdatePlugin(JmOptionPlugin):
|
||||
class ZipPlugin(JmOptionPlugin):
|
||||
plugin_key = 'zip'
|
||||
|
||||
# noinspection PyAttributeOutsideInit
|
||||
def invoke(self,
|
||||
album: JmAlbumDetail,
|
||||
downloader,
|
||||
@ -198,49 +199,39 @@ class ZipPlugin(JmOptionPlugin):
|
||||
|
||||
from .jm_downloader import JmDownloader
|
||||
downloader: JmDownloader
|
||||
self.downloader = downloader
|
||||
self.level = level
|
||||
self.delete_original_file = delete_original_file
|
||||
|
||||
# 确保压缩文件所在文件夹存在
|
||||
zip_dir = JmcomicText.parse_to_abspath(zip_dir)
|
||||
mkdir_if_not_exists(zip_dir)
|
||||
|
||||
# 原文件夹 -> zip文件
|
||||
dir_zip_dict = {}
|
||||
photo_dict = downloader.all_downloaded[album]
|
||||
original_file_dir_list = []
|
||||
|
||||
if level == 'album':
|
||||
zip_path = self.get_zip_path(album, None, filename_rule, suffix, zip_dir)
|
||||
original_file_dir_list.append(
|
||||
self.zip_album(album, photo_dict, zip_path)
|
||||
)
|
||||
dir_path = self.zip_album(album, photo_dict, zip_path)
|
||||
dir_zip_dict[dir_path] = zip_path
|
||||
|
||||
elif level == 'photo':
|
||||
# dst_file_list = []
|
||||
|
||||
for photo, image_list in photo_dict.items():
|
||||
zip_path = self.get_zip_path(None, photo, filename_rule, suffix, zip_dir)
|
||||
original_file_dir_list.append(
|
||||
self.zip_photo(photo, image_list, zip_path)
|
||||
)
|
||||
# dst_file_list.append(zip_path)
|
||||
dir_path = self.zip_photo(photo, image_list, zip_path)
|
||||
dir_zip_dict[dir_path] = zip_path
|
||||
|
||||
else:
|
||||
raise NotImplementedError(f'level: {level}')
|
||||
|
||||
if delete_original_file is True:
|
||||
self.delete_all_files(downloader.all_downloaded, original_file_dir_list)
|
||||
|
||||
# noinspection PyMethodMayBeStatic
|
||||
def delete_all_files(self, all_downloaded: dict, dir_list: List[str]):
|
||||
import os
|
||||
for album, photo_dict in all_downloaded.items():
|
||||
for photo, image_list in photo_dict.items():
|
||||
for f, image in image_list:
|
||||
os.remove(f)
|
||||
jm_debug('plugin.zip.remove', f'移除原文件: {f}')
|
||||
|
||||
for d in dir_list:
|
||||
if len(files_of_dir(d)) == 0:
|
||||
os.removedirs(d)
|
||||
jm_debug('plugin.zip.remove', f'移除文件夹: {d}')
|
||||
self.after_zip(dir_zip_dict)
|
||||
|
||||
def zip_photo(self, photo, image_list: list, zip_path: str):
|
||||
"""
|
||||
压缩photo文件夹
|
||||
@return: photo文件夹路径
|
||||
"""
|
||||
photo_dir = self.option.decide_image_save_dir(photo) \
|
||||
if len(image_list) == 0 \
|
||||
else os.path.dirname(image_list[0][0])
|
||||
@ -257,6 +248,10 @@ class ZipPlugin(JmOptionPlugin):
|
||||
return photo_dir
|
||||
|
||||
def zip_album(self, album, photo_dict: dict, zip_path):
|
||||
"""
|
||||
压缩album文件夹
|
||||
@return: album文件夹路径
|
||||
"""
|
||||
album_dir = self.option.decide_album_dir(album)
|
||||
all_filepath: Set[str] = set()
|
||||
|
||||
@ -279,8 +274,19 @@ class ZipPlugin(JmOptionPlugin):
|
||||
jm_debug('plugin.zip.finish', f'压缩本子[{album.album_id}]成功 → {zip_path}')
|
||||
return album_dir
|
||||
|
||||
def after_zip(self, dir_zip_dict: Dict[str, str]):
|
||||
# 是否要删除所有原文件
|
||||
if self.delete_original_file is True:
|
||||
self.delete_all_files_and_empty_dir(
|
||||
all_downloaded=self.downloader.all_downloaded,
|
||||
dir_list=list(dir_zip_dict.keys())
|
||||
)
|
||||
|
||||
# noinspection PyMethodMayBeStatic
|
||||
def get_zip_path(self, album, photo, filename_rule, suffix, zip_dir):
|
||||
"""
|
||||
计算zip文件的路径
|
||||
"""
|
||||
filename = DirRule.apply_rule_directly(album, photo, filename_rule)
|
||||
from os.path import join
|
||||
return join(
|
||||
@ -288,6 +294,23 @@ class ZipPlugin(JmOptionPlugin):
|
||||
filename + fix_suffix(suffix),
|
||||
)
|
||||
|
||||
# noinspection PyMethodMayBeStatic
|
||||
def delete_all_files_and_empty_dir(self, all_downloaded: dict, dir_list: List[str]):
|
||||
"""
|
||||
删除所有文件和文件夹
|
||||
"""
|
||||
import os
|
||||
for album, photo_dict in all_downloaded.items():
|
||||
for photo, image_list in photo_dict.items():
|
||||
for f, image in image_list:
|
||||
os.remove(f)
|
||||
jm_debug('plugin.zip.remove', f'删除原文件: {f}')
|
||||
|
||||
for d in dir_list:
|
||||
if len(os.listdir(d)) == 0:
|
||||
os.removedirs(d)
|
||||
jm_debug('plugin.zip.remove', f'删除文件夹: {d}')
|
||||
|
||||
|
||||
JmModuleConfig.register_plugin(JmLoginPlugin)
|
||||
JmModuleConfig.register_plugin(UsageLogPlugin)
|
||||
|
38
tests/test_jmcomic/test_jm_custom.py
Normal file
38
tests/test_jmcomic/test_jm_custom.py
Normal file
@ -0,0 +1,38 @@
|
||||
from test_jmcomic import *
|
||||
|
||||
|
||||
class Test_Custom(JmTestConfigurable):
|
||||
|
||||
def test_custom_entity(self):
|
||||
"""
|
||||
测试自定义属性
|
||||
"""
|
||||
dic = {1: 'd', 2: 'e'}
|
||||
|
||||
class MyAlbum(JmAlbumDetail):
|
||||
|
||||
@property
|
||||
def aname(self):
|
||||
return dic[int(self.album_id)]
|
||||
|
||||
class MyPhoto(JmPhotoDetail):
|
||||
|
||||
@property
|
||||
def pname(self):
|
||||
return dic[int(self.photo_id)]
|
||||
|
||||
JmModuleConfig.CLASS_ALBUM = MyAlbum
|
||||
JmModuleConfig.CLASS_PHOTO = MyPhoto
|
||||
|
||||
base_dir: str = workspace()
|
||||
dir_rule = DirRule('Bd_Aaname_Ppname', base_dir)
|
||||
# noinspection PyTypeChecker
|
||||
save_dir = dir_rule.deside_image_save_dir(
|
||||
MyAlbum('1', *['0'] * 13),
|
||||
MyPhoto('2', *['0'] * 7)
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
os.path.abspath(save_dir),
|
||||
os.path.abspath(base_dir + dic[1] + '/' + dic[2]),
|
||||
)
|
Loading…
Reference in New Issue
Block a user