改用Yaml作为配置文件格式

This commit is contained in:
Womsxd 2022-08-23 23:42:46 +08:00
parent 19f44b402c
commit a91be638c1
No known key found for this signature in database
GPG Key ID: 0FE76418EE689B68
5 changed files with 119 additions and 104 deletions

105
config.py
View File

@ -1,12 +1,15 @@
import os import os
import json import json
from loghelper import log from loghelper import log
from ruamel.yaml import YAML
# 这个字段现在还没找好塞什么地方好就先塞config这里了 # 这个字段现在还没找好塞什么地方好就先塞config这里了
serverless = False serverless = False
# 提示需要更新config版本
update_config_need = False
config = { config = {
'enable': True, 'version': 5, 'enable': True, 'version': 6,
'account': { 'account': {
'cookie': '', 'cookie': '',
'login_ticket': '', 'login_ticket': '',
@ -15,11 +18,13 @@ config = {
}, },
'mihoyobbs': { 'mihoyobbs': {
'enable': True, 'checkin': True, 'checkin_multi': True, 'checkin_multi_list': [2, 5], 'enable': True, 'checkin': True, 'checkin_multi': True, 'checkin_multi_list': [2, 5],
'read_posts': True, 'like_posts': True, 'un_like': True, 'share_post': True 'read_posts': True, 'like_posts': True, 'cancel_like_posts': True, 'share_post': True
}, },
'games': { 'games': {
'cn': { 'cn': {
'enable': True, 'enable': True,
'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': []}, 'genshin': {'auto_checkin': True, 'black_list': []},
'hokai2': {'auto_checkin': False, 'black_list': []}, 'hokai2': {'auto_checkin': False, 'black_list': []},
'honkai3rd': {'auto_checkin': False, 'black_list': []}, 'honkai3rd': {'auto_checkin': False, 'black_list': []},
@ -32,70 +37,74 @@ config = {
} }
} }
path = os.path.dirname(os.path.realpath(__file__)) + "/config" path = os.path.dirname(os.path.realpath(__file__)) + "/config"
config_Path = f"{path}/config.json" config_Path_json = f"{path}/config.json"
config_Path = f"{path}/config.yaml"
def load_v4(data: dict): def load_config_json():
global config with open(config_Path_json, "r") as f:
# 配置开关
config["enable"] = data["enable_Config"]
# 账号 cookie
config["account"]["login_ticket"] = data["mihoyobbs_Login_ticket"]
config["account"]["stuid"] = data["mihoyobbs_Stuid"]
config["account"]["stoken"] = data["mihoyobbs_Stoken"]
config["account"]["cookie"] = data["mihoyobbs_Cookies"]
# bbs 相关设置(自己之前造的孽)
config["mihoyobbs"]["enable"] = data["mihoyobbs"]["bbs_Global"]
config["mihoyobbs"]["checkin"] = data["mihoyobbs"]["bbs_Signin"]
config["mihoyobbs"]["checkin_multi"] = data["mihoyobbs"]["bbs_Signin_multi"]
config["mihoyobbs"]["checkin_multi_list"] = data["mihoyobbs"]["bbs_Signin_multi_list"]
config["mihoyobbs"]["read_posts"] = data["mihoyobbs"]["bbs_Read_posts"]
config["mihoyobbs"]["like_posts"] = data["mihoyobbs"]["bbs_Like_posts"]
config["mihoyobbs"]["un_like"] = data["mihoyobbs"]["bbs_Unlike"]
config["mihoyobbs"]["share_post"] = data["mihoyobbs"]["bbs_Share"]
# 游戏相关设置 v4只支持原神和崩坏3所以其他选项默认关闭
config["games"]["cn"]["genshin"]["auto_checkin"] = data["genshin_Auto_sign"]
config["games"]["cn"]["honkai3rd"]["auto_checkin"] = data["honkai3rd_Auto_sign"]
def load_config():
global config
with open(config_Path, "r") as f:
data = json.load(f) data = json.load(f)
if data.get('version') == 5: if data.get('version') == 5:
config = data config_json = data
try: try:
config["mihoyobbs"]["like_post"] config_json["mihoyobbs"]["like_post"]
except KeyError: except KeyError:
pass pass
else: else:
config["mihoyobbs"]["read_posts"] = config["mihoyobbs"]["read_post"] config_json["mihoyobbs"]["read_posts"] = config_json["mihoyobbs"]["read_post"]
config["mihoyobbs"]["like_posts"] = config["mihoyobbs"]["like_post"] config_json["mihoyobbs"]["like_posts"] = config_json["mihoyobbs"]["like_post"]
del config["mihoyobbs"]["like_post"] del config_json["mihoyobbs"]["like_post"]
del config["mihoyobbs"]["read_post"] del config_json["mihoyobbs"]["read_post"]
save_config()
else: else:
load_v4(data) log.error("config版本过低请手动更新到基于yaml版本的新版本配置文件更新完成后请删除json版的配置文件")
log.info("升级v5 config") exit(1)
# 直接升级到v5 config 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() save_config()
f.close() 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加载完毕") log.info("Config加载完毕")
def save_config(): def save_config():
global serverless global serverless
yaml = YAML()
if serverless: if serverless:
log.info("云函数执行,无法保存") log.info("云函数执行,无法保存")
return None return None
with open(config_Path, "r+") as f: with open(config_Path, "w+") as f:
temp_text = json.dumps(config, sort_keys=False, indent=4, separators=(', ', ': '))
try: try:
f.seek(0) f.seek(0)
f.truncate() f.truncate()
f.write(temp_text) yaml.dump(config, f)
f.flush() f.flush()
except OSError: except OSError:
serverless = True serverless = True
@ -103,12 +112,12 @@ def save_config():
exit(-1) exit(-1)
else: else:
log.info("Config保存完毕") log.info("Config保存完毕")
f.close()
def clear_cookies(): def clear_cookies():
global config global config
global serverless global serverless
yaml = YAML()
if serverless: if serverless:
log.info("云函数执行,无法保存") log.info("云函数执行,无法保存")
return None return None
@ -118,18 +127,17 @@ def clear_cookies():
config["account"]["stuid"] = "" config["account"]["stuid"] = ""
config["account"]["stoken"] = "" config["account"]["stoken"] = ""
config["account"]["cookie"] = "CookieError" config["account"]["cookie"] = "CookieError"
temp_text = json.dumps(config, sort_keys=False, indent=4, separators=(', ', ': '))
try: try:
f.seek(0) f.seek(0)
f.truncate() f.truncate()
f.write(temp_text) yaml.dump(config, f)
f.flush() f.flush()
except OSError: except OSError:
serverless = True serverless = True
log.info("Cookie删除失败") log.info("Cookie删除失败")
else: else:
log.info("Cookie删除完毕") log.info("Cookie删除完毕")
f.close()
if __name__ == "__main__": if __name__ == "__main__":
# 初始化配置文件 # 初始化配置文件
@ -140,4 +148,5 @@ if __name__ == "__main__":
# except OSError: # except OSError:
# pass # pass
# save_config() # save_config()
# update_config()
pass pass

View File

@ -1,49 +0,0 @@
{
"enable": true,
"version": 5,
"account": {
"cookie": "",
"login_ticket": "",
"stuid": "",
"stoken": ""
},
"mihoyobbs": {
"enable": true,
"checkin": true,
"checkin_multi": true,
"checkin_multi_list": [2, 5],
"read_posts": true,
"like_posts": true,
"un_like": true,
"share_post": true
},
"games": {
"cn": {
"enable": true,
"hokai2": {
"auto_checkin": false,
"black_list": []
},
"honkai3rd": {
"auto_checkin": false,
"black_list": []
},
"tears_of_themis": {
"auto_checkin": false,
"black_list": []
},
"genshin": {
"auto_checkin": false,
"black_list": []
}
},
"os": {
"enable": false,
"cookie": "",
"genshin": {
"auto_checkin": false,
"black_list": []
}
}
}
}

View File

@ -0,0 +1,41 @@
enable: true #这里控制整个config文件是否启用
version: 6 #config的版本
account:
cookie: "" #登入账号只需要修改这里就行了,下面会自动获取
login_ticket: ""
stuid: ""
stoken: ""
mihoyobbs:
enable: true #控制bbs功能是否启用
checkin: true #社区签到
checkin_multi: true #多个社区签到,关闭后只签到大别野
checkin_multi_list: #签到的社区列表 详情可以看config文件夹下面的readme
- 2
- 5
read_posts: true #看帖
like_posts: true #点赞帖子
cancel_like_posts: true #取消点赞
share_post: true #分享帖子
games:
cn: #国服控制区域
enable: true #控制是否启用国内签到
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"
#配置签到用的ua
genshin: #原神自动签到
auto_checkin: true #控制是否启用签到,下面一致
black_list: [] #这里是不签到的账号,填入文本型数据 示例[“114514“,"1919810"] 下面的一样
hokai2: #崩坏2
auto_checkin: false
black_list: []
honkai3rd: #崩坏3
auto_checkin: false
black_list: []
tears_of_themis: #未定事件簿
auto_checkin: false
black_list: []
os: #外服控制区域,现在暂时无用
enable: false
cookie: ""
genshin:
auto_checkin: false
black_list: []

View File

@ -1,3 +1,4 @@
import os
import time import time
import push import push
import login import login
@ -103,6 +104,8 @@ def main():
if __name__ == "__main__": if __name__ == "__main__":
if not os.path.exists(config.config_Path):
config.update_config()
try: try:
status_code, message = main() status_code, message = main()
except CookieError: except CookieError:

View File

@ -11,10 +11,10 @@ from error import CookieError
# 搜索配置文件 # 搜索配置文件
def fund_config() -> list: def fund_config(ext: str) -> list:
file_name = [] file_name = []
for files in os.listdir(config.path): for files in os.listdir(config.path):
if os.path.splitext(files)[1] == '.json': if os.path.splitext(files)[1] == ext:
file_name.append(files) file_name.append(files)
return file_name return file_name
@ -22,9 +22,20 @@ def fund_config() -> list:
def main_multi(autorun: bool): def main_multi(autorun: bool):
log.info("AutoMihoyoBBS Multi User mode") log.info("AutoMihoyoBBS Multi User mode")
log.info("正在搜索配置文件!") log.info("正在搜索配置文件!")
config_list = fund_config() json_config = fund_config('.json')
if len(json_config) != 0:
log.info(f'已搜索到{len(json_config)}个旧版config文件正在进行升级')
for i in json_config:
log.info(f'开始处理{i}')
config.config_Path_json = f"{config.path}/{i}"
config.config_Path = f"{config.path}/{i[:-4]}yaml"
config.update_config()
if config.serverless:
push.push(1, "云函数环境请手动执行一下脚本更新config文件")
exit(1)
config_list = fund_config('.yaml')
if len(config_list) == 0: if len(config_list) == 0:
log.warning("未检测到配置文件请确认config文件夹存在.json后缀名的配置文件") log.warning("未检测到配置文件请确认config文件夹存在.yaml后缀名的配置文件!")
exit(1) exit(1)
if autorun: if autorun:
log.info(f"已搜索到{len(config_list)}个配置文件,正在开始执行!") log.info(f"已搜索到{len(config_list)}个配置文件,正在开始执行!")