From c305ae87becafc2fe5aee6855fc52a3fc5e36d34 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Tue, 4 Oct 2022 15:40:46 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=96=B0=E5=A2=9E=E7=B1=B3?= =?UTF-8?q?=E5=93=88=E6=B8=B8=E8=AE=BA=E5=9D=9B=E6=88=AA=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- defs/mihoyo_bbs.py | 26 +++++++++++++++++++++++++ modules/mihoyo_bbs.py | 45 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 defs/mihoyo_bbs.py create mode 100644 modules/mihoyo_bbs.py 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