forked from Womsxd/MihoyoBBSTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
192 lines (169 loc) · 5.61 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import os
import yaml
import setting
from loghelper import log
# 这个字段现在还没找好塞什么地方好,就先塞config这里了
serverless = False
# 提示需要更新config版本
update_config_need = False
config = {
'enable': True, 'version': 9,
'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, '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',
'genshin': {'auto_checkin': True, 'black_list': []},
'honkai2': {'auto_checkin': False, 'black_list': []},
'honkai3rd': {'auto_checkin': False, 'black_list': []},
'tears_of_themis': {'auto_checkin': False, 'black_list': []},
'honkai_sr': {'auto_checkin': False, 'black_list': []},
},
'os': {
'enable': False, 'cookie': '',
'genshin': {'auto_checkin': False, 'black_list': []},
'honkai_sr': {'auto_checkin': False, 'black_list': []}
}
},
'cloud_games': {
"genshin": {
'enable': False,
'token': ''
}
}
}
config_raw = {}
config_raw.update(config)
path = os.path.dirname(os.path.realpath(__file__)) + "/config"
if os.getenv("AutoMihoyoBBS_config_path") is not None:
path = os.getenv("AutoMihoyoBBS_config_path")
config_prefix = os.getenv("AutoMihoyoBBS_config_prefix")
if config_prefix is None:
config_prefix = ""
config_Path = f"{path}/{config_prefix}config.yaml"
def copy_config():
return config_raw
def config_v7_update(data: dict):
global update_config_need
update_config_need = True
data['version'] = 7
data['cloud_games'] = {"genshin": {'enable': False, 'token': ''}}
log.info("config已升级到: 7")
return data
def config_v8_update(data: dict):
global update_config_need
update_config_need = True
returns = config.copy()
returns["enable"] = data["enable"]
returns["account"].update(data["account"])
returns["mihoyobbs"].update(data["mihoyobbs"])
returns["cloud_games"].update(data["cloud_games"])
returns["games"]["os"].update(data["games"]["os"])
for i in data['games']['cn'].keys():
if i == "hokai2":
returns['games']['cn']['honkai2'].update(data['games']['cn']['hokai2'])
continue
returns['games']['cn'][i] = data['games']['cn'][i]
log.info("config已升级到: 8")
return returns
def config_v9_update(data: dict):
global update_config_need
update_config_need = True
data['version'] = 9
data['games']['os'] = {
'enable': False, 'cookie': '',
'genshin': {'auto_checkin': False, 'black_list': []},
'honkai_sr': {'auto_checkin': False, 'black_list': []}
}
log.info("config已升级到: 9")
return data
def load_config(p_path=None):
global config
if not p_path:
p_path = config_Path
with open(p_path, "r", encoding='utf-8') as f:
data = yaml.load(f, Loader=yaml.FullLoader)
if data['version'] == 9:
config = data
else:
if data['version'] == 6:
data = config_v7_update(data)
if data['version'] == 7:
config = config_v8_update(data)
if data['version'] == 8:
config = config_v9_update(data)
save_config()
log.info("Config加载完毕")
return config
def save_config(p_path=None, p_config=None):
global serverless
if serverless:
log.info("云函数执行,无法保存")
return None
if not p_path:
p_path = config_Path
if not p_config:
p_config = config
with open(p_path, "w+") as f:
try:
f.seek(0)
f.truncate()
f.write(yaml.dump(p_config, Dumper=yaml.Dumper, sort_keys=False))
f.flush()
except OSError:
serverless = True
log.info("Cookie保存失败")
exit(-1)
else:
log.info("Config保存完毕")
def clear_cookies():
global config
if serverless:
log.info("云函数执行,无法保存")
return None
config["enable"] = False
config["account"]["login_ticket"] = ""
config["account"]["stuid"] = ""
config["account"]["stoken"] = ""
config["account"]["cookie"] = "CookieError"
log.info("Cookie已删除")
save_config()
def clear_cookie_game(game_id: str):
global config
if serverless:
log.info("云函数执行,无法保存")
return None
config["account"]["cookie"] = "GameCookieError"
config["games"]["cn"][setting.game_id2config[game_id]]["auto_checkin"] = False
log.info(f"游戏签到Cookie已删除")
save_config()
def clear_cookie_cloudgame():
global config
if serverless:
log.info("云函数执行,无法保存")
return None
config['cloud_games']['genshin']["enable"] = False
config['cloud_games']['genshin']['token'] = ""
log.info("云原神Cookie删除完毕")
save_config()
if __name__ == "__main__":
# 初始化配置文件
# try:
# account_cookie = config['account']['cookie']
# config = load_config()
# config['account']['cookie'] = account_cookie
# except OSError:
# pass
# save_config()
# update_config()
pass