支持群组内显示下载按钮

This commit is contained in:
xtaodada 2023-08-18 17:26:57 +08:00
parent 9e1d5cedaa
commit 8f0ab8746f
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
4 changed files with 35 additions and 10 deletions

View File

@ -9,6 +9,7 @@ import aiofiles
from bilibili_api import HEADERS from bilibili_api import HEADERS
from bilibili_api.video import Video, VideoDownloadURLDataDetecter, VideoQuality from bilibili_api.video import Video, VideoDownloadURLDataDetecter, VideoQuality
from httpx import AsyncClient, Response from httpx import AsyncClient, Response
from pyrogram.enums import ParseMode
from pyrogram.types import Message from pyrogram.types import Message
from defs.request import cache_dir from defs.request import cache_dir
@ -203,10 +204,9 @@ async def get_video_height_width(path: str) -> Tuple[int, int]:
return int(video_height), int(video_width) return int(video_height), int(video_width)
async def take_screenshot(v: Video) -> Optional[BytesIO]: async def take_screenshot(info: Dict) -> Optional[BytesIO]:
"""获取视频封面""" """获取视频封面"""
try: try:
info = await v.get_info()
pic_get = (await request.get(info["pic"])).content pic_get = (await request.get(info["pic"])).content
pic = BytesIO(pic_get) pic = BytesIO(pic_get)
pic.name = "screenshot.jpg" pic.name = "screenshot.jpg"
@ -291,11 +291,19 @@ async def go_upload(v: Video, p_num: int, m: Message):
try: try:
video_duration = await get_video_duration(video_path) video_duration = await get_video_duration(video_path)
video_height, video_width = await get_video_height_width(video_path) video_height, video_width = await get_video_height_width(video_path)
video_jpg = await take_screenshot(v) try:
info = await v.get_info()
video_jpg = await take_screenshot(info)
caption = f"<b>{info['title']}</b>\n\n{info['desc']}\n\nhttps://b23.tv/{v.get_bvid()}"
except Exception:
video_jpg = None
caption = f"https://b23.tv/{v.get_bvid()}"
logger.info(f"Uploading {video_path}") logger.info(f"Uploading {video_path}")
await bot.send_video( await bot.send_video(
chat_id=m.chat.id, chat_id=m.chat.id,
video=str(video_path), video=str(video_path),
caption=caption,
parse_mode=ParseMode.HTML,
duration=int(video_duration), duration=int(video_duration),
width=video_width, width=video_width,
height=video_height, height=video_height,

View File

@ -25,4 +25,4 @@ def gen_button(data: List) -> InlineKeyboardMarkup:
buttons_callback.append( buttons_callback.append(
InlineKeyboardButton(text=button.name, callback_data=button.data) InlineKeyboardButton(text=button.name, callback_data=button.data)
) )
return InlineKeyboardMarkup(inline_keyboard=[buttons_callback, buttons_url]) return InlineKeyboardMarkup(inline_keyboard=[buttons_url, buttons_callback])

View File

@ -12,6 +12,7 @@ from defs.bilibili import (
check_and_refresh_credential, check_and_refresh_credential,
) )
from defs.button import gen_button, Button from defs.button import gen_button, Button
from defs.glover import bili_auth_user
from init import bot from init import bot
from scheduler import scheduler from scheduler import scheduler
@ -35,12 +36,13 @@ async def bili_resolve(_: Client, message: Message):
video_info = await video_info_get(video_number) if video_number else None video_info = await video_info_get(video_number) if video_number else None
if video_info: if video_info:
image = await binfo_image_create(video_info) 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:
buttons.append(Button(1, "Download", "download_" + video_info["bvid"]))
await message.reply_photo( await message.reply_photo(
image, image,
quote=True, quote=True,
reply_markup=gen_button( reply_markup=gen_button(buttons),
[Button(0, "Link", "https://b23.tv/" + video_info["bvid"])]
),
) )
raise ContinuePropagation raise ContinuePropagation

View File

@ -1,9 +1,9 @@
import re import re
from pyrogram import filters, Client, ContinuePropagation from pyrogram import filters, Client, ContinuePropagation
from pyrogram.types import Message from pyrogram.types import Message, CallbackQuery
from defs.bilibili import b23_extract, video_info_get, create_video from defs.bilibili import b23_extract, create_video
from defs.bilibili_download import go_download from defs.bilibili_download import go_download
from defs.glover import bili_auth_user from defs.glover import bili_auth_user
from init import bot from init import bot
@ -11,7 +11,7 @@ from init import bot
@bot.on_message( @bot.on_message(
filters.incoming filters.incoming
& filters.private & filters.text
& filters.user(bili_auth_user) & filters.user(bili_auth_user)
& filters.command(["download"]) & filters.command(["download"])
) )
@ -31,3 +31,18 @@ async def bili_download_resolve(_: Client, message: Message):
video = create_video(video_number) video = create_video(video_number)
m = await message.reply("开始获取视频数据", quote=True) m = await message.reply("开始获取视频数据", quote=True)
bot.loop.create_task(go_download(video, p_num, m)) bot.loop.create_task(go_download(video, p_num, m))
@bot.on_callback_query(filters.regex(r"^download_(.*)$"))
async def bili_download_resolve_cb(_: Client, callback_query: CallbackQuery):
if not callback_query.from_user:
await callback_query.answer("请私聊机器人")
return
if callback_query.from_user.id not in bili_auth_user:
await callback_query.answer("你没有权限使用此功能")
return
video_number = callback_query.matches[0].group(1)
video = create_video(video_number)
m = await callback_query.message.reply("开始获取视频数据", quote=True)
bot.loop.create_task(go_download(video, 0, m))
await callback_query.answer("开始下载")