-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
312 lines (263 loc) · 10.5 KB
/
Copy path__init__.py
File metadata and controls
312 lines (263 loc) · 10.5 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
from nonebot.adapters.onebot.v11 import Bot, MessageEvent, MessageSegment
from nonebot.plugin import on_command, PluginMetadata, on_keyword
from nonebot import get_driver, logger, require
import nonebot
from datetime import datetime
from typing import List
import time
from os import makedirs, path
require("nonebot_plugin_apscheduler")
require("nonebot_plugin_htmlrender")
from nonebot_plugin_htmlrender import md_to_pic
from nonebot_plugin_apscheduler import scheduler
from .config import Config
from .utils import upd_luogu, upd_cf, upd_atc, upd_nowcoder, get_today, get_preluogu, get_luogu, get_precf, get_cf, get_preatc, get_atc, get_prenc, get_nc
driver = get_driver()
global_config = get_driver().config
config = Config.parse_obj(global_config)
__plugin_meta__ = PluginMetadata(
name="OCN",
description="OI Contest Notifier订阅洛谷/CF/Atcoder/牛客平台的比赛信息",
usage="luogu/preluogu(洛谷/洛谷历史) x 查询洛谷近期/历史的x场比赛(x默认为3)"
"cf/precf x 查询CF近期/历史的x场比赛(x默认为3)"
"atc/preatc x 查询Atcoder近期/历史的x场比赛(x默认为3)"
"nc/prenc(牛客/牛客历史) x 查询牛客近期/历史的x场比赛(x默认为3)"
"today(今日/今日比赛/今天/今天比赛) 查询所有平台今天的比赛"
"next(近期/近期比赛) x 查询所有平台近期的x场比赛(x默认为3)"
"update(手动更新) 手动更新缓存"
"help 输出指令帮助")
headers = {
'User-Agent':
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE'
}
@driver.on_startup
async def startup():
# check data folder
if not path.exists('data'):
makedirs('./data')
logger.info("自检成功!")
async def _update():
await upd_luogu()
await upd_cf()
await upd_atc()
await upd_nowcoder()
# 先写定时部分
# 定时自动更新缓存
#! 定时自动更新通知会向所有在.env.dev文件中配置了SUPERUSER的QQ号播报,config.py里有开关,默认开
@scheduler.scheduled_job('interval',
minutes=config.update_time,
id="auto_update")
async def auto_update():
try:
await _update()
logger.info("自动更新成功!")
if config.update_notifiy == True:
# 轮番给管理发送更新成功消息
for adid in global_config.superusers:
await bot.send_msg(message_type="private",
message="定时更新成功!\n" + "\n\n防风控编码" +
str(time.time()),
user_id=int(adid))
except Exception as e:
bot = nonebot.get_bot()
logger.warning("自动更新失败!")
logger.warning(e)
if config.update_notifiy == True:
# 轮番给管理发送更新失败消息
for adid in global_config.superusers:
await bot.send_msg(
message_type="private",
message=MessageSegment.image(
await md_to_pic("定时更新失败!\n" + e + "\n\n防风控编码" +
str(time.time()))),
user_id=int(adid))
# 每日八点一分向所有群聊播报今日比赛
#TODO 添加群聊/私聊订阅功能,只向订阅了的播报今日比赛
@scheduler.scheduled_job("cron",
hour=str(config.broadcast_hour),
minute=str(config.broadcast_minute),
id="auto_today")
async def auto_today():
bot = nonebot.get_bot()
group_list = await bot.get_group_list()
res = await get_today()
for group in group_list:
await bot.send_msg(message_type="group",
group_id=group['group_id'],
message=res)
# 手动更新
update = on_command('update', priority=1, aliases={'手动更新'})
@update.handle()
async def manual_update(event: MessageEvent):
try:
await _update()
await update.finish("更新成功" + "\n\n防风控编码" + str(time.time()))
except Exception as e:
logger.warning("手动更新失败!")
logger.warning(e)
await update.finish(
MessageSegment.image(await
md_to_pic("手动更新失败!\n" + e + "\n\n防风控编码" +
str(time.time()))))
# today今日比赛
today = on_command('today', priority=3, aliases={'今日', '今日比赛', '今天', '今天比赛'})
@today.handle()
async def cmd_today(event: MessageEvent):
try:
res = await get_today()
await today.finish(MessageSegment.image(await md_to_pic(res)))
except Exception as e:
logger.warning("查询失败!")
logger.warning(e)
await today.finish(
MessageSegment.image(await md_to_pic("查询失败!\n" + e + "\n\n防风控编码" +
str(time.time()))))
# 洛谷
preluogu = on_keyword(['preluogu', '洛谷历史'], priority=2, block=True)
luogu = on_keyword(['luogu', '洛谷'], priority=3)
@preluogu.handle()
async def cmd_preluogu(event: MessageEvent):
try:
counts = int(str(event.get_message()).split()[1])
except:
counts = 3
try:
res = await get_preluogu(counts)
await preluogu.finish(MessageSegment.image(await md_to_pic(res)))
except Exception as e:
logger.warning("查询失败!")
logger.warning(e)
await preluogu.finish(
MessageSegment.image(await md_to_pic("查询失败!\n" + e + "\n\n防风控编码" +
str(time.time()))))
@luogu.handle()
async def cmd_luogu(event: MessageEvent):
try:
counts = int(str(event.get_message()).split()[1])
except:
counts = 3
try:
res = await get_luogu(counts)
await luogu.finish(MessageSegment.image(await md_to_pic(res)))
except Exception as e:
logger.warning("查询失败!")
logger.warning(e)
await luogu.finish(
MessageSegment.image(await md_to_pic("查询失败!\n" + e + "\n\n防风控编码" +
str(time.time()))))
# CF
precf = on_keyword(['precf'], priority=2, block=True)
cf = on_keyword(['cf'], priority=3)
@precf.handle()
async def cmd_precf(event: MessageEvent):
try:
counts = int(str(event.get_message()).split()[1])
except:
counts = 3
try:
res = await get_precf(counts)
await precf.finish(MessageSegment.image(await md_to_pic(res)))
except Exception as e:
logger.warning("查询失败!")
logger.warning(e)
await precf.finish(
MessageSegment.image(await md_to_pic("查询失败!\n" + e + "\n\n防风控编码" +
str(time.time()))))
@cf.handle()
async def cmd_cf(event: MessageEvent):
try:
counts = int(str(event.get_message()).split()[1])
except:
counts = 3
try:
res = await get_cf(counts)
await cf.finish(MessageSegment.image(await md_to_pic(res)))
except Exception as e:
logger.warning("查询失败!")
logger.warning(e)
await cf.finish(
MessageSegment.image(await md_to_pic("查询失败!\n" + e + "\n\n防风控编码" +
str(time.time()))))
# atcoder
preatc = on_keyword(['preatc'], priority=2, block=True)
atc = on_keyword(['atc'], priority=3)
@preatc.handle()
async def cmd_preatc(event: MessageEvent):
try:
counts = int(str(event.get_message()).split()[1])
except:
counts = 3
try:
res = await get_preatc(counts)
await preatc.finish(MessageSegment.image(await md_to_pic(res)))
except Exception as e:
logger.warning("查询失败!")
logger.warning(e)
await preatc.finish(
MessageSegment.image(await md_to_pic("查询失败!\n" + e + "\n\n防风控编码" +
str(time.time()))))
@atc.handle()
async def cmd_atc(event: MessageEvent):
try:
counts = int(str(event.get_message()).split()[1])
except:
counts = 3
try:
res = await get_atc(counts)
await atc.finish(MessageSegment.image(await md_to_pic(res)))
except Exception as e:
logger.warning("查询失败!")
logger.warning(e)
await atc.finish(
MessageSegment.image(await md_to_pic("查询失败!\n" + e + "\n\n防风控编码" +
str(time.time()))))
# 牛客
prenc = on_keyword(['prenc', '牛客历史'], priority=2, block=True)
nc = on_keyword(['nc', '牛客'], priority=3)
@prenc.handle()
async def cmd_prenc(event: MessageEvent):
try:
counts = int(str(event.get_message()).split()[1])
except:
counts = 3
try:
res = await get_prenc(counts)
await prenc.finish(MessageSegment.image(await md_to_pic(res)))
except Exception as e:
logger.warning("查询失败!")
logger.warning(e)
await prenc.finish(
MessageSegment.image(await md_to_pic("查询失败!\n" + e + "\n\n防风控编码" +
str(time.time()))))
@nc.handle()
async def cmd_nc(event: MessageEvent):
try:
counts = int(str(event.get_message()).split()[1])
except:
counts = 3
try:
res = await get_nc(counts)
await nc.finish(MessageSegment.image(await md_to_pic(res)))
except Exception as e:
logger.warning("查询失败!")
logger.warning(e)
await nc.finish(
MessageSegment.image(await md_to_pic("查询失败!\n" + e + "\n\n防风控编码" +
str(time.time()))))
# next
_next = on_keyword(['next','近期','近期比赛'], priority=2)
@_next.handle()
async def cmd_next(event: MessageEvent):
try:
counts = int(str(event.get_message()).split()[1])
except:
counts = 3
try:
res = await get_next(counts)
await _next.finish(MessageSegment.image(await md_to_pic(res)))
except Exception as e:
logger.warning("查询失败!")
logger.warning(e)
await _next.finish(
MessageSegment.image(await md_to_pic("查询失败!\n" + e + "\n\n防风控编码" +
str(time.time()))))