75 lines
2.2 KiB
Python
75 lines
2.2 KiB
Python
import json
|
|
import requests,os
|
|
from utils.comic.PathStr import pathStr
|
|
|
|
class fileUtils:
|
|
comic_name = ""
|
|
|
|
#文件保存
|
|
@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)
|
|
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 + "文件写入失败"
|
|
return result
|
|
|
|
@classmethod
|
|
def save_conf(cls,file, data, mode=None):
|
|
file = os.path.join(cls.conf_path,file)
|
|
cls.file_save(file,data,mode)
|
|
|
|
@classmethod
|
|
def save_comic(cls,name, data, mode=None):
|
|
print("comic_save=", name)
|
|
cls.file_save(cls.get_utl_save_comic(name),data,mode)
|
|
|
|
@classmethod
|
|
def saveConfComicChapterInfo(cls,chapter,data,comic_name=None,mode=None):
|
|
cls.file_save(cls.getPathConfComicChapterInfo(chapter,comic_name), data,mode)
|
|
|
|
@classmethod
|
|
def getPathConfComicChapterInfo(cls,chapter,comic_name=None):
|
|
if comic_name == None:
|
|
comic_name = cls.comic_name
|
|
return os.path.join(pathStr.base_conf_path,comic_name,"info_"+chapter)
|
|
|
|
@classmethod
|
|
def getInfoConfComicChapter(cls,chapter,comic_name=None):
|
|
data = None
|
|
path = cls.getPathConfComicChapterInfo(chapter,comic_name)
|
|
with open(path,"r",encoding="utf-8") as fs:
|
|
data = json.loads(fs.read())
|
|
return data
|
|
|
|
@classmethod
|
|
def read_comic(cls,name):
|
|
file = os.path.join(cls.comic_path,name)
|
|
data = None
|
|
try:
|
|
with open(file,"r",encoding="utf-8") as fs:
|
|
data = json.loads(fs.read())
|
|
except:
|
|
print("文件出错了 file=", file)
|
|
return data
|
|
|
|
'''
|
|
返回漫画保存路径
|
|
'''
|
|
@classmethod
|
|
def get_utl_save_comic(cls,name):
|
|
file = os.path.join(pathStr.base_comic_img,name)
|
|
return file |