143 lines
5.2 KiB
Python
143 lines
5.2 KiB
Python
import json
|
||
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
|
||
else:
|
||
print("文件已存在:", target_file)
|
||
return len(filenames)
|
||
|
||
@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} 发生错误,已跳过")
|
||
try:
|
||
size_zip = cls.zip_compression(chapter_path,packCBZ_path+".CBZ")
|
||
size_imgs = len(comicInfo.getChapterImgs())
|
||
if size_zip -1 == size_imgs:
|
||
ntfy.sendMsg(f"打包校验成功: {packCBZ_path}")
|
||
comicInfo.nextCBZToDoneChapter()
|
||
return True
|
||
else:
|
||
ntfy.sendMsg(f"打包检验不完整:{packCBZ_path}")
|
||
return False
|
||
|
||
except:
|
||
print("")
|
||
#ntfy.sendMsg("CBZ打包失败")
|
||
|
||
@classmethod
|
||
def zip_info(cls,zip_path=None):
|
||
pack_cbz_path = comicInfo.getDirCBZComicChapter()
|
||
if zip_path == None:
|
||
zip_path = pack_cbz_path+".CBZ"
|
||
data = None
|
||
if os.path.exists(zip_path):
|
||
with ZipFile(zip_path, "r") as zfile:
|
||
data = zfile.namelist()
|
||
zfile.close()
|
||
return data
|
||
|
||
class verUtils:
|
||
@classmethod
|
||
def verCBZ(cls,data=None):
|
||
zip_info = CBZUtils.zip_info()
|
||
if zip_info != None:
|
||
len_zip = len(zip_info) - 1
|
||
else:
|
||
len_zip = 0
|
||
info_data = None
|
||
if data == None:
|
||
info_path = comicInfo.nextSavePath("done_")
|
||
try:
|
||
with open(info_path,"r",encoding="utf-8") as fs:
|
||
info_data = json.loads(fs.read())
|
||
fs.close()
|
||
except:
|
||
ntfy.sendMsg("校验失败")
|
||
else:
|
||
info_data = data
|
||
if info_data != None:
|
||
if len(info_data) == len_zip:
|
||
return True
|
||
else:
|
||
ntfy.sendMsg("数据不完整,删除配置文件中")
|
||
try:
|
||
os.remove(comicInfo.nextSavePath("done_"))
|
||
ntfy.sendMsg("配置文件删除成功")
|
||
except:
|
||
ntfy.sendMsg("配置文件删除失败")
|
||
return False
|
||
else:
|
||
ntfy.sendMsg("info_data 为空")
|
||
return False
|
||
|
||
@classmethod
|
||
def verNextCBZ(cls,list_img):
|
||
#验证数据是已存在且是否完整
|
||
cbz_path = comicInfo.getDirCBZComicChapter()+".CBZ"
|
||
is_next = False
|
||
if os.path.exists(cbz_path):
|
||
try:
|
||
cbz_size = len(CBZUtils.zip_info(cbz_path)) - 1
|
||
except:
|
||
cbz_size = 0
|
||
if len(list_img) == cbz_size:
|
||
ntfy.sendMsg(f"{comicInfo.getComicName()} {comicInfo.getChapter()} 数据完整,已跳过")
|
||
is_next = True
|
||
else:
|
||
ntfy.sendMsg(f"{comicInfo.getComicName()} {comicInfo.getChapter()} 数据不完整,尝试删除配置CBZ文件后重试")
|
||
try:
|
||
if cbz_size < len(list_img) or os.path.getsize(cbz_path) < 300000:
|
||
ntfy.sendMsg(f"删除 {cbz_path}")
|
||
os.remove(cbz_path)
|
||
else:
|
||
is_next = True
|
||
except:
|
||
ntfy(f"删除失败 {cbz_path}")
|
||
return is_next |