215 lines
8.4 KiB
Python
215 lines
8.4 KiB
Python
import json,os
|
||
import logging
|
||
from xml.dom.minidom import Document
|
||
from utils.FileUtils import dbUtils as db
|
||
from common.Comic import Comic
|
||
from common.Constant import ComicPath
|
||
from utils.Logger import logger
|
||
|
||
class ComicInfoEntity:
|
||
@classmethod
|
||
def getNodes(cls):
|
||
return [Comic.dict_chapter,Comic.dict_comic_name,Comic.dict_number,Comic.dict_comic_names,
|
||
Comic.dict_dep,Comic.dict_year,Comic.dict_month,Comic.dict_day,Comic.dict_author,
|
||
Comic.dict_cbs,Comic.dict_genre,Comic.dict_tags,Comic.dict_page_count,
|
||
Comic.dict_language,Comic.dict_agerating,Comic.dict_pages]
|
||
@classmethod
|
||
def getJsonNodes(cls):
|
||
return [Comic.dict_chapter,Comic.dict_comic_name,Comic.dict_icon,Comic.dict_number,
|
||
Comic.dict_comic_names,
|
||
Comic.dict_dep,Comic.dict_year,Comic.dict_month,Comic.dict_day,Comic.dict_author,
|
||
Comic.dict_cbs,Comic.dict_genre,Comic.dict_tags,Comic.dict_page_count,
|
||
Comic.dict_language,Comic.dict_agerating,Comic.dict_pages,
|
||
Comic.dict_list_chapter,Comic.dict_chapter_imgs]
|
||
class ComicInfo:
|
||
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=None):
|
||
if values == None: values = Comic.getChapterFilesName()
|
||
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",ComicPath.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.dict_pages = Comic.setField(Comic.dict_pages,root_node,convert=False)
|
||
|
||
@classmethod
|
||
def getBaseUrl(cls,url=None):
|
||
if url == None:
|
||
url = Comic.getHomePage()
|
||
(num,index) = [3,0]
|
||
for x in range(0, num):
|
||
index = str(url).find("/",index)+1
|
||
return url[0:index-1]
|
||
|
||
#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 and value[4]:
|
||
#数据为空 value[0], 但不允许为空value[4] = False
|
||
msg = f"#数据为空 key={value[3]} value[0]={value[0]}, 但不允许为空value[4]={value[4]}"
|
||
logger.error(msg)
|
||
exit()
|
||
if value[0] != None: root.appendChild(cls.setNodeAndValue(value[2],value[0]))
|
||
|
||
@classmethod
|
||
def initComicInfoXML(cls):
|
||
cls.setPages()
|
||
|
||
@classmethod
|
||
def writeComicInfoXML(cls,overlay=False):
|
||
save_path = ComicPath.getPathComicInfoXML()
|
||
if os.path.exists(save_path):
|
||
if overlay:
|
||
os.remove(save_path)
|
||
logging.info(f"已存在文件,已删除... {save_path}")
|
||
else:
|
||
logging.info(f"已存在文件,跳过中... {save_path}")
|
||
return True
|
||
cls.initComicInfoXML()
|
||
root = cls.root_node("ComicInfo")
|
||
new_document = Document()
|
||
new_document.appendChild(root)
|
||
cls.add_nodes(root,ComicInfoEntity.getNodes())
|
||
with open(save_path, "w", encoding="utf-8") as fo:
|
||
new_document.writexml(fo, indent='', addindent='\t', newl='\n', encoding="utf-8")
|
||
fo.close()
|
||
logger.info(f"已生成文件... {save_path}")
|
||
|
||
@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,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)
|
||
|
||
@classmethod
|
||
def writeJson(cls):
|
||
dict_data = {}
|
||
nodes = ComicInfoEntity.getJsonNodes()
|
||
for node in nodes:
|
||
key = Comic.getFieldNode(node)
|
||
value = Comic.getFieldOrigin(node)
|
||
if isinstance(value,list):
|
||
value = ",".join(value)
|
||
if key != None and isinstance(value,str):
|
||
child_dict = { key : value}
|
||
dict_data.update(child_dict)
|
||
s = json.dumps(dict_data,ensure_ascii=True)
|
||
logging.debug(f"json={s}")
|
||
with open(ComicPath.getPathConfComicChapterJson(mkdir=True),"w") as fs:
|
||
fs.write(s)
|
||
|
||
class ComicInfoUtils:
|
||
PROGRESS_DOWN = "down"
|
||
PROGRESS_CBZ = "cbz"
|
||
PROGRESS_DONE = "done"
|
||
PROGRESS_FAIL = "fail"
|
||
IS_NEW_ICON = False
|
||
list_skip = []
|
||
|
||
@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 setProgress(cls,progress): db.set(Comic.getOriginChapterName(),progress,Comic.getOriginComicName())
|
||
|
||
@classmethod
|
||
def isProgress(cls,progress): return db.query(Comic.getOriginChapterName(),progress,Comic.getOriginComicName())
|
||
|
||
#判断是否完成
|
||
@classmethod
|
||
def isProgressDone(cls): return cls.isProgress(cls.PROGRESS_DONE)
|
||
#设置为完成
|
||
@classmethod
|
||
def setProgressDone(cls): return cls.setProgress(cls.PROGRESS_DONE)
|
||
#设置未完成
|
||
@classmethod
|
||
def setProgressFail(cls): return cls.setProgress(cls.PROGRESS_FAIL)
|
||
@classmethod
|
||
def isProgressDown(cls): return cls.isProgress(cls.PROGRESS_DOWN)
|
||
@classmethod
|
||
def setProgressDown(cls): return cls.setProgress(cls.PROGRESS_DOWN)
|
||
@classmethod
|
||
def isProgressCBZ(cls): return cls.isProgress(cls.PROGRESS_CBZ)
|
||
@classmethod
|
||
def setProgressCBZ(cls): return cls.setProgress(cls.PROGRESS_CBZ)
|
||
@classmethod
|
||
def iconDB(cls): db.set(Comic.getOriginComicName(),Comic.getIcon(),"icons")
|
||
|
||
@classmethod
|
||
def equIcon(cls): return db.query(Comic.getOriginComicName(),Comic.getIcon(),"icons")
|
||
|
||
@classmethod
|
||
def updateLastDate(cls,date=None):
|
||
update_at = Comic.getUpdateAt()
|
||
if date != None: update_at = date
|
||
db.set(Comic.getOriginComicName(), update_at, "update")
|
||
|
||
@classmethod
|
||
def isUpdateComic(cls,update_at=None):
|
||
if update_at == None: update_at = Comic.getUpdateAt()
|
||
return db.query(Comic.getOriginComicName(), update_at,"update")
|
||
|
||
@classmethod
|
||
def comicChapterDownload(cls,imgs,names):
|
||
Comic.setChapterImgs(imgs)
|
||
ComicInfo.setPages(names)
|
||
Comic.setChapterFilesName(names) |