-
Notifications
You must be signed in to change notification settings - Fork 150
/
server.py
191 lines (173 loc) · 6.63 KB
/
server.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
# Server Mod
import os
#import json
import time
import config
import threading
import main as single
import main_multi as multi
from loghelper import log
time_interval = 720 # 默认签到间隔时间,单位minute(分钟)
mod = 1 # 单用户模式/自动判断
def runingtime():
return int(time.time())
def control(time_interval, mod, event, detal):
last_time = runingtime()
while True:
now_time = runingtime()
if now_time > last_time + time_interval * 60:
last_time = runingtime()
if mod == 1:
try:
single.main()
except:
log.info("single_user start failed")
else:
try:
multi.main_multi(True)
except:
log.info("multi_user start failed")
if event.is_set():
log.info("Stoping threading")
break
if (detal.is_set()):
log.info("The Next check time is {}s".format(last_time - now_time + time_interval * 60))
time.sleep(20)
def command(detal):
global mod
global time_interval
global show
#show = False # 显示倒计时信息
#if show:
# detal.set()
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(*.yaml) enable(attribute) Ture(value)\ntime " \
"x:set interval time (minute)\nshow true/false:show the time count "
log.info(help)
while True:
command = input()
if command == "help" or command == "exit" or command == "?" or command == "":
log.info(help)
if command == "stop" or command == "exit":
log.info("Stoping Server Plase Wait")
return False
if command == "reload":
return True
if command == "test":
if mod==1:
try:
single.main()
except:
log.info("single_user start failed")
else:
try:
multi.main_multi(True)
except:
log.info("multi_user start failed")
if command == "single":
try:
single.main()
except:
log.info("single_user start failed")
if command == "mulit":
try:
multi.main_multi(True)
except:
log.info("multi_user start failed")
command = command.split(' ')
for i in range(0, len(command)):
if command[i] == "time":
if len(command) == 2:
time_interval = int(command[1])
log.info("switching interval to {} minute".format(time_interval))
return True
if command[i] == "mod":
if len(command) == 2:
mod_new = int(command[1])
if mod_new > 2 or mod_new < 1:
log.info("error mod")
else:
mod = mod_new
log.info("switching mod to {}".format(mod))
else:
log.info("Error Command")
if command[i] == "show":
if len(command) == 2:
if command[1] == "true":
detal.set()
log.info("switching to detail mod")
if command[1] == "false":
detal.clear()
log.info("switching to slient mod")
else:
log.info("Error Command")
if command[i] == "add":
cookie = ""
for m in range(i+1, len(command)):
cookie += command[m]
log.info("adding")
if mod == 1:
name = "config"
else:
log.info("Plase input your config name(*.yaml):")
name = input()
new_config = config.copy_config()
new_config['account']['cookie']=cookie
file_path = os.path.dirname(os.path.realpath(__file__)) + "/config/" + name + ".yaml"
try:
config.save_config(file_path,new_config)
log.info("Saving OK")
except:
log.info('Saving failed,plase check your file system')
#file = open(file_path, 'w')
#file.write(json.dumps(config))
#file.close()
if command[i] == "set":
if len(command) == 4:
file_path = os.path.dirname(os.path.realpath(__file__)) + "/config/" + command[1] + ".yaml"
if not os.path.exists(file_path):
log.info("User is not exist")
else:
new_config = config.load_config(file_path)
#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_config[command[2]] = value
try:
config.save_config(file_path,new_config)
log.info("Saving OK")
except:
log.info('Saving failed,plase check your file system')
#file = open(file_path, 'w')
#file.write(json.dumps(new_conifg))
#file.close()
return True
if __name__ == '__main__':
log.info('Running in Server Mod')
file_path = os.path.dirname(os.path.realpath(__file__)) + "/config/config.yaml"
if os.path.exists(file_path):
mod = 1
else:
mod = 2
while True:
log.info("switching to mod {}".format(mod))
t1_stop = threading.Event()
detal = threading.Event()
thread1 = threading.Thread(name='time_check', target=control, args=(time_interval, mod, t1_stop, detal))
thread1.start()
try:
if command(detal):
t1_stop.set()
continue
else:
t1_stop.set()
break
except:
t1_stop.set()
continue