PyComicPackRouMan/common/Constant.py
2023-04-07 20:30:43 +08:00

199 lines
8.4 KiB
Python

import os,datetime,shutil
from time import strftime
from common.Comic import Comic
class pathStr:
comic_name = None
comic_jm="JM"
comic_bz="BZ"
comic_rm="RM"
comic_url_main = None
base_comic_out = os.path.join("/mnt", "Comics")
old_cbz_path = os.path.join("/mnt","OldComics")
@classmethod
def base_cbz(cls): return cls.getBaseComicPath("CBZ")
@classmethod
def base_comic_img(cls): return cls.getBaseComicPath("outputComic")
@classmethod
def base_conf_path(cls): return cls.getBaseComicPath(".conf")
@classmethod
def base_html_cache(cls): return cls.getBaseComicPath("html_cache")
@classmethod
def base_html_chapter(cls): return cls.getBaseComicPath("html_updated")
@classmethod
def base_comic_update(cls): return cls.getBaseComicPath("comic_update")
@classmethod
def base_db(cls): return cls.getBaseComicPath("db")
@classmethod
def getBaseUrl(cls,url=None):
if url == None:
url = Comic.getHomePage()
num = 3
index = 0
for x in range(0, num):
index = str(url).find("/",index)+1
return url[0:index-1]
@classmethod
def getBaseComicPath(cls,join_path): return os.path.join(cls.base_comic_out,join_path)
@classmethod
def setComicMainAndPath(cls,value):
cls.setComicMain(value)
cls.setComicMainPath(value)
@classmethod
def setComicMain(cls,value): cls.comic_name = value
@classmethod
def getComicMain(cls): return cls.comic_name
@classmethod
def setComicMainPath(cls,value):
#if value != cls.comic_rm: cls.base_comic_out = os.path.join(cls.base_comic_out, value)
cls.base_comic_out = os.path.join(cls.base_comic_out, value)
@classmethod
def base_html_week(cls):
date_path = cls.getDatePath()
return os.path.join(cls.base_comic_out,"html_"+str(date_path))
@classmethod
def getDatePath(cls):
date = datetime.datetime.now()
year = int(date.strftime("%Y"))
month = int(date.strftime("%m"))
day = int(date.strftime("%d"))
week = cls.get_week_of_month(year, month, day)
return f"{year}{month}{week}"
@classmethod
def get_week_of_month(cls, year, month, day):
begin = int(datetime.date(year, month, 1).strftime("%W"))
end = int(datetime.date(year, month, day).strftime("%W"))
week = "{:0>2d}".format(end - begin + 1)
return week
class ComicPath:
WORK_PATH = os.path.abspath(os.curdir)
LOG_CONF_PATH = os.path.join(WORK_PATH,"logconf.yml")
COMIC_ICON_NAME = "000"
COMIC_ICON_SUFFIX = "jpg"
COMIC_ICON_FILE_NAME = COMIC_ICON_NAME+"."+COMIC_ICON_SUFFIX
DEFAULT_PATH = "default"
PROGRESS_PATH = "progress"
ICONS_PATH = "icons"
COMIC_INFO_XML = "ComicInfo.xml"
TIME_SLEEP = 0.5
IS_UPDATE_COMIC = False
COMIC_SCRABMLE_COUNT = 0
@classmethod
def getUrl(cls,relative_url):
if not str(relative_url).startswith("http"): relative_url = pathStr.getBaseUrl(Comic.getHomePage())+relative_url
return relative_url
#顶级路径
@classmethod
def setJoinPathDir(cls,path,dir="",suffix=None,mkdir=False):
result = dir
if isinstance(path,dict) or isinstance(path,list):
for x in path:
if x != None: result = os.path.join(result,x)
else: result = os.path.join(result,path)
if mkdir:
base_dir = os.path.dirname(result)
if not os.path.exists(base_dir):
os.makedirs(base_dir)
print(f"新路径 {result} mkdir={mkdir}")
if suffix != None: result += "."+suffix
return result
@classmethod
def setDirConf(cls,path,suffix=None,mkdir=False): return cls.setJoinPathDir(path,pathStr.base_conf_path(),suffix=suffix,mkdir=mkdir)
@classmethod
def setDirCBZ(cls,path,suffix=None,mkdir=False): return cls.setJoinPathDir(path,pathStr.base_cbz(),suffix=suffix,mkdir=mkdir)
@classmethod
def setDirImg(cls,path,suffix=None,mkdir=False): return cls.setJoinPathDir(path,pathStr.base_comic_img(),suffix=suffix,mkdir=mkdir)
@classmethod
def setDirOld(cls,path,suffix=None,mkdir=False): return cls.setJoinPathDir(path,pathStr.old_cbz_path,suffix=suffix,mkdir=mkdir)
#漫画配置文件路径
@classmethod
def getDirConfComic(cls): return cls.setDirConf(Comic.getOriginComicName())
#章节配置json文件路径
@classmethod
def getPathConfComicChapterJson(cls,mkdir=True): return cls.setDirConf([Comic.getOriginComicName(),Comic.getOriginChapterName()],suffix="json",mkdir=mkdir)
#漫画Icon路径
@classmethod
def getPathConfComicIcon(cls,mkdir=True): return cls.setDirConf([Comic.getOriginComicName(),cls.COMIC_ICON_NAME],suffix=cls.COMIC_ICON_SUFFIX,mkdir=mkdir)
#Icons json文件路径
@classmethod
def getDirConfDefault(cls,path=None,suffix=None,mkdir=True): return cls.setDirConf([cls.DEFAULT_PATH,path],suffix=suffix,mkdir=mkdir)
#漫画进度文件
@classmethod
def getPathProgressJson(cls,mkdir=True): return cls.setDirConf([cls.PROGRESS_PATH,Comic.getOriginComicName()],suffix="json",mkdir=mkdir)
#漫画CBZ路径
@classmethod
def getDirCBZComic(cls): return cls.setDirCBZ(Comic.getComicName())
#漫画章节CBZ路径
@classmethod
def getDirCBZComicChapter(cls,suffix=None): return cls.setDirCBZ([Comic.getComicName(),Comic.getChapterName()],suffix=suffix)
#漫画章节CBZ同根下封面
@classmethod
def getPathCBZComicChapterIcon(cls,suffix="jpg",mkdir=True): return cls.setDirCBZ([Comic.getComicName(),Comic.getChapterName()],suffix=suffix,mkdir=mkdir)
#旧版本漫画章节CBZ路径
@classmethod
def getPathOldOriginCBZComicChapter(cls): return cls.setDirOld([Comic.getOriginComicName(),Comic.getOriginChapterName()],suffix="CBZ")
@classmethod
def getPathOldCBZComicChapter(cls): return cls.setDirOld([Comic.getComicName(),Comic.getChapterName()],suffix="CBZ")
@classmethod
def getPathCBZComicChapter(cls): return cls.setDirCBZ([Comic.getComicName(),Comic.getChapterName()],suffix="CBZ")
#排序
@classmethod
def getSortDirCBZComicChapter(cls): return cls.setDirCBZ([Comic.getComicName()],str(Comic.getNumber())+" "+Comic.getChapterName())
@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): return cls.setDirImg(Comic.getComicName())
@classmethod
def getDirComicChapter(cls): return cls.setJoinPathDir(Comic.getChapterName(),cls.getDirComic())
@classmethod
def getPathComicInfoXML(cls,mkdir=True): return cls.setDirImg([Comic.getComicName(),Comic.getChapterName()
,cls.COMIC_INFO_XML],mkdir=mkdir)
#scramble图像路径
@classmethod
def getPathImageScrambleComicChapter(cls,count,block,mkdir=True,suffix="jpg"): return cls.setDirImg([Comic.getComicName(),Comic.getChapterName()
,"scramble="+block+"_"+count],mkdir=mkdir,suffix=suffix)
#
@classmethod
def getPathImageSaveScrambleComicChapter(cls,path,mkdir=True):
count = str(path).split("_")[-1]
return cls.setDirImg([Comic.getComicName(),Comic.getChapterName()
,count],mkdir=mkdir)
@classmethod
def getFileScrambleImageName(cls,count,block,suffix=".jpg"): return "scramble="+str(block)+"_"+str(count)+suffix
@classmethod
def getFileScrambleImageSave(cls,file): return str(file).split("_")[-1]