MihoyoBBSTools/server.py

203 lines
7.2 KiB
Python
Raw Normal View History

2022-08-23 15:55:18 +00:00
# Server Mod
2022-08-05 09:09:03 +00:00
import os
2022-08-23 15:55:18 +00:00
import json
2022-08-05 09:09:03 +00:00
import time
import threading
import main as single
import main_multi as multi
from loghelper import log
2022-08-23 15:55:18 +00:00
time_interval = 720 # 默认签到间隔时间单位minute(分钟)
mod = 1 # 单用户模式/自动判断
2022-08-05 09:09:03 +00:00
def runingtime():
return int(time.time())
2022-08-23 15:55:18 +00:00
def control(time_interval, mod, event, detal):
last_time = runingtime()
2022-08-05 09:09:03 +00:00
while True:
2022-08-23 15:55:18 +00:00
now_time = runingtime()
if now_time > last_time + time_interval * 60:
if mod == 1:
2022-08-05 09:09:03 +00:00
try:
single.main()
except:
log.info("single_user start failed")
else:
try:
multi.main_multi(True)
except:
log.info("multi_user start failed")
2022-08-23 15:55:18 +00:00
last_time = runingtime()
2022-08-05 09:09:03 +00:00
if event.is_set():
2022-08-05 10:01:07 +00:00
log.info("Stoping threading")
2022-08-05 09:09:03 +00:00
break
2022-08-23 15:55:18 +00:00
if (detal.is_set()):
log.info("The Next check time is {}s".format(last_time - now_time + time_interval * 60))
2022-08-05 09:09:03 +00:00
time.sleep(20)
2022-08-23 15:55:18 +00:00
2022-08-05 17:46:31 +00:00
def command(detal):
2022-08-05 09:09:03 +00:00
global mod
global time_interval
2022-08-05 17:46:31 +00:00
global show
2022-08-23 15:55:18 +00:00
show = False # 显示倒计时信息
2022-08-05 17:46:31 +00:00
if show:
detal.set()
2022-08-23 15:55:18 +00:00
help = "command windows\nstop:stop server\nreload:reload config and refish tiem\nsingle:test single " \
"config\nmulit:test mulit conifg\nmod x:x is refer single or multi 1 is single 2 is multi\nadd " \
"'yourcookie'\nset user attribute value: such set username(*.json) enable(attribute) Ture(value)\ntime " \
"x:set interval time (minute)\nshow true/false:show the time count "
2022-08-05 09:09:03 +00:00
log.info(help)
2022-08-23 15:55:18 +00:00
while True:
command = input()
if command == "help" or command == "?" or command == "":
2022-08-05 09:09:03 +00:00
log.info(help)
2022-08-23 15:55:18 +00:00
if command == "stop" or command == "exit":
2022-08-05 10:01:07 +00:00
log.info("Stoping Server Plase Wait")
2022-08-05 09:09:03 +00:00
return False
2022-08-23 15:55:18 +00:00
if command == "reload":
2022-08-05 09:09:03 +00:00
return True
2022-08-23 15:55:18 +00:00
if command == "test":
2022-08-05 09:09:03 +00:00
try:
single.main()
except:
log.info("single_user start failed")
2022-08-23 15:55:18 +00:00
if command == "single":
2022-08-05 09:09:03 +00:00
try:
single.main()
except:
log.info("single_user start failed")
2022-08-23 15:55:18 +00:00
if command == "mulit":
2022-08-05 09:09:03 +00:00
try:
multi.main_multi(True)
except:
log.info("multi_user start failed")
2022-08-23 15:55:18 +00:00
command = command.split(' ')
for i in range(0, len(command)):
if command[i] == "time":
if len(command) == 2:
time_interval = int(command[1])
2022-08-05 10:01:07 +00:00
log.info("switching interval to {} minute".format(time_interval))
return True
2022-08-23 15:55:18 +00:00
if command[i] == "mod":
if len(command) == 2:
mod_new = int(command[1])
if mod_new > 2 or mod_new < 1:
2022-08-05 09:09:03 +00:00
log.info("error mod")
else:
2022-08-23 15:55:18 +00:00
mod = mod_new
2022-08-05 09:09:03 +00:00
log.info("switching mod to {}".format(mod))
2022-08-05 17:46:31 +00:00
else:
log.info("Error Command")
2022-08-23 15:55:18 +00:00
if command[i] == "show":
if len(command) == 2:
if command[1] == "true":
show = True
2022-08-05 17:46:31 +00:00
detal.set()
log.info("switching to detail mod")
2022-08-23 15:55:18 +00:00
if command[1] == "false":
2022-08-05 17:46:31 +00:00
detal.unset()
2022-08-23 15:55:18 +00:00
show = False
2022-08-05 17:46:31 +00:00
log.info("switching to slient mod")
2022-08-23 15:55:18 +00:00
2022-08-05 09:09:03 +00:00
else:
log.info("Error Command")
2022-08-23 15:55:18 +00:00
if command[i] == "add":
cookie = ""
for m in range(i, len(command)):
cookie += command[m]
2022-08-05 10:01:07 +00:00
log.info("adding")
2022-08-23 15:55:18 +00:00
if mod == 1:
name = "config"
2022-08-05 10:01:07 +00:00
else:
log.info("Plase input your config name(*.json):")
2022-08-23 15:55:18 +00:00
name = input()
2022-08-05 10:01:07 +00:00
config = {
2022-08-05 09:09:03 +00:00
'enable': True, 'version': 5,
'account': {
2022-08-23 15:55:18 +00:00
'cookie': 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
2022-08-05 09:09:03 +00:00
},
2022-08-23 15:55:18 +00:00
'games': {
'cn': {
'enable': True,
'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': []},
2022-08-05 09:09:03 +00:00
},
2022-08-23 15:55:18 +00:00
'os': {
'enable': False, 'cookie': '',
'genshin': {'auto_checkin': False, 'black_list': []}
2022-08-05 09:09:03 +00:00
}
2022-08-23 15:55:18 +00:00
}
}
file_path = os.path.dirname(os.path.realpath(__file__)) + "/config/" + name + ".json"
file = open(file_path, 'w')
2022-08-05 10:01:07 +00:00
file.write(json.dumps(config))
file.close()
log.info("Saving OK")
2022-08-23 15:55:18 +00:00
if command[i] == "set":
if len(command) == 4:
file_path = os.path.dirname(os.path.realpath(__file__)) + "/config/" + command[1] + ".json"
if not os.path.exists(file_path):
2022-08-05 09:09:03 +00:00
log.info("User is not exist")
else:
with open(file_path, "r") as f:
2022-08-23 15:55:18 +00:00
new_conifg = json.load(f)
value = command[3]
if command[3] == "true":
value = True
if command[3] == "false":
value = False
if command[3].isdigit():
value = int(command[3])
new_conifg[command[2]] = value
file = open(file_path, 'w')
2022-08-05 09:09:03 +00:00
file.write(json.dumps(new_conifg))
file.close()
2022-08-05 10:01:07 +00:00
return True
2022-08-05 09:09:03 +00:00
2022-08-23 15:55:18 +00:00
if __name__ == '__main__':
2022-08-05 09:09:03 +00:00
log.info('Running in Server Mod')
time_interval = 720
file_path = os.path.dirname(os.path.realpath(__file__)) + "/config/config.json"
if os.path.exists(file_path):
2022-08-23 15:55:18 +00:00
mod = 1
2022-08-05 09:09:03 +00:00
else:
2022-08-23 15:55:18 +00:00
mod = 2
2022-08-05 09:09:03 +00:00
while True:
log.info("switching to mod {}".format(mod))
t1_stop = threading.Event()
2022-08-05 17:46:31 +00:00
detal = threading.Event()
2022-08-23 15:55:18 +00:00
thread1 = threading.Thread(name='time_check', target=control, args=(time_interval, mod, t1_stop, detal))
2022-08-05 09:09:03 +00:00
thread1.start()
2022-08-05 10:01:07 +00:00
try:
2022-08-05 17:46:31 +00:00
if command(detal):
2022-08-05 10:01:07 +00:00
t1_stop.set()
continue
else:
t1_stop.set()
break
except:
2022-08-05 09:09:03 +00:00
t1_stop.set()
continue