PyComicPackRouMan/common/ComicInfo.py
2023-04-04 06:29:49 +08:00

254 lines
9.1 KiB
Python

import json,os
from xml.dom.minidom import Document
from common.Constant import pathStr
from utils.FileUtils import dbUtils
from utils.FileUtils import fileUtils
from common.Comic import Comic
from common.Constant import ComicPath
class ComicInfoEntity:
#章节名 value node child
@classmethod
def getNodes(cls):
#web [Comic.homepage,"Web"]
nodes = []
#章节名
nodes.append([Comic.chapter,"Title"])
#漫画名
nodes.append([Comic.comic_name,"Series"])
#编号
nodes.append([Comic.number,"Number"])
#别名
nodes.append([Comic.comic_names,"SeriesGroup"])
#概述
nodes.append([Comic.dep,"Summary"])
#年
nodes.append([Comic.year,"Year"])
#月
nodes.append([Comic.month,"Month"])
#日
nodes.append([Comic.day,"Day"])
#作者
nodes.append([Comic.author,"Writer"])
#出版社
nodes.append([Comic.cbs,"Publisher"])
#流派
nodes.append([Comic.genre,"Genre"])
#标签
nodes.append([Comic.tags,"Tags"])
#主页
#nodes.append([Comic.homepage,"Web"])
#总页数
nodes.append([Comic.page_count,"PageCount"])
#语言
nodes.append([Comic.language,"LanguageISO"])
#年龄分级
nodes.append([Comic.agerating,"AgeRating"])
#页码
nodes.append([Comic.pages,"Pages"])
return nodes
class ComicInfo:
COMIC_ICON_NAME = "000"
COMIC_INFO_XML = "ComicInfo.xml"
IS_NEW_ICON = False
document = Document()
path_comic_info = None
@classmethod
def parseExec(cls,data,exec,start_add=None,item=True):
if data !=None and exec != None:
dots = str(exec).split(".")
if not isinstance(data,dict): data = json.loads(data)
for dot in dots:
data = data.get(dot)
if start_add != None and data != None:
data = start_add+data
return data
@classmethod
def setNodeAndValue(cls,node,value):
if value != None:
if isinstance(value,str):
c_node = cls.document.createElement(node)
child_node = cls.document.createTextNode(value)
c_node.appendChild(child_node)
return c_node
else: return value
return None
#页数
@classmethod
def setPages(cls,values):
if values != None and isinstance(values,list):
suffix = "."+str(values[0]).split(".")[-1]
join_list=",".join(values).replace(suffix,"")
values = join_list.split(",")
Comic.setPageCount(len(values)+1 if cls.IS_NEW_ICON else len(values))
root_node = cls.document.createElement("Pages")
if cls.IS_NEW_ICON:
#添加封面
icon_node = cls.document.createElement("Page")
icon_node.setAttribute("Image",cls.COMIC_ICON_NAME)
icon_node.setAttribute("Type","FrontCover")
root_node.appendChild(icon_node)
for page in values:
c_node = cls.document.createElement("Page")
page = page.split("_")[-1]
c_node.setAttribute("Image",page)
root_node.appendChild(c_node)
Comic.pages = root_node
@classmethod
def getBaseUrl(cls,url=None):
if url == None:
url = Comic.homepage
(num,index) = [3,0]
for x in range(0, num):
index = str(url).find("/",index)+1
return url[0:index-1]
@classmethod
def getPathComicInfoXML(cls):
try:
cls.path_comic_info = os.path.join(pathStr.base_comic_img(),
Comic.comic_name,Comic.chapter, cls.COMIC_INFO_XML)
except:
return None
return cls.path_comic_info
#XML根文档
@classmethod
def root_node(cls,root_value): return cls.document.createElement(root_value)
@classmethod
def add_nodes(cls,root,list_value):
if len(list_value) == 0: return list_value
for value in list_value:
#Comic.chapter
if value[0] != None: root.appendChild(cls.setNodeAndValue(value[1],value[0]))
@classmethod
def writeComicInfoXML(cls,chapter=None,path=None,overlay=False):
root = cls.root_node("ComicInfo")
new_document = Document()
new_document.appendChild(root)
cls.add_nodes(root,ComicInfoEntity.getNodes())
cls.getPathComicInfoXML()
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) and not overlay:
print(f"{cls.COMIC_INFO_XML} 已存在")
return None
with open(cls.path_comic_info , "w", encoding="utf-8") as fo:
new_document.writexml(fo, indent='', addindent='\t', newl='\n', encoding="utf-8")
fo.close()
print(f"{cls.COMIC_INFO_XML} 已生成 pathd=", cls.path_comic_info)
@classmethod
def setComicInfo(cls,comicname=None,homepage=None,alias=None,author=None,icon=None,tags=None,
dep=None,genre=None,lang=None,age_rating=None,chapters=None,update_at=None,current_chapter_img=None):
author = ",".join(set(str(str(author).replace("&",",").replace(" ",",")).split(",")))
Comic.setHomePage(homepage)
Comic.setIcon(icon)
Comic.setListChapter(chapters)
Comic.setUpdateAt(update_at)
Comic.setComicName(str(comicname))
#if alias != None: comicInfo.setComicNames(alias)
Comic.setAuthor(author)
Comic.setTags(tags)
Comic.setDep(dep)
#comicInfo.setCBS("韩漫")
if genre != None: Comic.setGenre(genre)
Comic.setLanguage(lang)
Comic.setAgeRating(age_rating)
Comic.setCurrentChapterImg(current_chapter_img)
class ComicInfoUtils:
PROGRESS_INFO = "info"
PROGRESS_DOWN = "download"
PROGRESS_IMG = "download"
PROGRESS_CBZ = "cbz"
PROGRESS_DONE = "done"
PROGRESS_NONE = "none"
IS_NEW_ICON = False
list_skip = []
@classmethod
def getListToString(cls,to_list):
value = to_list
if isinstance(to_list,list):
value = ",".join(to_list)
return value
@classmethod
def setComicNameSkips(cls,value): return cls.list_skip.append(value)
@classmethod
def getIsComicNameSkips(cls,value): return value in ",".join(cls.list_skip)
@classmethod
def nextSavePath(cls,next,data=None):
save_path = ComicPath.getDirConfComic()+"/"+next+Comic.getChapterName()
if data != None: fileUtils.file_save(save_path, data)
return save_path
@classmethod
def nextSaveInfoChapter(cls,chapter,data=None):
if data == None: data = Comic.getChapterImgs()
if Comic.getChapterName() != chapter:
print(f"chapter {Comic.getChapterName()}{chapter} 不一致,已自动跳过")
cls.setProgress(cls.PROGRESS_INFO)
cls.nextSavePath("info_",data)
@classmethod
def nextInfoToImgChapter(cls): cls.setProgress(cls.PROGRESS_IMG)
@classmethod
def nextImgToDownloadChapter(cls): cls.setProgress(cls.PROGRESS_DOWN)
@classmethod
def nextDownloadToCBZChapter(cls): cls.setProgress(cls.PROGRESS_CBZ)
@classmethod
def nextCBZToDoneChapter(cls): cls.setProgress(cls.PROGRESS_DONE)
@classmethod
def nextDoneSave(cls,data): cls.nextSavePath("done_",data)
@classmethod
def setProgress(cls,progress):
dbUtils.setComic(Comic.getChapterName(),progress,Comic.getComicName())
@classmethod
def isProgress(cls,progress,remove=None):
if remove: cls.setProgress("None")
return dbUtils.query(Comic.getChapter(),progress,Comic.getComicName())
@classmethod
def iconDB(cls): dbUtils.setComic(Comic.getComicName(),Comic.getIcon(),"icons")
@classmethod
def equIcon(cls): return dbUtils.query(Comic.getComicName(),Comic.getIcon(),"icons")
@classmethod
def setConfDirComicPath(cls,file_name,comic_name=None):
if comic_name != None: Comic.setComicName(comic_name)
return os.path.join(ComicPath.getDirConfComic(),file_name)
@classmethod
def saveConfComicData(cls,file_name,data,comic_name=None): fileUtils.file_save(cls.setConfDirComicPath(file_name,comic_name), data)
@classmethod
def getPathInitConfComicData(cls,file_name,comic_name=None): return cls.setConfDirComicPath(file_name,comic_name)
@classmethod
def updateLastDate(cls,date=None):
update_at = Comic.getUpdateAt()
if date != None: update_at = date
dbUtils.setComic(Comic.getComicName(), update_at, "update")
@classmethod
def isUpdateComic(cls):
return dbUtils.query(Comic.getComicName(), Comic.getUpdateAt(),"update")
@classmethod
def comicChapterDownload(cls,imgs,names):
Comic.setChapterImgs(imgs)
#Comic.setChapterListImg(imgs)
ComicInfo.setPages(names)
Comic.setChapterFilesName(names)