-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
351 lines (317 loc) · 15.6 KB
/
main.py
File metadata and controls
351 lines (317 loc) · 15.6 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import datetime
import json
import logging
import pickle
import re
import traceback
import pytz
import uuid
from telegram import ParseMode
from telegram import Update
from telegram.ext import CallbackContext
from telegram.ext import CommandHandler
from telegram.ext import Defaults
from telegram.ext import Updater
from bit import Bit, BitInfoError
from data_storage import SqliteStorage
configs = json.load(open("config.json", 'r'))
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=configs['logging_level'])
db = SqliteStorage(configs['Sqlite_filename'])
with open("TOS.txt", 'r', encoding='UTF-8') as f:
TOS = f.read()
def get_scores_message(scores):
msg = ""
for i in scores:
info = scores[i]
msg += f"\n<b>{info['name']} - {info['id']}</b>\n学期:{info['term']}\n成绩:{info['score']}\n平均分:{info['average']}\n最高分:{info['max']}\n班级排名:{int(info['class_rank'] * 100)}%(第{round(info['class_rank'] * info['class_total'])}/{info['class_total']}位)\n专业排名:{int(info['majority_rank'] * 100)}%(第{round(info['majority_rank'] * info['majority_total'])}/{info['majority_total']}位)\n"
return msg
def get_score_update_of_user(tgid, refresh_all=False):
obj = db.get_obj(tgid)
if obj is None:
return "你还没有绑定学号,使用 /link 绑定后才能使用本功能"
bit = pickle.loads(obj)
updates = bit.get_scores_update(refresh_all)
logging.info(f"开始为{bit.username}更新成绩")
if len(updates) == 0:
return "没有新的成绩更新"
else:
db.save_obj(bit.username, bit.serialize(), tgid)
msg = "天啊天啊有新的成绩!!!\n"
msg += get_scores_message(updates)
return msg
def refresh_scores(context: CallbackContext):
logging.info("开始更新所有用户成绩")
ids = db.get_all_users()
for ID in ids:
msg = get_score_update_of_user(ID)
if msg.startswith("天啊天啊有新的成绩!!!"):
context.bot.send_message(chat_id=ID, text=msg)
def start_handler(update: Update, context: CallbackContext):
context.bot.send_message(chat_id=update.effective_chat.id,
text="这是你的BIT小秘书,我可以帮你查成绩,使用 /tos 查看我们的使用条款,继续使用视为你已经同意条款")
def tos_handler(update: Update, context: CallbackContext):
context.bot.send_message(chat_id=update.effective_chat.id, text=TOS)
def refresh_handler(update: Update, context: CallbackContext):
chat_id = update.effective_chat.id
try:
refresh_all = False
if len(context.args) > 0:
refresh_all = True
msg = get_score_update_of_user(chat_id, refresh_all)
for x in range(0, len(msg), 4096):
context.bot.send_message(chat_id=chat_id, text=msg[x:x + 4096])
except BitInfoError as e:
context.bot.send_message(chat_id=chat_id, text=str(e))
logging.error(f"from {chat_id}:{e}")
except Exception as e:
errid = uuid.uuid1()
logging.error(f"{errid}:{repr(e)}")
logging.error(traceback.format_exc())
context.bot.send_message(chat_id=chat_id, text=f"出现了未知错误,错误id {errid}")
def link_handler(update: Update, context: CallbackContext):
chat_id = update.effective_chat.id
obj = db.get_obj(chat_id)
if obj is not None:
context.bot.send_message(
chat_id=chat_id, text="你已经绑定成功,如需重新绑定请 /unlink 解绑之后重新绑定")
return
if len(context.args) != 2:
context.bot.send_message(chat_id=chat_id, text="使用格式:/link 学号 密码")
return
username = context.args[0]
password = context.args[1]
try:
bit = Bit(username, password)
bit.login()
db.save_obj(username, bit.serialize(), chat_id)
context.bot.send_message(chat_id=chat_id, text=f"成功绑定学号{username}")
except BitInfoError as e:
context.bot.send_message(chat_id=chat_id, text=str(e))
logging.error(f"from {chat_id}:{e}")
except Exception as e:
errid = uuid.uuid1()
logging.error(f"{errid}:{repr(e)}")
logging.error(traceback.format_exc())
context.bot.send_message(
chat_id=chat_id, text=f"绑定失败,出现了未知错误,错误id {errid}")
def unlink_handler(update: Update, context: CallbackContext):
chat_id = update.effective_chat.id
obj = db.get_obj(chat_id)
if obj is None:
context.bot.send_message(
chat_id=chat_id, text="你还没有绑定学号,使用 /link 绑定后才能使用本功能")
return
else:
db.delete_user(chat_id)
context.bot.send_message(chat_id=chat_id, text="解绑成功")
def info_handler(update: Update, context: CallbackContext):
chat_id = update.effective_chat.id
msg = f"你的chat id是:{chat_id}\n当前绑定状态:"
obj = db.get_obj(chat_id)
if obj is None:
msg += "未绑定"
else:
bit = pickle.loads(obj)
msg += f"已绑定 {bit.username}"
context.bot.send_message(chat_id=chat_id, text=msg)
def getscores_handler(update: Update, context: CallbackContext):
chat_id = update.effective_chat.id
obj = db.get_obj(chat_id)
if obj is None:
context.bot.send_message(
chat_id=chat_id, text="你还没有绑定学号,使用 /link 绑定后才能使用本功能")
return
else:
context.bot.send_message(chat_id=chat_id, text="请稍候,正在为你查询……")
try:
bit = pickle.loads(obj)
if len(context.args) == 0:
scores = bit.scores
else:
if len(context.args) > 1 or context.args[0] == 'help':
context.bot.send_message(
chat_id=chat_id, text="使用格式 /getscores [学期,如 2019-2020-1] 默认查询所有成绩")
return
else:
term = context.args[0]
years = re.findall(r'\d\d\d\d', term)
if int(years[1]) - int(years[0]) != 1:
context.bot.send_message(
chat_id=chat_id, text="学期格式有误")
return
scores = {
i: bit.scores[i] for i in bit.scores if bit.scores[i]['term'] == term}
msg = f"为你查询到{len(scores)}条结果:\n"
msg += get_scores_message(scores)
for x in range(0, len(msg), 4096):
context.bot.send_message(chat_id=chat_id, text=msg[x:x + 4096])
except BitInfoError as e:
context.bot.send_message(chat_id=chat_id, text=str(e))
logging.error(f"from {chat_id}:{e}")
except Exception as e:
errid = uuid.uuid1()
logging.error(f"{errid}:{repr(e)}")
logging.error(f"from {chat_id}: {update.message}")
logging.error(traceback.format_exc())
context.bot.send_message(
chat_id=chat_id, text=f"出现了未知错误,错误id {errid}")
def getclasses_handler(update: Update, context: CallbackContext):
chat_id = update.effective_chat.id
obj = db.get_obj(chat_id)
if obj is None:
context.bot.send_message(
chat_id=chat_id, text="你还没有绑定学号,使用 /link 绑定后才能使用本功能")
return
else:
try:
bit = pickle.loads(obj)
if len(context.args) == 0:
term = bit.get_current_term()
else:
if len(context.args) > 1 or context.args[0] == 'help' or not re.match(r'\d\d\d\d-\d\d\d\d-\d',
context.args[0]):
context.bot.send_message(
chat_id=chat_id, text="使用方法:/getclasses [ 学期,如 2019-2020-1 ] 默认查询当前学期")
return
term = context.args[0]
years = re.findall(r'\d\d\d\d', term)
if int(years[1]) - int(years[0]) != 1:
context.bot.send_message(chat_id=chat_id, text="学期格式有误")
return
context.bot.send_message(chat_id=chat_id, text="请稍候,正在为你查询……")
res = bit.get_term_classes_ics(term)
db.save_obj(bit.username, bit.serialize(), chat_id)
context.bot.send_message(
chat_id=chat_id, text=f"这是为你查询到的学期 {term} 课表")
context.bot.send_document(chat_id=chat_id, document=str(res).encode('UTF-8'),
filename=f"{bit.username}-{term}.ics")
except BitInfoError as e:
context.bot.send_message(chat_id=chat_id, text=str(e))
logging.error(f"from {chat_id}:{e}")
except Exception as e:
errid = uuid.uuid1()
logging.error(f"{errid}:{repr(e)}")
logging.error(f"from {chat_id}: {update.message}")
logging.error(traceback.format_exc())
context.bot.send_message(
chat_id=chat_id, text=f"出现了未知错误,错误id {errid}")
def getexams_handler(update: Update, context: CallbackContext):
chat_id = update.effective_chat.id
obj = db.get_obj(chat_id)
if obj is None:
context.bot.send_message(
chat_id=chat_id, text="你还没有绑定学号,使用 /link 绑定后才能使用本功能")
return
else:
try:
bit = pickle.loads(obj)
if len(context.args) == 0:
term = bit.get_current_term()
else:
if len(context.args) > 1 or context.args[0] == 'help' or not re.match(r'\d\d\d\d-\d\d\d\d-\d',
context.args[0]):
context.bot.send_message(
chat_id=chat_id, text="使用方法:/getexams [ 学期,如 2019-2020-1 ] 默认查询当前学期")
return
term = context.args[0]
years = re.findall(r'\d\d\d\d', term)
if int(years[1]) - int(years[0]) != 1:
context.bot.send_message(chat_id=chat_id, text="学期格式有误")
return
context.bot.send_message(chat_id=chat_id, text="请稍候,正在为你查询……")
res = bit.get_exams(term)
if len(res) == 0:
context.bot.send_message(
chat_id=chat_id, text=f"你在学期 {term} 暂无考试安排")
return
msg = f"这是为你查询到的学期 {term} 考试安排,共 {len(res)} 项\n"
for i in res:
msg += f"\n<b>{i['name']}</b>\n地点:{i['location']}\n时间:{i['begin'].strftime('%Y-%m-%d %H:%M')} - {i['end'].strftime('%Y-%m-%d %H:%M')}\n备注:{i['description'] if 'description' in i.keys() else '无'}\n"
res_ics = bit.get_exams_ics(term)
db.save_obj(bit.username, bit.serialize(), chat_id)
context.bot.send_message(chat_id=chat_id, text=msg)
context.bot.send_document(chat_id=chat_id, document=str(res_ics).encode('UTF-8'),
filename=f"{bit.username}-{term}-exams.ics")
except BitInfoError as e:
context.bot.send_message(chat_id=chat_id, text=str(e))
logging.error(f"from {chat_id}:{e}")
except Exception as e:
errid = uuid.uuid1()
logging.error(f"{errid}:{repr(e)}")
logging.error(f"from {chat_id}: {update.message}")
logging.error(traceback.format_exc())
context.bot.send_message(
chat_id=chat_id, text=f"出现了未知错误,错误id {errid}")
def getaverage_handler(update: Update, context: CallbackContext):
chat_id = update.effective_chat.id
obj = db.get_obj(chat_id)
if obj is None:
context.bot.send_message(
chat_id=chat_id, text="你还没有绑定学号,使用 /link 绑定后才能使用本功能")
return
else:
context.bot.send_message(chat_id=chat_id, text="请稍候,正在为你查询……")
try:
if len(context.args) > 1 or (
len(context.args) == 1 and (context.args[0] == 'help' or not re.match(r'\d\d\d\d-\d\d\d\d-\d',
context.args[0]))):
context.bot.send_message(
chat_id=chat_id, text="使用格式 /getaverage [学期,如 2019-2020-1] 默认查询所有成绩的加权均分")
return
bit = pickle.loads(obj)
msg = get_score_update_of_user(chat_id)
if msg.startswith("天啊天啊有新的成绩!!!"):
context.bot.send_message(chat_id=chat_id, text=msg)
if len(context.args) == 0:
term = bit.get_current_term()
else:
term = context.args[0]
years = re.findall(r'\d\d\d\d', term)
if int(years[1]) - int(years[0]) != 1:
context.bot.send_message(chat_id=chat_id, text="学期格式有误")
return
scores = {i: bit.scores[i] for i in bit.scores if
bit.scores[i]['term'] == term and bit.scores[i]['type'] != '校公选课'}
total = 0
total_credit = 0
for i in scores:
total += scores[i]['score'] * scores[i]['credit']
total_credit += scores[i]['credit']
if len(scores) == 0:
msg = f"你在学期 {term} 还没有考试成绩"
else:
msg = f"你在学期 {term} 共有 {len(scores)} 项考试成绩,均分为 {round(total / total_credit, 3)} 分"
context.bot.send_message(chat_id=chat_id, text=msg)
except BitInfoError as e:
context.bot.send_message(chat_id=chat_id, text=str(e))
logging.error(f"from {chat_id}:{e}")
except Exception as e:
errid = uuid.uuid1()
logging.error(f"{errid}:{repr(e)}")
logging.error(f"from {chat_id}: {update.message}")
logging.error(traceback.format_exc())
context.bot.send_message(
chat_id=chat_id, text=f"出现了未知错误,错误id {errid}")
def run():
defaults = Defaults(parse_mode=ParseMode.HTML,
tzinfo=pytz.timezone('Asia/Shanghai'))
updater = Updater(token=configs['bot_token'], use_context=True, defaults=defaults, request_kwargs={
'proxy_url': configs['proxy_url']})
job_queue = updater.job_queue
job_queue.run_daily(refresh_scores, datetime.time(0, 35, 0, 0))
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler('start', start_handler))
dispatcher.add_handler(CommandHandler('tos', tos_handler))
dispatcher.add_handler(CommandHandler('link', link_handler))
dispatcher.add_handler(CommandHandler('refresh', refresh_handler))
dispatcher.add_handler(CommandHandler('unlink', unlink_handler))
dispatcher.add_handler(CommandHandler('info', info_handler))
dispatcher.add_handler(CommandHandler('getscores', getscores_handler))
dispatcher.add_handler(CommandHandler('getclasses', getclasses_handler))
dispatcher.add_handler(CommandHandler('getexams', getexams_handler))
dispatcher.add_handler(CommandHandler('getaverage', getaverage_handler))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
run()