import json,os,time,random,shutil from utils.HtmlUtils import htmlUtils from utils.FileUtils import imageUtils from utils.comic.ComicInfo import comicInfo from utils.CBZUtils import CBZUtils from utils.downloader import download_images from utils.downloader import download_comic_icon from utils.Ntfy import ntfy from utils.CBZUtils import verUtils from entity.down.RouMan import comicCommon as RouManComicCommon from entity.down.JM import comicCommon as JMComicCommon from entity.down.BaoZi import comicCommon as BaoZiComicCommon from utils.comic.PathStr import pathStr class baseComic: count_chapter = 0 @classmethod def downladsComcis(cls,book_name,comic_href,updated=None): comicInfo.setComicName(book_name) if updated != None: comicInfo.setUpdateAt(updated) random_int = random.randint(5,20) comicInfo.setComicName(book_name) dir_conf_comic = comicInfo.getDirConfComic() if comicInfo.getIsComicNameSkips(book_name): print(f"{book_name} 在白名单中,已跳过") return None if not os.path.exists(dir_conf_comic): ntfy.sendMsg(f"{random_int}秒后开始下载 漫画:{book_name}") time.sleep(random_int) else: ntfy.sendMsg(f"已存在 漫画:{book_name}") if comicInfo.isUpdateComic(): return comic_href else: ntfy.sendMsg(f"{book_name} 已是最新") chapters = htmlUtils.xpathData('//div[contains(@class,"bookid_chapterBox")]//div[contains(@class,"bookid_chapter")]/a/text()',url=comic_href,update=False) chapter_index = 1 for chapter in chapters: comicInfo.setChapterIndex(chapter_index) comicInfo.setChapterName(chapter) cbz_path = comicInfo.getNewCBZComicChapter("file") icon_path = comicInfo.getNewIconComicChapter("file") CBZUtils.replaceZip(cbz_path) if comicInfo.nextExistsGetPath("done_") and not os.path.exists(cbz_path): comicInfo.nextExistsGetPath("done_",remove=True) if not os.path.exists(cbz_path): comicInfo.updateComicDate("0") return comic_href else: cover_icon_path = os.path.join(comicInfo.getDirConfComic(),"cover.jpg") if not os.path.exists(icon_path) and os.path.exists(cover_icon_path): shutil.copy(cover_icon_path,icon_path) print(f"{cover_icon_path} copy 至:{icon_path}") chapter_index = chapter_index + 1 return None #print(books) #for comicHref in comicsHref: # cls.oneComic(comicHref,random.uniform(10,20)) @classmethod def oneComic(cls,url,title,author,icon,tags,dep,chapters,chapter_href,alias=None,genre=None,lang="zh",sleep=None): author = str(author).replace("&",",").replace(" ",",") comicInfo.setHomePage(url) comicInfo.setComicName(str(title)) if alias != None: comicInfo.setComicNames(title+","+alias) comicInfo.setAuthor(author) comicInfo.setIcon(icon) comicInfo.setTags(tags) comicInfo.setDep(dep) #comicInfo.setCBS("韩漫") if genre != None: comicInfo.setGenre(genre) comicInfo.setLang(lang) comicInfo.setListChapter(chapters) #comicUtils.setComic(title,alias,icon,author,tags,action,dep,update_date,chapters,chapter_href) cls.count_chapter = 0 for href in chapter_href: chapter = chapters[cls.count_chapter] comicInfo.setChapterIndex(cls.count_chapter+1) comicInfo.setChapterName(chapter) if comicInfo.nextExistsGetPath("done_") and not os.path.exists(comicInfo.getNewCBZComicChapter("file")): comicInfo.nextExistsGetPath("done_",remove=True) if not comicInfo.nextExistsGetPath("done_"): cls.comicChapter(href,scramble=True,sleep=random.randint(5,15)) #存在就校验CBZ包是否完整 if comicInfo.nextExistsGetPath("done_"): verUtils.verCBZ() cls.count_chapter += 1 #一本漫画下载后等待 #清空文件夹 path_dir_comic = comicInfo.getDirComic() if os.path.exists(path_dir_comic): shutil.rmtree(path_dir_comic) if sleep != None: time.sleep(sleep) ''' 读取某章节下所有图片 ''' @classmethod def comicChapter(cls,chapter_url,scramble=None,sleep=None): is_next = True try: is_next = cls.Onechapter(chapter_url,scramble) #进入下个阶段 if comicInfo.nextExistsGetPath("down_"): #章节图片全部下载后,调用下载封面 download_comic_icon() #下个阶段 if comicInfo.nextExistsGetPath("cbz_"): time.sleep(0.1) #下载后自动打包 is_next = CBZUtils.packAutoComicChapterCBZ() #完成删除原文件 remove_path = comicInfo.getDirComicChapter() if os.path.exists(remove_path): shutil.rmtree(remove_path) print(f"文件已删除: {remove_path}") except Exception as e: print(e) ntfy.sendMsg(f"{comicInfo.getComicName()} 下载出错了") is_next = False ntfy.sendMsg(f"预计总章节大小:{cls.count_chapter + 1} / "+ str(comicInfo.getLenChapters())) comicInfo.setChapterIndex(cls.count_chapter + 1) if sleep != None and is_next == True: ntfy.sendMsg(f"{sleep} 秒后开始下载下一个章节") time.sleep(sleep) @classmethod def Onechapter(cls,chapter_url,scramble=None): is_next = True if not str(chapter_url).startswith("http"): chapter_url = comicInfo.getBaseUrl() + chapter_url try: is_next = cls.comicChapterDownload(chapter_url) except: htmlUtils.remove_HtmlCache(chapter_url) comicInfo.nextInfoToImgChapter() #下载完成后, 开始解密图片 chapter_dir = comicInfo.getDirComicChapter() if scramble and os.path.exists(chapter_dir): #获取章节图片路径 dirs = os.listdir(chapter_dir) for img in dirs: if img.startswith("scramble="): c_path = os.path.join(chapter_dir, img) #imageUtils.getScrambleImage(c_path) imageUtils.encode_scramble_image(c_path) #进入下一阶段 comicInfo.nextImgToDownloadChapter() return is_next @classmethod def comicChapterDownload(cls,url): comic_main = pathStr.getComicMain() if comic_main == pathStr.comic_jm: JMComicCommon.comicChapterDownload(url) if comic_main == pathStr.comic_bz: BaoZiComicCommon.comicChapterDownload(url) if comic_main == pathStr.comic_rm: RouManComicCommon.comicChapterDownload(url) if comic_main == None: print("comic_main为空,退出中...") exit() list_img = comicInfo.getChapterListImg() files_name = comicInfo.getChapterFilesName() #netUtils.downloadComicChapterImages(list_img,scrambles=list_scramble) chapter_name = comicInfo.getChapter() book_name = comicInfo.getComicName() comicInfo.setChapterImgs(list_img) #保存图像 comicInfo.nextSaveInfoChapter(chapter_name, list_img) #验证数据是已存在且是否完整 #cbz_path = comicInfo.getDirCBZComicChapter()+".CBZ" cbz_path = comicInfo.getNewCBZComicChapter()+".CBZ" #更新Icon comicInfo.getNewIconComicChapter() is_next = True 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"{book_name} {chapter_name} 数据完整,已跳过") comicInfo.nextDoneSave(list_img) is_next = False else: ntfy.sendMsg(f"{book_name} {chapter_name} 数据不完整,尝试删除配置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 = False except: ntfy(f"删除失败 {cbz_path}") if is_next: path_comic_info = comicInfo.getPathComicInfoXML() if not os.path.exists(path_comic_info): #print("不存在ComicInfo.xml 生成中...") comicInfo.setPages(files_name) comicInfo.writeComicInfoXML(chapter_name) ntfy.sendMsg(f"{book_name} {chapter_name} 下载中") is_next = verUtils.verNextCBZ(list_img) repeat = 1 while not is_next and repeat <= 2: download_images(list_img,comicInfo.getDirComicChapter(), files_name=files_name,timeout=20) file_imgs = os.listdir(comicInfo.getDirComicChapter()) count_jpg = ",".join(file_imgs).split(".jpg") is_next = len(count_jpg)-1 == len(list_img) if not is_next: sleep_time = 3+int(repeat)*2 time.sleep(sleep_time) ntfy.sendMsg(f"下载数据({len(count_jpg)-1}/{len(list_img)})不完整,{sleep_time}秒钟后尝试第{repeat}次") repeat += 1 return is_next