MihoyoBBSTools/config.py

153 lines
4.6 KiB
Python
Raw Normal View History

2021-05-23 13:24:20 +00:00
import os
import json
2022-01-06 05:49:25 +00:00
from loghelper import log
2022-08-23 15:42:46 +00:00
from ruamel.yaml import YAML
2021-05-23 13:24:20 +00:00
2022-04-26 07:59:28 +00:00
# 这个字段现在还没找好塞什么地方好就先塞config这里了
serverless = False
2022-08-23 15:42:46 +00:00
# 提示需要更新config版本
update_config_need = False
2022-04-26 07:59:28 +00:00
config = {
2022-08-23 15:42:46 +00:00
'enable': True, 'version': 6,
'account': {
'cookie': '',
'login_ticket': '',
'stuid': '',
'stoken': ''
},
'mihoyobbs': {
'enable': True, 'checkin': True, 'checkin_multi': True, 'checkin_multi_list': [2, 5],
2022-08-23 15:42:46 +00:00
'read_posts': True, 'like_posts': True, 'cancel_like_posts': True, 'share_post': True
},
'games': {
'cn': {
'enable': True,
2022-08-23 15:42:46 +00:00
'useragent': 'Mozilla/5.0 (Linux; Android 12; Unspecified Device) AppleWebKit/537.36 (KHTML, like Gecko) '
'Version/4.0 Chrome/103.0.5060.129 Mobile Safari/537.36',
'genshin': {'auto_checkin': True, 'black_list': []},
'hokai2': {'auto_checkin': False, 'black_list': []},
'honkai3rd': {'auto_checkin': False, 'black_list': []},
'tears_of_themis': {'auto_checkin': False, 'black_list': []},
},
'os': {
'enable': False, 'cookie': '',
'genshin': {'auto_checkin': False, 'black_list': []}
}
}
}
2021-05-24 08:16:52 +00:00
path = os.path.dirname(os.path.realpath(__file__)) + "/config"
2022-08-23 15:42:46 +00:00
config_Path_json = f"{path}/config.json"
config_Path = f"{path}/config.yaml"
2022-08-23 15:42:46 +00:00
def load_config_json():
with open(config_Path_json, "r") as f:
2021-06-06 12:57:28 +00:00
data = json.load(f)
if data.get('version') == 5:
2022-08-23 15:42:46 +00:00
config_json = data
2022-04-30 04:38:52 +00:00
try:
2022-08-23 15:42:46 +00:00
config_json["mihoyobbs"]["like_post"]
2022-04-30 04:38:52 +00:00
except KeyError:
pass
else:
2022-08-23 15:42:46 +00:00
config_json["mihoyobbs"]["read_posts"] = config_json["mihoyobbs"]["read_post"]
config_json["mihoyobbs"]["like_posts"] = config_json["mihoyobbs"]["like_post"]
del config_json["mihoyobbs"]["like_post"]
del config_json["mihoyobbs"]["read_post"]
else:
2022-08-23 15:42:46 +00:00
log.error("config版本过低请手动更新到基于yaml版本的新版本配置文件更新完成后请删除json版的配置文件")
exit(1)
log.info("v5Config加载完毕")
return config_json
def update_config():
global config
global update_config_need
update_config_need = True
log.info("正在更新config....")
config_json = load_config_json()
config['account'] = config_json['account']
config['mihoyobbs'].update(config_json['mihoyobbs'])
del config['mihoyobbs']['un_like']
config['mihoyobbs']['cancel_like_posts'] = config_json['mihoyobbs']['un_like']
for i in config_json['games']['cn'].keys():
if i == 'enable':
continue
config['games']['cn'][i] = config_json['games']['cn'][i]
config['games']['os'] = config_json['games']['os']
save_config()
log.info('config更新完毕')
if not serverless:
os.remove(config_Path_json)
else:
log.error("请本地更新一下config")
def load_config():
yaml = YAML()
global config
with open(config_Path, "r", encoding='utf-8') as f:
config = yaml.load(f)
log.info("Config加载完毕")
2021-05-23 13:24:20 +00:00
2021-10-25 14:53:34 +00:00
2022-01-06 05:49:25 +00:00
def save_config():
2022-04-26 08:15:47 +00:00
global serverless
2022-08-23 15:42:46 +00:00
yaml = YAML()
2022-04-28 14:28:16 +00:00
if serverless:
2022-04-26 07:59:28 +00:00
log.info("云函数执行,无法保存")
return None
2022-08-23 15:42:46 +00:00
with open(config_Path, "w+") as f:
2022-04-26 08:15:47 +00:00
try:
f.seek(0)
f.truncate()
2022-08-23 15:42:46 +00:00
yaml.dump(config, f)
2022-04-26 08:15:47 +00:00
f.flush()
except OSError:
serverless = True
log.info("Cookie保存失败")
exit(-1)
else:
log.info("Config保存完毕")
2021-05-23 13:24:20 +00:00
2021-10-25 14:53:34 +00:00
2022-01-06 05:49:25 +00:00
def clear_cookies():
2022-04-30 01:00:07 +00:00
global config
2022-04-26 08:15:47 +00:00
global serverless
2022-08-23 15:42:46 +00:00
yaml = YAML()
2022-04-28 14:28:16 +00:00
if serverless:
2022-04-26 07:59:28 +00:00
log.info("云函数执行,无法保存")
return None
2021-10-25 14:53:34 +00:00
with open(config_Path, "r+") as f:
2022-04-30 01:00:07 +00:00
config["enable"] = False
config["account"]["login_ticket"] = ""
config["account"]["stuid"] = ""
config["account"]["stoken"] = ""
config["account"]["cookie"] = "CookieError"
2022-04-26 08:15:47 +00:00
try:
f.seek(0)
f.truncate()
2022-08-23 15:42:46 +00:00
yaml.dump(config, f)
2022-04-26 08:15:47 +00:00
f.flush()
except OSError:
serverless = True
log.info("Cookie删除失败")
else:
log.info("Cookie删除完毕")
2022-08-23 15:42:46 +00:00
if __name__ == "__main__":
# 初始化配置文件
# try:
# account_cookie = config['account']['cookie']
# config = load_config()
# config['account']['cookie'] = account_cookie
# except OSError:
# pass
# save_config()
2022-08-23 15:42:46 +00:00
# update_config()
pass