diff --git a/defs/mihoyo_bbs.py b/defs/mihoyo_bbs.py new file mode 100644 index 0000000..0c88ffa --- /dev/null +++ b/defs/mihoyo_bbs.py @@ -0,0 +1,26 @@ +from defs.browser import get_browser + + +async def get_mihoyo_screenshot(url): + browser = await get_browser() + context = await browser.new_context( + viewport={"width": 2560, "height": 1080}, + device_scale_factor=2, + ) + page = await context.new_page() + try: + await page.goto(url, wait_until="networkidle", timeout=60000) + # 被删除或者进审核了 + if page.url == "https://bbs.mihoyo.com/ys/404": + return None + card = await page.query_selector(".mhy-article-page__main") + assert card + clip = await card.bounding_box() + assert clip + clip["width"] += 310 + return await page.screenshot(clip=clip, full_page=True) + except Exception as e: + print(f"截取米哈游帖子时发生错误:{e}") + return await page.screenshot(full_page=True) + finally: + await context.close() diff --git a/modules/mihoyo_bbs.py b/modules/mihoyo_bbs.py new file mode 100644 index 0000000..028d623 --- /dev/null +++ b/modules/mihoyo_bbs.py @@ -0,0 +1,45 @@ +import re +from io import BytesIO + +from pyrogram import Client, filters, ContinuePropagation +from pyrogram.types import Message + +from PIL import Image + +from defs.mihoyo_bbs import get_mihoyo_screenshot +from defs.button import gen_button, Button + + +@Client.on_message(filters.incoming & filters.text & + filters.regex(r'(https://)?(m\.)?bbs.mihoyo.com/.+/article/\d+')) +async def bili_dynamic(_: Client, message: Message): + # sourcery skip: use-named-expression + try: + p = re.compile(r'(https://)?(m\.)?bbs.mihoyo.com/.+/article/\d+') + article = p.search(message.text) + if article: + article_url = article.group() + if not article_url.startswith(('https://', 'http://')): + article_url = f'https://{article_url}' + image = await get_mihoyo_screenshot(article_url) + if image: + # 将bytes结果转化为字节流 + photo = BytesIO(image) + photo.name = "screenshot.png" + pillow_photo = Image.open(BytesIO(image)) + width, height = pillow_photo.size + if abs(height - width) > 1300: + await message.reply_document( + document=photo, + quote=True, + reply_markup=gen_button([Button(0, "Link", article_url)]) + ) + else: + await message.reply_photo( + photo, + quote=True, + reply_markup=gen_button([Button(0, "Link", article_url)]) + ) + except Exception as e: + print(f"截取米哈游帖子时发生错误:{e}") + raise ContinuePropagation