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
2023-06-05 12:10:27 +08:00

99 lines
3.9 KiB
Python

import re
import ujson as json
from html import unescape
from core.builtins import Bot
from core.builtins import Plain, Image, Url
from core.component import module
from core.utils.http import get_url
from core.utils.i18n import Locale
from core.utils.image import msgchain2image
from modules.wiki.utils.screenshot_image import generate_screenshot_v2
from modules.wiki.utils.wikilib import WikiLib
from .teahouse import get_rss as get_teahouse_rss
async def get_weekly(with_img=False, zh_tw=False):
locale = Locale('zh_cn' if not zh_tw else 'zh_tw')
result = json.loads(await get_url(
'https://minecraft.fandom.com/zh/api.php?action=parse&page=Minecraft_Wiki/weekly&prop=text|revid|images&format=json' +
('&variant=zh-tw' if zh_tw else ''),
200))
html = result['parse']['text']['*']
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) # 移除不必要的空行
text = re.sub(r'\n*$', '', text)
text = unescape(text)
img = result['parse']['images']
page = re.findall(r'(?<=<b><a href=").*?(?=")', html)
msg_list = [Plain(locale.t("weekly.message.error.expired") if page[
0] == '/zh/wiki/%E7%8E%BB%E7%92%83' else locale.t(
"weekly.message", text=text))]
imglink = None
if img:
get_image = await (WikiLib('https://minecraft.fandom.com/zh/wiki/')).parse_page_info('File:' + img[0])
if get_image.status:
imglink = get_image.file
msg_list.append(
Plain(
locale.t(
"weekly.message.link",
img=imglink if imglink is not None else locale.t("none"),
article=str(
Url(f'https://minecraft.fandom.com{page[0]}')),
link=str(
Url(f'https://minecraft.fandom.com/zh/wiki/?oldid={str(result["parse"]["revid"])}')))))
if imglink is not None and with_img:
msg_list.append(Image(path=imglink))
return msg_list
async def get_weekly_img(with_img=False, zh_tw=False):
locale = Locale('zh_cn' if not zh_tw else 'zh_tw')
img = await generate_screenshot_v2('https://minecraft.fandom.com/zh/wiki/Minecraft_Wiki/weekly' +
('?variant=zh-tw' if zh_tw else ''), content_mode=True, allow_special_page=True)
msg_ = []
if img:
msg_.append(Image(path=img))
if with_img:
result = json.loads(await get_url(
'https://minecraft.fandom.com/zh/api.php?action=parse&page=Minecraft_Wiki/weekly&prop=images&format=json' +
('&variant=zh-tw' if zh_tw else ''),
200))
img = result['parse']['images']
if img:
get_image = await (WikiLib('https://minecraft.fandom.com/zh/wiki/')).parse_page_info('File:' + img[0])
if get_image.status:
msg_.append(Plain(locale.t("weekly.message.image", img=get_image.file)))
return msg_
wky = module('weekly', developers=['Dianliang233'], support_languages=['zh_cn', 'zh_tw'])
@wky.handle('{{weekly.help}}')
async def _(msg: Bot.MessageSession):
weekly = await get_weekly(True if msg.target.clientName in ['QQ', 'TEST'] else False,
zh_tw=True if msg.locale.locale == 'zh_tw' else False)
await msg.finish(weekly)
@wky.handle('image {{weekly.help.image}}')
async def _(msg: Bot.MessageSession):
await msg.finish(await get_weekly_img(True if msg.target.clientName in ['QQ', 'TEST'] else False,
zh_tw=True if msg.locale.locale == 'zh_tw' else False))
@wky.handle('teahouse {{weekly.help.teahouse}}')
async def _(msg: Bot.MessageSession):
weekly = await get_teahouse_rss()
await msg.finish(weekly)
@wky.handle('teahouse image {{weekly.help.teahouse}}')
async def _(msg: Bot.MessageSession):
weekly = await get_teahouse_rss()
await msg.finish(Image(await msgchain2image([Plain(weekly)])))