From 8f0ab8746fa344e877ed139888542ed6fb00e81d Mon Sep 17 00:00:00 2001 From: xtaodada Date: Fri, 18 Aug 2023 17:26:57 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=94=AF=E6=8C=81=E7=BE=A4?= =?UTF-8?q?=E7=BB=84=E5=86=85=E6=98=BE=E7=A4=BA=E4=B8=8B=E8=BD=BD=E6=8C=89?= =?UTF-8?q?=E9=92=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- defs/bilibili_download.py | 14 +++++++++++--- defs/button.py | 2 +- modules/bilibili.py | 8 +++++--- modules/bilibili_download.py | 21 ++++++++++++++++++--- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/defs/bilibili_download.py b/defs/bilibili_download.py index 12b7c77..ebc82be 100644 --- a/defs/bilibili_download.py +++ b/defs/bilibili_download.py @@ -9,6 +9,7 @@ import aiofiles from bilibili_api import HEADERS from bilibili_api.video import Video, VideoDownloadURLDataDetecter, VideoQuality from httpx import AsyncClient, Response +from pyrogram.enums import ParseMode from pyrogram.types import Message 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) -async def take_screenshot(v: Video) -> Optional[BytesIO]: +async def take_screenshot(info: Dict) -> Optional[BytesIO]: """获取视频封面""" try: - info = await v.get_info() pic_get = (await request.get(info["pic"])).content pic = BytesIO(pic_get) pic.name = "screenshot.jpg" @@ -291,11 +291,19 @@ async def go_upload(v: Video, p_num: int, m: Message): try: video_duration = await get_video_duration(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"{info['title']}\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}") await bot.send_video( chat_id=m.chat.id, video=str(video_path), + caption=caption, + parse_mode=ParseMode.HTML, duration=int(video_duration), width=video_width, height=video_height, diff --git a/defs/button.py b/defs/button.py index 9e75d4d..4927eb2 100644 --- a/defs/button.py +++ b/defs/button.py @@ -25,4 +25,4 @@ def gen_button(data: List) -> InlineKeyboardMarkup: buttons_callback.append( InlineKeyboardButton(text=button.name, callback_data=button.data) ) - return InlineKeyboardMarkup(inline_keyboard=[buttons_callback, buttons_url]) + return InlineKeyboardMarkup(inline_keyboard=[buttons_url, buttons_callback]) diff --git a/modules/bilibili.py b/modules/bilibili.py index ff3cb7d..3a2cbc1 100644 --- a/modules/bilibili.py +++ b/modules/bilibili.py @@ -12,6 +12,7 @@ from defs.bilibili import ( check_and_refresh_credential, ) from defs.button import gen_button, Button +from defs.glover import bili_auth_user from init import bot 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 if 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( image, quote=True, - reply_markup=gen_button( - [Button(0, "Link", "https://b23.tv/" + video_info["bvid"])] - ), + reply_markup=gen_button(buttons), ) raise ContinuePropagation diff --git a/modules/bilibili_download.py b/modules/bilibili_download.py index b2af403..520e004 100644 --- a/modules/bilibili_download.py +++ b/modules/bilibili_download.py @@ -1,9 +1,9 @@ import re 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.glover import bili_auth_user from init import bot @@ -11,7 +11,7 @@ from init import bot @bot.on_message( filters.incoming - & filters.private + & filters.text & filters.user(bili_auth_user) & filters.command(["download"]) ) @@ -31,3 +31,18 @@ async def bili_download_resolve(_: Client, message: Message): video = create_video(video_number) m = await message.reply("开始获取视频数据", quote=True) 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("开始下载")