This commit is contained in:
caiwx86 2023-02-14 02:56:19 +08:00
parent 2c275ebb00
commit 62006db439
5 changed files with 45 additions and 8 deletions

View File

@ -6,6 +6,7 @@ from zipfile import ZipFile
from utils.comic.ComicInfo import comicInfo
from utils.Ntfy import ntfy
from utils.FileUtils import fileUtils
from utils.comic.PathStr import pathStr
class CBZUtils:
@ -148,3 +149,32 @@ class verUtils:
except:
ntfy(f"删除失败 {cbz_path}")
return is_next
@classmethod
def existsUnzipCBZ(cls,filesname):
result = False
unzip_base_path = pathStr.base_unzip_path
zipfile_path = os.path.join(unzip_base_path,comicInfo.str_comic_name,comicInfo.str_chapter+".CBZ")
#判断是否存在已下载CBZ文件
if os.path.exists(zipfile_path) and not os.path.exists(CBZUtils.getCBZ_Path()):
print(f"存在CBZ文件{zipfile_path},解压中")
zip_file = ZipFile(zipfile_path)
#CBZ中文件数量剔除ComicInfo.xml
info_list = zip_file.namelist()
if len(filesname) == len(zip_file.namelist())-1:
unzip_path = comicInfo.getDirComicChapter()
zip_file.extractall(unzip_path)
zip_file.close()
print(f"解压完成: CBZ文件{zipfile_path}")
print(f"文件校验中")
for file in os.listdir(unzip_path):
if file.endswith(".jpg") and not fileUtils.ver_file(os.path.join(unzip_path,file),type="image"):
#清空文件
try:
shutil.rmtree(unzip_path)
except Exception as e:
print(e)
return False
comicInfo.writeComicInfoXML(overlay=True)
result = True
return result

View File

@ -207,19 +207,25 @@ class fileUtils:
@classmethod
def ver_file(cls,file_path,type):
time.sleep(0.1)
if type == "image" and int(os.path.getsize(file_path)) > 100:
file_size = os.path.getsize(file_path)
if type == "image" and int(file_size) > 100:
#验证是否是图像
try:
img = Image.open(file_path)
img.verify()
print(f"{file_path} 类型为type:{type} size: {file_size}")
return True
except:
if os.path.exists(file_path):
os.remove(file_path)
print(f"{file_path}已损坏 type:{type},删除重试中")
print(f"{file_path}已损坏 type:{type} size: {file_size},删除重试中")
return False
else:
print(f"文件小于100b,{file_path}")
print(f"文件小于100b删除中,{file_path} size: {file_size}")
try:
os.remove(file_path)
except Exception as e:
print(e)
return False
@classmethod

View File

@ -139,8 +139,9 @@ class baseComic:
ci.writeComicInfoXML(chapter_name)
ntfy.sendMsg(f"{book_name} {chapter_name} 下载中")
is_next = verUtils.verNextCBZ(list_img)
is_unzip = verUtils.existsUnzipCBZ(files_name)
repeat = 1
while not is_next and repeat <= 2:
while not is_next and repeat <= 2 and not is_unzip:
download_images(list_img,ci.getDirComicChapter(), files_name=files_name,concurrency=10,timeout=60)
is_next = len(",".join(os.listdir(ci.getDirComicChapter())).split(".jpg"))-1 == len(list_img)
if not is_next:

View File

@ -312,7 +312,7 @@ class comicInfo():
return cls.path_comic_info
@classmethod
def writeComicInfoXML(cls,chapter,path=None):
def writeComicInfoXML(cls,chapter=None,path=None,overlay=False):
root = cls.root_node()
new_document = Document()
new_document.appendChild(root)
@ -334,7 +334,7 @@ class comicInfo():
if path != None: cls.path_comic_info = os.path.join(path,cls.COMIC_INFO_XML)
base_dir = os.path.dirname(cls.path_comic_info)
if not os.path.exists(base_dir): os.makedirs(base_dir)
if os.path.exists(cls.path_comic_info):
if os.path.exists(cls.path_comic_info) and not overlay:
print(f"{cls.COMIC_INFO_XML} 已存在")
return None
with open(cls.path_comic_info , "w", encoding="utf-8") as fo:

View File

@ -9,7 +9,7 @@ class pathStr:
comic_url_main = None
base_comic_out = os.path.join("/mnt", "Comics")
base_unzip_path = os.path.join("/mnt","OldComics")
@classmethod
def base_cbz(cls): return cls.getBaseComicPath("CBZ")
@classmethod