forked from v2boardbot/v2boardbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.py
More file actions
253 lines (227 loc) · 8.43 KB
/
init.py
File metadata and controls
253 lines (227 loc) · 8.43 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import os
import requests
import yaml
from peewee import *
def print_log(log, type_='tips'):
if type_ == 'tips':
print(f'\033[32mTips: {log}\033[0m')
elif type_ == 'error':
print(f'\033[31mError: {log}\033[0m')
else:
print('Info', log)
def save_config(config, config_path='config.yaml'):
with open(config_path, "w") as yaml_file:
yaml.dump(config, yaml_file, default_style=None)
def check_database(config_path):
if os.path.exists(config_path):
with open(config_path, 'r', encoding='utf8') as fp:
config = yaml.safe_load(fp)
else:
config = {
'DATABASE': {}
}
if config.get('DATABASE'):
db = MySQLDatabase(**config.get('DATABASE'))
try:
db.connect()
print_log('Successfully connected to database')
db.close()
except:
print_log('Failed to connect to database', type_='error')
config['DATABASE'] = {}
save_config(config, config_path)
check_database(config_path)
else:
config['DATABASE'] = {}
config['DATABASE']['host'] = input('请输入数据库地址(默认:localhost) [localhost]:') or 'localhost'
config['DATABASE']['database'] = input('请输入数据库名:')
config['DATABASE']['user'] = input('请输入数据库用户名:')
config['DATABASE']['password'] = input('请输入数据库密码:')
save_config(config, config_path)
check_database(config_path)
def init_database(config_path):
from models import V2User, Db, BotDb, BotUser
Db.connect()
if os.path.exists('bot.db'):
res = BotDb.connect()
else:
res = BotDb.connect()
BotDb.create_tables([BotUser])
for v2_user in V2User.select():
if v2_user.telegram_id:
bot_user = BotUser.select().where(BotUser.telegram_id == v2_user.telegram_id).first()
if not bot_user: # 数据库绑定了,但是本地bot.db没有数据
BotUser.create(telegram_id=v2_user.telegram_id, v2_user=v2_user)
print(v2_user.telegram_id, '本地bot.db没有该绑定信息')
Db.close()
BotDb.close()
def check_telegram_connect(config_path):
if os.path.exists(config_path):
with open(config_path, 'r', encoding='utf8') as fp:
config = yaml.safe_load(fp)
else:
config = {
'TELEGRAM': {}
}
if config.get('TELEGRAM'):
# 电报连接测试
if config.get('TELEGRAM').get('http_proxy'):
os.environ['HTTP_PROXY'] = config.get('TELEGRAM').get('http_proxy')
os.environ['HTTPS_PROXY'] = config.get('TELEGRAM').get('https_proxy')
if not config.get('TELEGRAM').get('token'):
config['TELEGRAM']['token'] = input('请输入机器人Token:')
try:
res = requests.get(f'https://api.telegram.org/bot{config["TELEGRAM"]["token"]}/getMe')
if res.json()['ok'] == False:
print_log('Telegram token setting error', 'error')
config['TELEGRAM']['token'] = input('请输入机器人Token:')
save_config(config, config_path)
check_telegram_connect(config_path)
else:
save_config(config, config_path)
print_log(f'Welcome {res.json()["result"]["first_name"]} uses v2boardbot')
except Exception as e:
print_log(f'Telegram API connection failed:{e}', 'error')
config['TELEGRAM']['http_proxy'] = input('请输入代理地址:')
config['TELEGRAM']['https_proxy'] = config['TELEGRAM']['http_proxy']
save_config(config, config_path)
check_telegram_connect(config_path)
else:
config['TELEGRAM'] = {}
config['TELEGRAM']['token'] = input('请输入机器人Token:')
save_config(config, config_path)
check_telegram_connect(config_path)
def check_v2board(config_path):
if os.path.exists(config_path):
with open(config_path, 'r', encoding='utf8') as fp:
config = yaml.safe_load(fp)
else:
config = {
'WEBSITE': {}
}
if config.get('WEBSITE'):
api = config.get('WEBSITE').get('url') + '/api/v1/passport/auth/login'
data = {
'email': config.get('WEBSITE').get('email'),
'password': config.get('WEBSITE').get('password')
}
res = requests.post(api, data=data)
try:
auth_data = res.json()['data']['auth_data']
print_log(f'Airport administrator successfully logged in, Authorization: {auth_data}')
except:
print_log(f'Airport administrator login failed, result: {res.text}', 'error')
config['WEBSITE'] = {}
save_config(config, config_path)
check_v2board(config_path)
else:
config['WEBSITE'] = {}
while True:
url = input('请输入机场面板管理员地址:')
try:
protocol = url.split('//')[0] + '//'
host = url.split('//')[1].split('/')[0]
suffix = url.split('//')[1].split('/')[1].replace('#', '').replace('/', '')
config['WEBSITE']['url'] = protocol + host
config['WEBSITE']['suffix'] = suffix
break
except:
pass
config['WEBSITE']['email'] = input('请输入机场管理员邮箱:')
config['WEBSITE']['password'] = input('请输入机场管理员密码:')
save_config(config, config_path)
check_v2board(config_path)
def check_file(config_path):
if os.path.exists(config_path):
with open(config_path, 'r', encoding='utf8') as fp:
config = yaml.safe_load(fp)
if not config.get('TELEGRAM'):
config['TELEGRAM'] = {
'token': None,
'checkin': None,
'lucky': None,
'delete_message': 60,
'title': '尊敬的用户,欢迎使用v2boardbot\n项目地址:https://github.com/v2boardbot/v2boardbot\n"春风不写失意,梦醒仍寻旧忆。"'
}
if not config.get('GAME'):
config['GAME'] = {
'switch': False
}
if not config.get('TIGER'):
config['TIGER'] = {
'switch': False,
'rate': 20,
}
if not config.get('DICE'):
config['DICE'] = {
'switch': False,
'rate': 2,
}
if not config.get('BASKETBALL'):
config['BASKETBALL'] = {
'switch': False,
'rate': 3,
}
if not config.get('FOOTBALL'):
config['FOOTBALL'] = {
'switch': False,
'rate': 3,
}
if not config.get('BULLSEYE'):
config['BULLSEYE'] = {
'switch': False,
'rate': 1.1,
}
if not config.get('BOWLING'):
config['BOWLING'] = {
'switch': False,
'rate': 1.1,
}
save_config(config)
else:
config = {
'TELEGRAM': {
'checkin': None,
'lucky': None,
'delete_message': 60,
'title': '尊敬的用户,欢迎使用v2boardbot\n项目地址:https://github.com/v2boardbot/v2boardbot\n"春风不写失意,梦醒仍寻旧忆。"'
},
'GAME': {
'switch': False
},
'TIGER': {
'switch': False,
'rate': 20,
},
'DICE': {
'switch': False,
'rate': 2,
},
'BASKETBALL': {
'switch': False,
'rate': 3,
},
'FOOTBALL': {
'switch': False,
'rate': 3,
},
'BULLSEYE': {
'switch': False,
'rate': 1.1,
},
'BOWLING': {
'switch': False,
'rate': 1.1,
}
}
save_config(config)
def init(config_path='config.yaml'):
check_file(config_path)
check_database(config_path)
init_database(config_path)
check_telegram_connect(config_path)
check_v2board(config_path)
with open(config_path, 'r', encoding='utf8') as fp:
config = yaml.safe_load(fp)
return config
init()