PyComicPackRouMan/utils/CBZUtils.py
2022-12-20 19:04:36 +08:00

72 lines
2.7 KiB
Python

import os
from pathlib import Path
from zipfile import ZipFile
from utils.comic.ComicInfo import comicInfo
from utils.Ntfy import ntfy
class CBZUtils:
@classmethod
def readDirsOrFiles(cls,dir,type):
data = []
files = os.listdir(dir)
for file in files:
path = os.path.join(dir,file)
if type == "files" and os.path.isfile(path):
data.append(path)
if type == "dirs" and os.path.isdir(path):
data.append(path)
return data
@classmethod
def zip_compression(cls,source_dir, target_file):
target_dir = os.path.dirname(target_file)
if not os.path.exists(target_dir):
os.makedirs(target_dir)
if not os.path.exists(target_file):
with ZipFile(target_file, mode='w') as zf:
for path, dir_names, filenames in os.walk(source_dir):
path = Path(path)
arc_dir = path.relative_to(source_dir)
y = 0
for filename in filenames:
y = y + 1
print("打包中:" + str(y) + "/" + str(len(filenames)), os.path.join(source_dir, filename))
zf.write(path.joinpath(filename), arc_dir.joinpath(filename))
zf.close()
ntfy.sendMsg(f"打包完成:{target_file}")
#md5_file = md5(target_file)
#print("md5:", md5_file)
#msg[target_file] = md5_file
return len(filenames)
else:
print("文件已存在:", target_file)
@classmethod
def packAutoComicChapterCBZ(cls):
chapter_path = comicInfo.getDirComicChapter()
packCBZ_path = comicInfo.getDirCBZComicChapter()
if os.path.exists(chapter_path):
dirs = os.listdir(chapter_path)
for file in dirs:
if file.startswith("scramble="):
try:
os.remove(file)
except:
print(f"删除 {file} 发生错误,已跳过")
cbz_size = cls.zip_compression(chapter_path,packCBZ_path+".CBZ")
chapter_img_size = len(comicInfo.getChapterImgs())
if cbz_size - 1 == chapter_img_size:
comicInfo.nextCBZToDoneChapter()
else:
cbz_path = comicInfo.nextSavePath("cbz_")
ntfy.sendMsg(f"{cbz_path} 数据不完整 删除配置文件中")
if os.path.exists(cbz_path):
try:
os.remove(cbz_path)
ntfy.sendMsg(f"删除成功 {cbz_path}")
except:
ntfy.sendMsg(f"删除失败 {cbz_path}")