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.comic.ComicInfo import comicInfo
from utils.Ntfy import ntfy from utils.Ntfy import ntfy
from utils.FileUtils import fileUtils from utils.FileUtils import fileUtils
from utils.comic.PathStr import pathStr
class CBZUtils: class CBZUtils:
@ -147,4 +148,33 @@ class verUtils:
is_next = True is_next = True
except: except:
ntfy(f"删除失败 {cbz_path}") ntfy(f"删除失败 {cbz_path}")
return is_next 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 @classmethod
def ver_file(cls,file_path,type): def ver_file(cls,file_path,type):
time.sleep(0.1) 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: try:
img = Image.open(file_path) img = Image.open(file_path)
img.verify() img.verify()
print(f"{file_path} 类型为type:{type} size: {file_size}")
return True return True
except: except:
if os.path.exists(file_path): if os.path.exists(file_path):
os.remove(file_path) os.remove(file_path)
print(f"{file_path}已损坏 type:{type},删除重试中") print(f"{file_path}已损坏 type:{type} size: {file_size},删除重试中")
return False return False
else: 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 return False
@classmethod @classmethod

View File

@ -139,8 +139,9 @@ class baseComic:
ci.writeComicInfoXML(chapter_name) ci.writeComicInfoXML(chapter_name)
ntfy.sendMsg(f"{book_name} {chapter_name} 下载中") ntfy.sendMsg(f"{book_name} {chapter_name} 下载中")
is_next = verUtils.verNextCBZ(list_img) is_next = verUtils.verNextCBZ(list_img)
is_unzip = verUtils.existsUnzipCBZ(files_name)
repeat = 1 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) 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) is_next = len(",".join(os.listdir(ci.getDirComicChapter())).split(".jpg"))-1 == len(list_img)
if not is_next: if not is_next:

View File

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

View File

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