Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/modules/weekly/__init__.py

38 lines
2.2 KiB
Python
Raw Normal View History

2021-04-05 00:41:50 +00:00
import json
2021-04-08 15:22:55 +00:00
import re
2021-04-05 00:41:50 +00:00
from graia.application import MessageChain
2021-04-05 09:20:08 +00:00
from graia.application.message.elements.internal import Image, Plain
2021-04-08 15:22:55 +00:00
2021-04-05 00:41:50 +00:00
from core.template import sendMessage
2021-04-08 15:22:55 +00:00
from core.utils import get_url
2021-04-05 00:41:50 +00:00
2021-04-05 09:20:08 +00:00
2021-04-05 00:41:50 +00:00
async def weekly(kwargs: dict):
try:
2021-04-08 15:22:55 +00:00
result = json.loads(await get_url(
'https://minecraft.fandom.com/zh/api.php?action=parse&page=Minecraft_Wiki/weekly&prop=text|revid&format=json'))
2021-04-05 00:41:50 +00:00
html = result['parse']['text']['*']
2021-04-08 15:22:55 +00:00
text = re.sub(r'<p>', '\n', html) # 分段
text = re.sub(r'<(.*?)>', '', text, flags=re.DOTALL) # 移除所有 HTML 标签
text = re.sub(r'\n\n\n', '\n\n', text) # 移除不必要的空行
2021-04-05 00:41:50 +00:00
text = re.sub(r'\n*$', '', text)
img = re.findall(r'(?<=src=")(.*?)(?=/revision/latest/scale-to-(width|height)-down/\d{3}\?cb=\d{14}?")', html)
page = re.findall(r'(?<=<b><a href=").*?(?=")', html)
2021-04-05 09:27:25 +00:00
sended_img = Image.fromNetworkAddress(img[0][0]) if img else Plain('\n(发生错误:图片获取失败)')
2021-04-08 15:22:55 +00:00
msg = '发生错误:本周页面已过期,请联系中文 Minecraft Wiki 更新。' if page[
0] == '/zh/wiki/%E7%8E%BB%E7%92%83' else '本周的每周页面:\n\n' + text + '\n图片:' + \
img[0][
0] + '?format=original\n\n页面链接https://minecraft.fandom.com' + \
page[
0] + '\n每周页面https://minecraft.fandom.com/zh/wiki/?oldid=' + str(
result['parse']['revid'])
2021-04-05 09:20:08 +00:00
await sendMessage(kwargs, MessageChain.create([Plain(msg), sended_img]))
2021-04-05 00:41:50 +00:00
except Exception as e:
await sendMessage(kwargs, '发生错误:' + str(e))
command = {'weekly': weekly}
2021-04-08 15:22:55 +00:00
help = {'weekly': {'module': '获取中文 Minecraft Wiki 的每周页面。', 'help': '''~weekly - 获取中文 Minecraft Wiki 的每周页面。'''}}