PyComicPackRouMan/utils/comic/ComicInfo.py
2023-01-14 17:49:40 +08:00

466 lines
14 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
class comicInfo():
document = Document()
pathComicInfo = None
root = "ComicInfo"
chapter = "Title"
comic_name = "Series"
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"
str_comicName = None
str_chapter = None
str_icon = None
str_homePage = None
str_listChapter = None
str_chapter_imgs = None
str_updateAt = 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_value1 = None
chapter_node = None
comicName_node = None
dep_node = None
author_node = None
genre_node = None
cbs_node = None
lang_node = None
comicNames_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
@classmethod
def setNodeAndValue(cls,node,value):
if value != None:
c_node = cls.document.createElement(node)
node_text = cls.document.createTextNode(str(value).replace("\n",""))
c_node.appendChild(node_text)
return c_node
return None
@classmethod
def Root(cls):
return cls.document.createElement(cls.root)
@classmethod
def setChapterName(cls,value):
cls.str_chapter = cls.fixFileName(value)
cls.chapter_node = cls.setNodeAndValue(cls.chapter,value)
@classmethod
def setListChapter(cls, value):
cls.str_listChapter = value
@classmethod
def setChapterImgs(cls, value):
cls.str_chapter_imgs = value
@classmethod
def getChapterImgs(cls):
return cls.str_chapter_imgs
@classmethod
def getLenChapters(cls):
return len(cls.str_listChapter)
@classmethod
def setComicName(cls,value):
cls.str_comicName = cls.fixFileName(value)
cls.comicName_node = cls.setNodeAndValue(cls.comic_name, value)
@classmethod
def setComicNames(cls,value):
cls.comicNames_node = cls.setNodeAndValue(cls.comic_names,value)
@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 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 = value
cls.web_node = cls.setNodeAndValue(cls.web,value)
@classmethod
def setChapterListImg(cls,value):
cls.str_list_img=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=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 = value
cls.page_count_node = cls.setNodeAndValue(cls.page_count,value)
@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(str(len(value)))
root_node = cls.document.createElement(cls.pages)
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 = value
return cls.str_icon
@classmethod
def setHomePage(cls, value):
cls.str_homePage = value
@classmethod
def getHomePage(cls):
return cls.str_homePage
@classmethod
def setUpdateAt(cls, value):
cls.str_updateAt = value
@classmethod
def getUpdateAt(cls):
return cls.str_updateAt
@classmethod
def getListToString(cls,to_list):
value = to_list
if isinstance(to_list,list):
value = ",".join(to_list)
return value
'''
获取网站主页
'''
@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_comicName
@classmethod
def getChapter(cls):
return cls.str_chapter
@classmethod
def fixFileName(cls,filename,replace=None):
intab = r'[?*/\|.:><]'
str_replace = ""
if not replace == None:
str_replace = replace
filename = re.sub(intab, str_replace, filename)
return filename
@classmethod
def getDirConfComic(cls):
if not cls.str_comicName == None:
return os.path.join(pathStr.base_conf_path, cls.str_comicName)
else:
print("comicName不存在退出中")
exit()
@classmethod
def getDirCBZComic(cls):
if not cls.str_comicName == None:
return os.path.join(pathStr.base_cbz, cls.str_comicName)
else:
print("comicName不存在退出中")
exit()
@classmethod
def getDirCBZComicChapter(cls):
if not cls.str_comicName == None and not cls.str_chapter == None:
return os.path.join(pathStr.base_cbz,cls.str_comicName,cls.str_chapter)
else:
print("comicName不存在退出中")
exit()
@classmethod
def getDirComic(cls):
if cls.str_comicName != None:
return os.path.join(pathStr.base_comic_img, cls.str_comicName)
else:
print("comicName不存在退出中")
exit()
@classmethod
def getDirComicChapter(cls):
if cls.str_comicName != None and cls.str_chapter != None:
return os.path.join(pathStr.base_comic_img,cls.str_comicName,cls.str_chapter)
else:
print("comicName与chapter 不存在,退出中")
exit()
@classmethod
def getPathComicInfoXML(cls):
try:
cls.pathComicInfo = os.path.join(pathStr.base_comic_img,cls.str_comicName,cls.str_chapter,"ComicInfo.xml")
except:
return None
return cls.pathComicInfo
@classmethod
def writeComicInfoXML(cls,chapter,path=None):
if cls.chapter == cls.fixFileName(chapter):
print(f"cls.chapter {cls.chapter} 与 chapter {chapter} 不相等,已自动跳过")
root = cls.Root()
newDocument = Document()
newDocument.appendChild(root)
if cls.chapter_node != None: root.appendChild(cls.chapter_node)
if cls.comicName_node != None: root.appendChild(cls.comicName_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.comicNames_node != None: root.appendChild(cls.comicNames_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.pathComicInfo = os.path.join(path,"ComicInfo.xml")
base_dir = os.path.dirname(cls.pathComicInfo)
if not os.path.exists(base_dir):
os.makedirs(base_dir)
if os.path.exists(cls.pathComicInfo):
print("ComicInfo.xml 已存在")
return None
with open(cls.pathComicInfo , "w", encoding="utf-8") as fo:
newDocument.writexml(fo, indent='', addindent='\t', newl='\n', encoding="utf-8")
fo.close()
print("ComicInfo.xml 已生成 pathd=", cls.pathComicInfo)
#文件保存
@classmethod
def file_save(cls,path,data,mode=None):
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()
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 not data == None:
cls.file_save(save_path, data)
return save_path
@classmethod
def nextSaveInfoChapter(cls,chapter,data):
if not cls.str_chapter == chapter:
print(f"chapter {cls.str_chapter}{chapter} 不一致,已自动跳过")
cls.nextSavePath("info_",data)
@classmethod
def nextInfoToImgChapter(cls):
info = cls.nextSavePath("info_")
img = cls.nextSavePath("img_")
shutil.move(info,img)
@classmethod
def nextImgToDownloadChapter(cls):
img = cls.nextSavePath("img_")
download = cls.nextSavePath("down_")
shutil.move(img, download)
@classmethod
def nextDownloadToCBZChapter(cls):
download = cls.nextSavePath("down_")
cbz = cls.nextSavePath("cbz_")
shutil.move(download, cbz)
@classmethod
def nextCBZToDoneChapter(cls):
cbz = cls.nextSavePath("cbz_")
done = cls.nextSavePath("done_")
shutil.move(cbz,done)
@classmethod
def nextDoneSave(cls,data):
cls.nextSavePath("done_",data)
@classmethod
def nextExistsGetPath(cls,msg):
path = cls.nextSavePath(msg)
return os.path.exists(path)
@classmethod
def saveConfComicData(cls,fileName,data,comicName=None):
if not comicName == None:
cls.setComicName(comicName)
dirConfComic = cls.getDirConfComic()
save_path = os.path.join(dirConfComic,fileName)
cls.file_save(save_path, data)
@classmethod
def getPathInitConfComicData(cls,fileName,comicName=None):
if not comicName == None:
cls.setComicName(comicName)
dirConfComic = cls.getDirConfComic()
save_path = os.path.join(dirConfComic,fileName)
return save_path
@classmethod
def updateComicDate(cls):
data = {}
update_at = cls.str_updateAt
comic_name = cls.str_comicName
update_path = pathStr.base_comic_update
update_dir = os.path.dirname(update_path)
if not os.path.exists(update_dir):
os.makedirs(update_dir)
if os.path.exists(update_path):
with open(update_path,"r") as fs:
data = json.loads(fs.read())
fs.close()
data[comic_name] = update_at
with open(update_path,"w") as fs:
fs.write(json.dumps(data))
fs.close()
@classmethod
def isUpdateComic(cls):
is_update = True
data = {}
c_update_at = cls.str_updateAt
comic_name = cls.str_comicName
update_path = pathStr.base_comic_update
update_dir = os.path.dirname(update_path)
if not os.path.exists(update_dir):
os.makedirs(update_dir)
try:
with open(update_path,"r") as fs:
data = json.loads(fs.read())
fs.close()
except:
data = {}
comic_update = data.get(comic_name)
if comic_name != None and comic_update == c_update_at:
is_update = False
return is_update
@classmethod
def comicChapterDownload(cls,imgs,names):
cls.setChapterListImg(imgs)
cls.setChapterFilesName(names)