2022-05-15 20:15:10 +08:00
|
|
|
|
import re
|
|
|
|
|
from nonebot import on_command, require, get_bot, logger
|
|
|
|
|
from nonebot.params import CommandArg
|
2022-05-29 12:45:41 +08:00
|
|
|
|
from nonebot.adapters.onebot.v11 import MessageEvent, MessageSegment, Message
|
2022-05-20 18:44:18 +08:00
|
|
|
|
from utils import aiorequests
|
|
|
|
|
from utils.file_handler import load_json, save_json
|
|
|
|
|
from utils.message_util import get_message_id
|
2022-05-15 20:15:10 +08:00
|
|
|
|
|
2022-06-09 21:57:56 +08:00
|
|
|
|
__paimon_help__ = {
|
|
|
|
|
'type': '娱乐',
|
|
|
|
|
'range': ['private', 'group', 'guild']
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-15 20:15:10 +08:00
|
|
|
|
news60s_pic = on_command('早报', aliases={'今日早报', '今日新闻', '60s读世界'}, priority=13, block=True)
|
2022-06-09 21:57:56 +08:00
|
|
|
|
news60s_pic.__paimon_help__ = {
|
|
|
|
|
"usage": "今日早报",
|
|
|
|
|
"introduce": "获取60秒读世界早报,后加on时间/off可以开启/关闭推送",
|
|
|
|
|
"priority": 10
|
|
|
|
|
}
|
2022-05-15 20:15:10 +08:00
|
|
|
|
|
|
|
|
|
scheduler = require('nonebot_plugin_apscheduler').scheduler
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@news60s_pic.handle()
|
2022-05-29 12:45:41 +08:00
|
|
|
|
async def news60s_pic_handler(event: MessageEvent, msg: Message = CommandArg()):
|
|
|
|
|
msg = msg.extract_plain_text().strip()
|
2022-05-15 20:15:10 +08:00
|
|
|
|
if not msg:
|
|
|
|
|
url = 'https://api.iyk0.com/60s/'
|
2022-05-19 18:15:56 +08:00
|
|
|
|
res = await aiorequests.get(url=url)
|
|
|
|
|
res = res.json()
|
|
|
|
|
await news60s_pic.finish(MessageSegment.image(file=res['imageUrl']))
|
2022-05-29 12:45:41 +08:00
|
|
|
|
elif msg.startswith(('开启', 'on', '打开')):
|
2022-05-15 20:15:10 +08:00
|
|
|
|
# 匹配msg中的xx:xx时间
|
2022-05-19 18:15:56 +08:00
|
|
|
|
time_str = re.search(r'(\d{1,2}):(\d{2})', msg)
|
2022-05-15 20:15:10 +08:00
|
|
|
|
if time_str:
|
2022-05-20 18:44:18 +08:00
|
|
|
|
push_data = load_json('news60s_push.json')
|
|
|
|
|
push_id = str(get_message_id(event))
|
2022-05-15 20:15:10 +08:00
|
|
|
|
push_data[push_id] = {
|
|
|
|
|
'type': event.message_type,
|
|
|
|
|
'hour': int(time_str.group(1)),
|
|
|
|
|
'minute': int(time_str.group(2))
|
|
|
|
|
}
|
|
|
|
|
if event.message_type == 'guild':
|
|
|
|
|
push_data[push_id]['guild_id'] = event.guild_id
|
2022-05-20 18:44:18 +08:00
|
|
|
|
if scheduler.get_job('60sNews' + str(get_message_id(event))):
|
|
|
|
|
scheduler.remove_job('60sNews' + str(get_message_id(event)))
|
2022-05-15 20:15:10 +08:00
|
|
|
|
scheduler.add_job(
|
|
|
|
|
func=news60s_push_task,
|
|
|
|
|
trigger='cron',
|
|
|
|
|
hour=int(time_str.group(1)),
|
|
|
|
|
minute=int(time_str.group(2)),
|
2022-05-20 18:44:18 +08:00
|
|
|
|
id='60sNews' + str(get_message_id(event)),
|
|
|
|
|
args=(str(get_message_id(event)),
|
2022-05-29 12:45:41 +08:00
|
|
|
|
push_data[str(get_message_id(event))]),
|
|
|
|
|
misfire_grace_time=10
|
2022-05-15 20:15:10 +08:00
|
|
|
|
)
|
2022-05-20 18:44:18 +08:00
|
|
|
|
save_json(push_data, 'news60s_push.json')
|
2022-05-15 20:15:10 +08:00
|
|
|
|
await news60s_pic.finish('开启60s读世界推送成功', at_sender=True)
|
|
|
|
|
else:
|
|
|
|
|
await news60s_pic.finish('请给出正确的时间,格式为12:00', at_sender=True)
|
2022-05-29 12:45:41 +08:00
|
|
|
|
elif msg.startswith(('关闭', 'off', 'close')):
|
2022-05-20 18:44:18 +08:00
|
|
|
|
push_data = load_json('news60s_push.json')
|
2022-05-29 13:02:49 +08:00
|
|
|
|
if str(get_message_id(event)) in push_data:
|
|
|
|
|
del push_data[str(get_message_id(event))]
|
|
|
|
|
if scheduler.get_job('60sNews' + str(get_message_id(event))):
|
|
|
|
|
scheduler.remove_job('60sNews' + str(get_message_id(event)))
|
|
|
|
|
save_json(push_data, 'news60s_push.json')
|
2022-05-15 20:15:10 +08:00
|
|
|
|
await news60s_pic.finish('关闭60s读世界推送成功', at_sender=True)
|
2022-05-29 13:02:49 +08:00
|
|
|
|
elif msg.startswith(('状态', 'status', 'setting')):
|
|
|
|
|
push_data = load_json('news60s_push.json')
|
|
|
|
|
if str(get_message_id(event)) not in push_data:
|
|
|
|
|
await news60s_pic.finish('当前会话未开启60秒读世界订阅', at_sender=True)
|
|
|
|
|
else:
|
|
|
|
|
reply_msg = f'60秒读世界订阅时间: {push_data[str(get_message_id(event))]["hour"]}:{push_data[str(get_message_id(event))]["minute"]:02d}'
|
|
|
|
|
await news60s_pic.finish(reply_msg, at_sender=True)
|
|
|
|
|
else:
|
|
|
|
|
await news60s_pic.finish('指令错误')
|
2022-05-15 20:15:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def news60s_push_task(push_id, push_data: dict):
|
|
|
|
|
try:
|
|
|
|
|
url = 'https://api.iyk0.com/60s/'
|
2022-05-19 18:15:56 +08:00
|
|
|
|
res = await aiorequests.get(url=url)
|
|
|
|
|
res = res.json()
|
2022-05-29 12:45:41 +08:00
|
|
|
|
if push_data['type'] == 'private':
|
|
|
|
|
api = 'send_private_msg'
|
|
|
|
|
data = {'user_id': push_id, 'message': MessageSegment.image(file=res['imageUrl'])}
|
2022-05-19 18:15:56 +08:00
|
|
|
|
elif push_data['type'] == 'guild':
|
2022-05-29 12:45:41 +08:00
|
|
|
|
api = 'send_guild_channel_msg'
|
|
|
|
|
data = {'guild_id': push_id, 'channel_id': push_data['channel_id'], 'message': MessageSegment.image(file=res['imageUrl'])}
|
|
|
|
|
else:
|
|
|
|
|
api = 'send_group_msg'
|
|
|
|
|
data = {'group_id': push_id, 'message': MessageSegment.image(file=res['imageUrl'])}
|
|
|
|
|
await get_bot().call_api(api, **data)
|
2022-05-19 18:15:56 +08:00
|
|
|
|
logger.info(f'{push_data["type"]}的{push_id}的60秒读世界推送成功')
|
2022-05-15 20:15:10 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.exception(f'{push_data["type"]}的{push_id}的60秒读世界推送失败:{e}')
|
|
|
|
|
|
|
|
|
|
|
2022-05-20 18:44:18 +08:00
|
|
|
|
for push_id, push_data in load_json('news60s_push.json').items():
|
2022-05-15 20:15:10 +08:00
|
|
|
|
scheduler.add_job(
|
|
|
|
|
func=news60s_push_task,
|
|
|
|
|
trigger='cron',
|
|
|
|
|
hour=push_data['hour'],
|
|
|
|
|
minute=push_data['minute'],
|
2022-05-19 18:15:56 +08:00
|
|
|
|
id='60sNews' + push_id,
|
2022-05-15 20:15:10 +08:00
|
|
|
|
args=(push_id,
|
2022-05-29 12:45:41 +08:00
|
|
|
|
push_data),
|
|
|
|
|
misfire_grace_time=10
|
2022-05-19 18:15:56 +08:00
|
|
|
|
)
|