iShotaBot/modules/bilibili.py

89 lines
2.9 KiB
Python
Raw Permalink 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
2024-08-19 13:44:43 +00:00
from bilibili_api import ResponseCodeException
2021-11-28 14:55:10 +00:00
from pyrogram import Client, filters, ContinuePropagation
from pyrogram.types import Message
2023-01-12 13:19:54 +00:00
from defs.bilibili import (
b23_extract,
video_info_get,
binfo_image_create,
get_dynamic_screenshot_pc,
2023-08-05 05:33:03 +00:00
check_and_refresh_credential,
2023-01-12 13:19:54 +00:00
)
2021-11-28 14:55:10 +00:00
from defs.button import gen_button, Button
from defs.glover import bili_auth_user, bili_auth_chat
from init import bot
2024-08-19 13:44:43 +00:00
from scheduler import scheduler, add_delete_message_job
2021-11-28 14:55:10 +00:00
@bot.on_message(
2023-01-12 13:19:54 +00:00
filters.incoming
& filters.text
2024-08-19 13:25:26 +00:00
& filters.regex(r"av(\d{1,12})|BV(\w{10})|b23.tv")
2023-08-18 14:26:05 +00:00
& ~(
filters.command(["download", "bilibili_fav"])
& (filters.user(bili_auth_user) | filters.chat(bili_auth_chat))
)
2023-01-12 13:19:54 +00:00
)
2022-07-25 09:35:54 +00:00
async def bili_resolve(_: Client, message: Message):
2021-11-28 14:55:10 +00:00
"""
2023-01-12 13:19:54 +00:00
解析 bilibili 链接
2021-11-28 14:55:10 +00:00
"""
if "b23.tv" in message.text:
message.text = await b23_extract(message.text)
2024-08-19 13:25:26 +00:00
p = re.compile(r"av(\d{1,12})|BV(\w{10})|b23.tv")
2021-11-28 14:55:10 +00:00
video_number = p.search(message.text)
if video_number:
2022-07-25 09:35:54 +00:00
video_number = video_number[0]
2024-08-19 13:44:43 +00:00
try:
video_info = await video_info_get(video_number) if video_number else None
except ResponseCodeException as e:
text = f"视频解析失败:[{e.code}] {e.msg}"
reply = await message.reply(text, quote=True)
add_delete_message_job(reply)
raise ContinuePropagation
2023-05-27 13:45:56 +00:00
if video_info:
2023-05-27 12:55:04 +00:00
image = await binfo_image_create(video_info)
buttons = [Button(0, "Link", "https://b23.tv/" + video_info["bvid"])]
if (message.from_user and message.from_user.id in bili_auth_user) or (
message.chat and message.chat.id in bili_auth_chat
):
buttons.append(Button(1, "Download", "download_" + video_info["bvid"]))
2022-09-23 08:51:38 +00:00
await message.reply_photo(
image,
quote=True,
reply_markup=gen_button(buttons),
2022-09-23 08:51:38 +00:00
)
raise ContinuePropagation
@bot.on_message(
2023-01-12 13:19:54 +00:00
filters.incoming & filters.text & filters.regex(r"t.bilibili.com/([0-9]*)")
)
2022-09-23 08:51:38 +00:00
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,
2023-01-12 13:19:54 +00:00
reply_markup=gen_button(
[Button(0, "Link", f"https://t.bilibili.com/{dynamic_id}")]
),
2022-09-23 08:51:38 +00:00
)
2021-11-28 14:55:10 +00:00
raise ContinuePropagation
2023-08-05 05:33:03 +00:00
@scheduler.scheduled_job("interval", hours=1, id="bili_cookie_refresh")
async def bili_cookie_refresh():
await check_and_refresh_credential()