145 lines
6.3 KiB
Python
145 lines
6.3 KiB
Python
import json,os,time,random,shutil
|
||
from utils.NetUtils import htmlUtils
|
||
from utils.FileUtils import imageUtils
|
||
from utils.ComicUtils import CBZUtils
|
||
from utils.NetUtils import downloadUtils
|
||
from utils.ComicUtils import ntfy
|
||
from utils.FileUtils import fileUtils as fu
|
||
from domain.Domains import domains
|
||
from common.ComicInfo import ComicInfoUtils as ciUtils
|
||
from common.ComicInfo import ComicInfo as ci
|
||
from common.Comic import Comic
|
||
from common.Comic import ListComic
|
||
from common.Constant import ComicPath
|
||
|
||
#中心框架
|
||
class baseComic:
|
||
count_chapter = 0
|
||
|
||
#校验该漫画是否为最新
|
||
# Y/跳过 N/下载 返回下载链接
|
||
@classmethod
|
||
def updateComics(cls,chapters_xpath):
|
||
comics = ListComic.getListComicsLinksUpdateAt()
|
||
if comics == None: return False
|
||
(book_name,comic_href,updated) = [comics[0],comics[1],comics[2]]
|
||
cls.updateOneComic(book_name,comic_href,updated,chapters_xpath)
|
||
return True
|
||
|
||
@classmethod
|
||
def updateOneComic(cls,book_name,comic_href,update_at,chapters_xpath):
|
||
#白名单跳过
|
||
if ciUtils.getIsComicNameSkips(book_name): return None
|
||
#添加
|
||
Comic.setComicName(book_name)
|
||
Comic.setUpdateAt(update_at)
|
||
Comic.setCurrentDownLink(None)
|
||
if not ciUtils.isUpdateComic():
|
||
ntfy.sendMsg(f"开始下载 漫画:{book_name}",alert=True)
|
||
Comic.setCurrentDownLink(comic_href)
|
||
ComicPath.IS_UPDATE_COMIC = True
|
||
return True
|
||
else:
|
||
ntfy.sendMsg(f"{book_name} 已是最新",alert=True)
|
||
return False
|
||
#chapters = htmlUtils.xpathData(chapters_xpath,url=comic_href,update=False)
|
||
#chapter_index = 1
|
||
#for chapter in chapters:
|
||
# Comic.setNumber(chapter_index)
|
||
# Comic.setChapterName(chapter)
|
||
# cbz_path = ComicPath.getNewCBZComicChapter("file")
|
||
# icon_path = ComicPath.getNewIconComicChapter("file")
|
||
# CBZUtils.replaceZip(cbz_path)
|
||
# #判断漫画未完成
|
||
# if ciUtils.isProgressDone() and fu.notExists(cbz_path):
|
||
# ciUtils.setProgressFail()
|
||
# ciUtils.updateLastDate("0")
|
||
# Comic.setCurrentDownLink(comic_href)
|
||
# chapter_index = chapter_index + 1
|
||
#return None
|
||
|
||
@classmethod
|
||
def oneComic(cls,url,title,author,icon,tags,dep,chapters,chapter_href,alias=None,genre=None,lang="zh",age_rating=None,sleep=None):
|
||
ci.setComicInfo(homepage=url,comicname=title,author=author,icon=icon,tags=tags,dep=dep,genre=genre,lang=lang,age_rating=age_rating,chapters=chapters)
|
||
cls.count_chapter = 0
|
||
for href in chapter_href:
|
||
Comic.setChapterName(chapters[cls.count_chapter])
|
||
Comic.setNumber(cls.count_chapter+1)
|
||
#存在完成配置文件 但文件不存在 将清空完成配置文件
|
||
#if ciUtils.isProgressDone() and fu.notExists(ComicPath.getNewCBZComicChapter("file")):
|
||
# ciUtils.setProgressFail()
|
||
#不存在完成配置文件 则允许下载
|
||
if not ciUtils.isProgressDone():
|
||
#if fu.exists(ComicPath.getNewCBZComicChapter("file")): ciUtils.setProgressDone()
|
||
cls.comicChapters(href)
|
||
cls.count_chapter += 1
|
||
#一本漫画下载后等待
|
||
#清空文件夹
|
||
if os.path.exists(ComicPath.getDirComic()): shutil.rmtree(ComicPath.getDirComic())
|
||
if sleep != None: time.sleep(sleep)
|
||
#完成 更新最近一次时间
|
||
ciUtils.updateLastDate()
|
||
|
||
'''
|
||
|
||
读取某章节下所有图片
|
||
'''
|
||
@classmethod
|
||
def comicChapters(cls,chapter_url):
|
||
is_next = False
|
||
#try:
|
||
cls.Onechapter(chapter_url)
|
||
#进入下个阶段
|
||
#章节图片全部下载后,调用下载封面
|
||
comic_icon_path = ComicPath.getPathConfComicIcon()
|
||
chapter_icon_path = ComicPath.getPathCBZComicChapterIcon()
|
||
if ciUtils.isProgressDown and os.path.exists(comic_icon_path) and not os.path.exists(chapter_icon_path):
|
||
shutil.copy(comic_icon_path,chapter_icon_path)
|
||
ntfy.sendMsg(f"复制成功:{comic_icon_path} -> {chapter_icon_path}")
|
||
ciUtils.iconDB()
|
||
ciUtils.setProgressCBZ()
|
||
#下个阶段
|
||
if ciUtils.isProgressCBZ: is_next = CBZUtils.packAutoComicChapterCBZ()
|
||
#except Exception as e: is_next = ntfy.sendMsg(f"{ci.getComicName()} 下载出错了",error=e)
|
||
ntfy.sendMsg(f"预计总章节大小:{cls.count_chapter + 1} / "+ str(Comic.getLenChapters()),alert=True)
|
||
Comic.setNumber(cls.count_chapter + 1)
|
||
if is_next: ntfy.sendMsg(f"{ComicPath.TIME_SLEEP} 秒后开始下载下一个章节",sleep=ComicPath.TIME_SLEEP)
|
||
|
||
#根据章节地址下载全部图片,并将文件名scramble开头的图片进行解密
|
||
@classmethod
|
||
def Onechapter(cls,chapter_url):
|
||
is_next = True
|
||
if not str(chapter_url).startswith("http"): chapter_url = ci.getBaseUrl() + chapter_url
|
||
#下载图片
|
||
is_next = cls.comicChapterDownload(chapter_url)
|
||
#下载完成后, 开始解密图片
|
||
chapter_dir = ComicPath.getDirComicChapter()
|
||
if os.path.exists(chapter_dir): #获取章节图片路径
|
||
dirs = os.listdir(chapter_dir)
|
||
for img in dirs:
|
||
if img.startswith("scramble="):
|
||
imageUtils.encode_scramble_image(os.path.join(chapter_dir,img))
|
||
#进入下一阶段
|
||
ciUtils.setProgressDown()
|
||
return is_next
|
||
|
||
@classmethod
|
||
def comicChapterDownload(cls,url):
|
||
is_next = True
|
||
#获取本次工程的HOME目录
|
||
try:
|
||
domains.setdomain(url)
|
||
except:
|
||
htmlUtils.remove_HtmlCache(url)
|
||
cls.comicChapterDownload(url)
|
||
#保存信息
|
||
ci.writeJson()
|
||
#验证数据是已存在且是否完整
|
||
is_next = CBZUtils.isVerCBZComic()
|
||
#不存在ComicInfo.xml则生成
|
||
ci.writeComicInfoXML()
|
||
#图标
|
||
downloadUtils.putDownImageUrlDirFile(Comic.getIcon(),ComicPath.getDirConfComic(),ComicPath.COMIC_ICON_FILE_NAME)
|
||
CBZUtils.isUpdateOldCBZ()
|
||
downloadUtils.start_downloads(timeout=8)
|
||
return is_next |