From 4667afe295178acae5c78553a9f053623ae6b63f Mon Sep 17 00:00:00 2001 From: caiwx86 Date: Tue, 14 Feb 2023 05:54:44 +0800 Subject: [PATCH] fix --- utils/CBZUtils.py | 72 +++++++++++++++++++++++++++-------- utils/FileUtils.py | 8 ++-- utils/base/BaseComicEntity.py | 26 ++++--------- 3 files changed, 67 insertions(+), 39 deletions(-) diff --git a/utils/CBZUtils.py b/utils/CBZUtils.py index fc2f95b..f2be460 100644 --- a/utils/CBZUtils.py +++ b/utils/CBZUtils.py @@ -45,18 +45,7 @@ class CBZUtils: zf.write(path.joinpath(filename), arc_dir.joinpath(filename)) zf.close() ntfy.sendMsg(f"打包完成:{target_file}") - else: - print("文件已存在:", target_file) - len_chapter_imgs = len(comicInfo.getChapterImgs()) - if len_chapter_imgs != 0 and len_chapter_imgs != len(cls.zip_info(target_file)): - try: - os.remove(target_file) - comicInfo.setProgress(comicInfo.PROGRESS_NONE) - except Exception as e: - print(e) - if remove: - filenames.remove(comicInfo.COMIC_ICON_NAME+".jpg") - filenames.remove(comicInfo.COMIC_INFO_XML) + cls.verCBZComic(target_file) return filenames @classmethod @@ -72,9 +61,7 @@ class CBZUtils: print(f"删除 {file} 发生错误,已跳过") cls.zip_compression(comicInfo.getDirComicChapter(), cls.getCBZ_Path()) time.sleep(0.1) - fileUtils.remove(comicInfo.getDirComicChapter()) - cls.zip_compression(comicInfo.getDirComicChapter(), cls.getCBZ_Path()) - verUtils.verNextCBZ() + fileUtils.remove(comicInfo.getDirComic()) @classmethod def replaceZip(cls,filepath,unpack_dir=None): @@ -127,9 +114,62 @@ class CBZUtils: print(e) return result + @classmethod + def verCBZComic(cls,path=None,list_img=None): + if path == None: + path = cls.getCBZ_Path() + if list_img == None: + list_img = comicInfo.getChapterImgs() + if not os.path.exists(path): return False + if os.path.exists(path) and len(cls.zip_info(path)) == len(list_img): + print(f"文件校验成功:{path}") + comicInfo.setProgress(comicInfo.PROGRESS_DONE) + return True + else: + try: + print(f"文件删除中:{path}") + os.remove(path) + comicInfo.setProgress(comicInfo.PROGRESS_NONE) + except Exception as e: + print(e) + return False + return False + + @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 + 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 + + @classmethod + def nextCBZ(cls,list_img=None): + return not cls.verCBZComic(list_img=list_img) + + class verUtils: @classmethod - def verNextCBZ(cls,list_img=None): + def depverNextCBZ(cls,list_img=None): #验证数据是已存在且是否完整 if list_img == None: comicInfo.getChapterImgs() diff --git a/utils/FileUtils.py b/utils/FileUtils.py index 7b8cc7e..640bc8b 100644 --- a/utils/FileUtils.py +++ b/utils/FileUtils.py @@ -9,19 +9,19 @@ class imageUtils: @classmethod def encodeImage(cls,str_en): - print("en",str_en) + #print("en",str_en) enc = base64.b64decode(str_en) - print("解密:",enc) + #print("解密:",enc) m = hashlib.md5() m.update(enc) md5 = m.digest() d = md5[-1] - print(md5) + #print(md5) try: blocks = d % 10 + 5 except: blocks = 0 %10 + 5 - print("blocks=",blocks) + #print("blocks=",blocks) return blocks @classmethod diff --git a/utils/base/BaseComicEntity.py b/utils/base/BaseComicEntity.py index fd9eb8b..8e7550e 100644 --- a/utils/base/BaseComicEntity.py +++ b/utils/base/BaseComicEntity.py @@ -66,8 +66,7 @@ class baseComic: 读取某章节下所有图片 ''' @classmethod - def comicChapter(cls,chapter_url,scramble=None,sleep=None): - is_next = True + def comicChapter(cls,chapter_url,scramble=None,sleep=None,is_next=True): try: is_next = cls.Onechapter(chapter_url,scramble) #进入下个阶段 @@ -78,7 +77,7 @@ class baseComic: except Exception as e: is_next = ntfy.sendMsg(f"{ci.getComicName()} 下载出错了",error=e) ntfy.sendMsg(f"预计总章节大小:{cls.count_chapter + 1} / "+ str(ci.getLenChapters())) ci.setChapterIndex(cls.count_chapter + 1) - if sleep != None and is_next == True: ntfy.sendMsg(f"{sleep} 秒后开始下载下一个章节",sleep=sleep) + if sleep != None and is_next: ntfy.sendMsg(f"{sleep} 秒后开始下载下一个章节",sleep=sleep) @classmethod @@ -120,31 +119,20 @@ class baseComic: cbz_path = ci.getNewCBZComicChapter()+".CBZ" #更新Icon ci.getNewIconComicChapter() - if os.path.exists(cbz_path): - if CBZUtils.PackCBZAndequZipLength: - ntfy.sendMsg(f"{book_name} {chapter_name} 数据完整,已跳过") - ci.nextDoneSave(list_img) - is_next = False - else: - ntfy.sendMsg(f"{book_name} {chapter_name} 数据不完整,尝试删除配置CBZ文件后重试") - try: - if os.path.getsize(cbz_path) < 300000: fileUtils.remove(cbz_path) - else: is_next = False - except: - ntfy(f"删除失败 {cbz_path}") + if os.path.exists(cbz_path): CBZUtils.verCBZComic(cbz_path) if is_next: if not os.path.exists(ci.getPathComicInfoXML()): #print("不存在ComicInfo.xml 生成中...") ci.setPages(files_name) ci.writeComicInfoXML(chapter_name) ntfy.sendMsg(f"{book_name} {chapter_name} 下载中") - is_next = verUtils.verNextCBZ(list_img) - is_unzip = verUtils.existsUnzipCBZ(files_name) + is_next = CBZUtils.nextCBZ(list_img) + is_unzip = CBZUtils.existsUnzipCBZ(files_name) repeat = 1 - while not is_next and repeat <= 2 and not is_unzip: + while 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: + if is_next: sleep_time = 3+int(repeat)*2 time.sleep(sleep_time) ntfy.sendMsg(f"下载数据(不完整,{sleep_time}秒钟后尝试第{repeat}次")