PyComicPackRouMan/utils/comic/ComicInfo.py
2023-04-01 13:23:34 +08:00

494 lines
18 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from xml.dom.minidom import Document
import os,re
from utils.comic.PathStr import pathStr
import json,shutil
from utils.FileUtils import dbUtils
from utils.ComicUtils import fontUtils
from utils.OldUtils import OldUtils
class comicInfo():
COMIC_ICON_NAME = "000"
COMIC_INFO_XML = "ComicInfo.xml"
PROGRESS_INFO = "info"
PROGRESS_DOWN = "download"
PROGRESS_IMG = "download"
PROGRESS_CBZ = "cbz"
PROGRESS_DONE = "done"
PROGRESS_NONE = "none"
IS_NEW_ICON = False
document = Document()
path_comic_info = None
root = "ComicInfo"
chapter = "Title"
comic_name = "Series"
number = "Number"
dep = "Summary"
author = "Writer"
genre = "Genre"
cbs = "Publisher"
lang = "LanguageISO"
comic_names = "SeriesGroup"
tags = "Tags"
date_year = "Year"
date_month = "Month"
date_day = "Day"
page_count = "PageCount"
pages = "Pages"
web = "Web"
age_rating = "AgeRating"
str_comic_name = None
str_chapter = None
str_number = None
str_icon = None
str_homepage = None
str_listchapter = None
str_chapter_imgs = None
str_update_at = None
str_date_year = None
str_date_month = None
str_date_day = None
str_page_count = None
str_web = None
str_list_img = None
str_files_img = None
str_chapter_index= None
str_value1 = None
str_current_chapter_img = None
list_skip = []
chapter_node = None
comic_name_node = None
number_node = None
dep_node = None
author_node = None
genre_node = None
cbs_node = None
lang_node = None
comic_names_node = None
tags_node = None
date_year_node = None
date_month_node = None
date_day_node = None
page_count_node = None
pages_node = None
web_node = None
age_rating_node = None
@classmethod
def setNodeAndValue(cls,node,value):
if value != None:
c_node = cls.document.createElement(node)
node_text = cls.document.createTextNode(fontUtils.ChineseConvert(str(value).replace("\n","")))
c_node.appendChild(node_text)
return c_node
return None
@classmethod
def root_node(cls): return cls.document.createElement(cls.root)
@classmethod
def setChapterName(cls,value):
cls.str_chapter = fontUtils.ChineseConvert(cls.fixFileName(value))
OldUtils.setOldChapter(cls.fixFileName(value))
cls.chapter_node = cls.setNodeAndValue(cls.chapter,value)
@classmethod
def setComicValue(cls,value):
result = None
if value != None or value != "": result = value
return result
@classmethod
def setListChapter(cls, value): cls.str_list_chapter = cls.setComicValue(value)
@classmethod
def setChapterImgs(cls, value): cls.str_chapter_imgs = cls.setComicValue(value)
@classmethod
def setCurrentChapterImg(cls, value): cls.str_current_chapter_img = cls.setComicValue(value)
@classmethod
def getCurrentChapterImg(cls): return cls.str_current_chapter_img
@classmethod
def getChapterImgs(cls): return cls.str_chapter_imgs
@classmethod
def getLenChapters(cls): return len(cls.str_list_chapter)
@classmethod
def setComicName(cls,value):
cls.str_comic_name = fontUtils.ChineseConvert(cls.fixFileName(value))
OldUtils.setOldComicName(cls.fixFileName(value))
cls.comic_name_node = cls.setNodeAndValue(cls.comic_name, value)
@classmethod
def setComicNames(cls,value):
#去重
value = ",".join(set(str(fontUtils.ChineseConvert(value)).split(",")))
cls.comic_names_node = cls.setNodeAndValue(cls.comic_names,value)
@classmethod
def setNumber(cls,value):
cls.str_number = value
cls.number_node = cls.setNodeAndValue(cls.number, value)
@classmethod
def getNumber(cls): return cls.str_number
@classmethod
def setDep(cls,value): cls.dep_node = cls.setNodeAndValue(cls.dep, value)
@classmethod
def setAuthor(cls,value): cls.author_node = cls.setNodeAndValue(cls.author,cls.getListToString(value))
@classmethod
def setLang(cls,value): cls.lang_node = cls.setNodeAndValue(cls.lang, value)
@classmethod
def setAgeRating(cls,value): cls.age_rating_node = cls.setNodeAndValue(cls.age_rating, value)
@classmethod
def setGenre(cls,value): cls.genre_node = cls.setNodeAndValue(cls.genre, cls.getListToString(value))
@classmethod
def setTags(cls,value): cls.tags_node = cls.setNodeAndValue(cls.tags,cls.getListToString(value))
@classmethod
def setCBS(cls,value): cls.cbs_node = cls.setNodeAndValue(cls.cbs,value)
@classmethod
def setWeb(cls,value):
cls.str_web = cls.setComicValue(value)
cls.web_node = cls.setNodeAndValue(cls.web,cls.setComicValue(value))
@classmethod
def setChapterListImg(cls,value): cls.str_list_img=cls.setComicValue(value)
@classmethod
def setValue1(cls,value): cls.str_value1 = value
@classmethod
def getValue1(cls): return cls.str_value1
@classmethod
def getChapterListImg(cls): return cls.str_list_img
@classmethod
def setChapterFilesName(cls,value): cls.str_files_img=cls.setComicValue(value)
@classmethod
def getChapterFilesName(cls): return cls.str_files_img
@classmethod
def getWeb(cls): return cls.str_web
@classmethod
def setPageCount(cls,value):
cls.str_page_count = cls.setComicValue(int(value))
cls.page_count_node = cls.setNodeAndValue(cls.page_count,cls.str_page_count)
@classmethod
def setPages(cls,value):
if value != None:
su = "."+str(value[0]).split(".")[-1]
join_list=",".join(value).replace(su,"")
value = join_list.split(",")
cls.setPageCount(len(value)+1 if cls.IS_NEW_ICON else len(value))
root_node = cls.document.createElement(cls.pages)
if cls.IS_NEW_ICON:
#添加封面
icon_node = cls.document.createElement("Page")
icon_node.setAttribute("Image","000")
icon_node.setAttribute("Type","FrontCover")
root_node.appendChild(icon_node)
for page in value:
c_node = cls.document.createElement("Page")
page = page.split("_")[-1]
c_node.setAttribute("Image",page)
root_node.appendChild(c_node)
cls.pages_node = root_node
@classmethod
def setDate(cls,value,split):
values = str(value).split(split)
cls.str_date_year = values[0]
cls.str_date_month = values[1]
cls.str_date_day = values[2]
cls.date_year_node = cls.setNodeAndValue(cls.date_year,values[0])
cls.date_month_node = cls.setNodeAndValue(cls.date_month,values[1])
cls.date_day_node = cls.setNodeAndValue(cls.date_day,values[2])
@classmethod
def setIcon(cls,value):
cls.str_icon = cls.setComicValue(value)
return cls.str_icon
@classmethod
def setHomePage(cls, value): cls.str_homepage = cls.setComicValue(value)
@classmethod
def getHomePage(cls): return cls.str_homepage
@classmethod
def setUpdateAt(cls, value): cls.str_update_at = cls.setComicValue(value)
@classmethod
def getUpdateAt(cls): return cls.str_update_at
@classmethod
def getListToString(cls,to_list):
value = to_list
if isinstance(to_list,list):
value = ",".join(to_list)
return value
@classmethod
def setChapterIndex(cls,value):
cls.setNumber(value)
cls.str_chapter_index = cls.setComicValue(value)
@classmethod
def getChapterIndex(cls): return cls.str_chapter_index
@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 getBaseUrl(cls,url=None):
if url == None:
url = cls.str_homepage
num = 3
index = 0
for x in range(0, num):
index = str(url).find("/",index)+1
return url[0:index-1]
@classmethod
def getIcon(cls): return cls.str_icon
@classmethod
def getComicName(cls): return cls.str_comic_name
@classmethod
def getChapter(cls): return cls.str_chapter
@classmethod
def fixFileName(cls,filename,replace=None):
intab = r'[?*/\|.:><]'
str_replace = ""
if replace != None:
str_replace = replace
filename = re.sub(intab, str_replace, filename)
count = 1
while True:
str_file = filename[0-count]
if str_file == " ":
count += 1
else:
filename = filename[0:len(filename)+1-count]
break
return filename
@classmethod
def getDirConfComic(cls):
if cls.str_comic_name != None:
return os.path.join(pathStr.base_conf_path(), cls.str_comic_name)
else:
print("comicName不存在退出中")
exit()
@classmethod
def getDirCBZComic(cls):
if cls.str_comic_name != None:
return os.path.join(pathStr.base_cbz(), cls.str_comic_name)
else:
print("comicName不存在退出中 getDirCBZComic")
exit()
@classmethod
def getDirCBZComicChapter(cls):
if cls.str_comic_name != None and cls.str_chapter != None:
return os.path.join(pathStr.base_cbz(),cls.str_comic_name,cls.str_chapter)
else:
print("comicName不存在退出中 getDirCBZComicChapter")
exit()
@classmethod
def getSortDirCBZComicChapter(cls):
if cls.str_comic_name != None and cls.str_chapter != None and cls.str_chapter_index != None:
return os.path.join(pathStr.base_cbz(),cls.str_comic_name,str(cls.str_chapter_index)+" "+cls.str_chapter)
else:
print("comicName不存在退出中 getSortDirCBZComicChapter")
return None
@classmethod
def getNewCBZComicChapter(cls,type="dir"): return cls.getNewToComicChapter(".CBZ", type)
@classmethod
def getNewIconComicChapter(cls,type="dir"): return cls.getNewToComicChapter(".jpg", type)
@classmethod
def getNewFileCBZComicChapter(cls,type="file"): return cls.getNewToComicChapter(".CBZ", type)
@classmethod
def getNewFileIconComicChapter(cls,type="file"): return cls.getNewToComicChapter(".jpg", type)
@classmethod
def getNewToComicChapter(cls,su,type="dir"):
c_dir = cls.getDirCBZComicChapter()
s_dir = cls.getSortDirCBZComicChapter()
c_path = cls.getDirCBZComicChapter()+su
s_path = cls.getSortDirCBZComicChapter()+su
if os.path.exists(s_path) and s_path != None:
shutil.move(s_path, c_path)
print("文件已移动至:", c_path)
if type == "file":
return c_path
return c_dir
@classmethod
def getDirComic(cls):
if cls.str_comic_name != None:
return os.path.join(pathStr.base_comic_img(), cls.str_comic_name)
else:
print("comicName不存在退出中")
exit()
@classmethod
def getDirComicChapter(cls):
if cls.str_comic_name != None and cls.str_chapter != None:
return os.path.join(pathStr.base_comic_img(),cls.str_comic_name,cls.str_chapter)
else:
print("comicName与chapter 不存在,退出中")
exit()
@classmethod
def getPathComicInfoXML(cls):
try:
cls.path_comic_info = os.path.join(pathStr.base_comic_img(),cls.str_comic_name,cls.str_chapter, cls.COMIC_INFO_XML)
except:
return None
return cls.path_comic_info
@classmethod
def writeComicInfoXML(cls,chapter=None,path=None,overlay=False):
root = cls.root_node()
new_document = Document()
new_document.appendChild(root)
if cls.chapter_node != None: root.appendChild(cls.chapter_node)
if cls.comic_name_node != None: root.appendChild(cls.comic_name_node)
if cls.number_node != None: root.appendChild(cls.number_node)
if cls.dep_node != None: root.appendChild(cls.dep_node)
if cls.author_node != None: root.appendChild(cls.author_node)
if cls.genre_node != None: root.appendChild(cls.genre_node)
if cls.cbs_node != None: root.appendChild(cls.cbs_node)
if cls.lang_node != None: root.appendChild(cls.lang_node)
if cls.age_rating_node != None: root.appendChild(cls.age_rating_node)
if cls.comic_names_node != None: root.appendChild(cls.comic_names_node)
if cls.tags_node != None: root.appendChild(cls.tags_node)
if cls.date_year_node != None: root.appendChild(cls.date_year_node)
if cls.date_month_node != None: root.appendChild(cls.date_month_node)
if cls.date_day_node != None: root.appendChild(cls.date_day_node)
if cls.page_count_node != None: root.appendChild(cls.page_count_node)
if cls.pages_node != None: root.appendChild(cls.pages_node)
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 file_save(cls,path,data,mode=None,print_msg=False):
result = {}
f = {}
dir_name = os.path.dirname(path)
if not os.path.exists(dir_name):
os.makedirs(dir_name)
save_path = os.path.join(path)
if os.path.exists(save_path):
os.remove(save_path)
data = json.dumps(data)
if mode == None:
mode = "w+"
try:
f = open(save_path, mode, encoding="utf-8")
f.write(data)
f.close()
if print_msg:
print("data=",data)
result = path + "文件写入成功"
except:
result = path + "文件写入失败"
print(result)
return result
@classmethod
def nextSavePath(cls,next,data=None):
save_path = cls.getDirConfComic()+"/"+next+cls.str_chapter
if data != None:
cls.file_save(save_path, data)
return save_path
@classmethod
def nextSaveInfoChapter(cls,chapter,data=None):
if data == None: data = cls.getChapterImgs()
if cls.str_chapter != chapter:
print(f"chapter {cls.str_chapter}{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(cls.str_chapter,progress,cls.str_comic_name)
@classmethod
def isProgress(cls,progress,remove=None):
if remove: cls.setProgress("None")
return dbUtils.query(cls.str_chapter,progress,cls.str_comic_name)
@classmethod
def iconDB(cls): dbUtils.setComic(cls.str_comic_name,cls.str_icon,"icons")
@classmethod
def equIcon(cls): return dbUtils.query(cls.str_comic_name,cls.str_icon,"icons")
@classmethod
def setConfDirComicPath(cls,file_name,comic_name=None):
if comic_name != None: cls.setComicName(comic_name)
return os.path.join(cls.getDirConfComic(),file_name)
@classmethod
def saveConfComicData(cls,file_name,data,comic_name=None): cls.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 updateComicDate(cls,date=None):
update_at = cls.getUpdateAt()
if date != None:
update_at = date
dbUtils.setComic(cls.str_comic_name, update_at, "update")
@classmethod
def isUpdateComic(cls):
return dbUtils.query(cls.str_comic_name, cls.str_update_at,"update")
@classmethod
def comicChapterDownload(cls,imgs,names):
cls.setChapterImgs(imgs)
cls.setChapterListImg(imgs)
cls.setPages(names)
cls.setChapterFilesName(names)
@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 = str(author).replace("&",",").replace(" ",",")
cls.setHomePage(homepage)
cls.setComicName(str(comicname))
if alias != None: comicInfo.setComicNames(alias)
cls.setAuthor(author)
cls.setIcon(icon)
cls.setTags(tags)
cls.setDep(dep)
#comicInfo.setCBS("韩漫")
if genre != None: cls.setGenre(genre)
cls.setLang(lang)
cls.setAgeRating(age_rating)
cls.setListChapter(chapters)
cls.setUpdateAt(update_at)
cls.setCurrentChapterImg(current_chapter_img)