iShotaBot/modules/bilibili.py

53 lines
1.9 KiB
Python
Raw Normal View History

2021-11-28 14:55:10 +00:00
import re
2022-09-23 08:57:04 +00:00
from io import BytesIO
2021-11-28 14:55:10 +00:00
from pyrogram import Client, filters, ContinuePropagation
from pyrogram.types import Message
2022-09-23 08:51:38 +00:00
from defs.bilibili import b23_extract, video_info_get, binfo_image_create, get_dynamic_screenshot_pc
2021-11-28 14:55:10 +00:00
from defs.button import gen_button, Button
2022-09-23 08:57:04 +00:00
@Client.on_message(filters.incoming & filters.text &
2021-11-28 14:55:10 +00:00
filters.regex(r"av(\d{1,12})|BV(1[A-Za-z0-9]{2}4.1.7[A-Za-z0-9]{2})|b23.tv"))
2022-07-25 09:35:54 +00:00
async def bili_resolve(_: Client, message: Message):
2021-11-28 14:55:10 +00:00
"""
解析 bilibili 链接
"""
if "b23.tv" in message.text:
message.text = await b23_extract(message.text)
p = re.compile(r"av(\d{1,12})|BV(1[A-Za-z0-9]{2}4.1.7[A-Za-z0-9]{2})")
video_number = p.search(message.text)
if video_number:
2022-07-25 09:35:54 +00:00
video_number = video_number[0]
video_info = await video_info_get(video_number) if video_number else None
if video_info and video_info["code"] == 0:
image = binfo_image_create(video_info)
2022-09-23 08:51:38 +00:00
await message.reply_photo(
image,
quote=True,
reply_markup=gen_button([Button(0, "Link", "https://b23.tv/" + video_info["data"]["bvid"])])
)
raise ContinuePropagation
2022-09-23 08:57:04 +00:00
@Client.on_message(filters.incoming & filters.text &
2022-09-23 08:51:38 +00:00
filters.regex(r"t.bilibili.com/([0-9]*)"))
async def bili_dynamic(_: Client, message: Message):
# sourcery skip: use-named-expression
p = re.compile(r"t.bilibili.com/([0-9]*)")
dynamic_number = p.search(message.text)
if dynamic_number:
dynamic_id = dynamic_number[1]
image = await get_dynamic_screenshot_pc(dynamic_id)
if image:
2022-09-23 08:57:04 +00:00
# 将bytes结果转化为字节流
photo = BytesIO(image)
photo.name = "screenshot.png"
2022-09-23 08:51:38 +00:00
await message.reply_photo(
2022-09-23 08:57:04 +00:00
photo,
2022-09-23 08:51:38 +00:00
quote=True,
reply_markup=gen_button([Button(0, "Link", f"https://t.bilibili.com/{dynamic_id}")])
)
2021-11-28 14:55:10 +00:00
raise ContinuePropagation