From 72421dd150460329b1c59464955c4ecc7c6f6567 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Fri, 18 Aug 2023 18:18:12 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=94=AF=E6=8C=81=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=20audio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- defs/bilibili.py | 8 ++++++++ defs/bilibili_download.py | 39 ++++++++++++++++++++++++++++++++++++ modules/bilibili.py | 2 +- modules/bilibili_download.py | 17 ++++++++++++++-- 4 files changed, 63 insertions(+), 3 deletions(-) diff --git a/defs/bilibili.py b/defs/bilibili.py index 25ca5e1..89bc945 100644 --- a/defs/bilibili.py +++ b/defs/bilibili.py @@ -6,6 +6,7 @@ import qrcode import string from bilibili_api import Credential +from bilibili_api.audio import Audio from bilibili_api.video import Video from bilibili_api.user import User from pyrogram import ContinuePropagation @@ -142,6 +143,13 @@ def create_video(cid) -> Optional[Video]: return v +def create_audio(aid: str) -> Optional[Audio]: + a = None + if aid[:2] == "au": + a = Audio(auid=int(aid[2:]), credential=credential) + return a + + async def video_info_get(cid): v = create_video(cid) if not v: diff --git a/defs/bilibili_download.py b/defs/bilibili_download.py index ebc82be..c068d8c 100644 --- a/defs/bilibili_download.py +++ b/defs/bilibili_download.py @@ -7,6 +7,7 @@ from typing import Tuple, Dict, Union, Optional import aiofiles from bilibili_api import HEADERS +from bilibili_api.audio import Audio from bilibili_api.video import Video, VideoDownloadURLDataDetecter, VideoQuality from httpx import AsyncClient, Response from pyrogram.enums import ParseMode @@ -216,6 +217,41 @@ async def take_screenshot(info: Dict) -> Optional[BytesIO]: return None +async def audio_download(a: Audio, m: Message): + try: + info = await a.get_info() + download_url_data = await a.get_download_url() + async with AsyncClient(headers=HEADERS, timeout=60) as client: + r = await client.get(download_url_data["cdns"][0]) + media = BytesIO(r.content) + media.name = None + media.seek(0) + if info.get("cover"): + r_ = await client.get(info.get("cover")) + thumb = BytesIO(r_.content) + thumb.seek(0) + else: + thumb = None + text = f"{info['title']}\n\n{info.get('desc', '')}\n\nhttps://www.bilibili.com/audio/au{a.get_auid()}" + if len(text) > 800: + text = f"{info['title']}\n\n简介过长,无法显示\n\nhttps://www.bilibili.com/audio/au{a.get_auid()}" + await bot.send_audio( + chat_id=m.chat.id, + audio=media, + caption=text, + parse_mode=ParseMode.HTML, + reply_to_message_id=m.reply_to_message_id, + thumb=thumb, + duration=info.get("duration"), + performer=info.get("author"), + ) + except BilibiliDownloaderError as e: + await fail_edit(m, e.MSG) + except Exception as e: + logger.exception("Downloading audio failed") + await fail_edit(m, f"下载/上传失败:{e}") + + async def go_download(v: Video, p_num: int, m: Message): video_path = cache_dir / f"{v.get_aid()}_{p_num}.mp4" safe_remove(video_path) @@ -295,6 +331,8 @@ async def go_upload(v: Video, p_num: int, m: Message): 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()}" + if len(caption) > 800: + caption = f"{info['title']}\n\n简介过长,无法显示\n\nhttps://b23.tv/{v.get_bvid()}" except Exception: video_jpg = None caption = f"https://b23.tv/{v.get_bvid()}" @@ -311,6 +349,7 @@ async def go_upload(v: Video, p_num: int, m: Message): supports_streaming=True, progress=go_upload_progress, progress_args=(m,), + reply_to_message_id=m.reply_to_message_id, ) logger.info(f"Upload {video_path} success") except BilibiliDownloaderError as e: diff --git a/modules/bilibili.py b/modules/bilibili.py index 3a2cbc1..423642c 100644 --- a/modules/bilibili.py +++ b/modules/bilibili.py @@ -21,7 +21,7 @@ from scheduler import scheduler filters.incoming & filters.text & filters.regex(r"av(\d{1,12})|BV(1[A-Za-z0-9]{2}4.1.7[A-Za-z0-9]{2})|b23.tv") - & ~filters.command(["download"]) + & ~(filters.command(["download"]) & filters.user(bili_auth_user)) ) async def bili_resolve(_: Client, message: Message): """ diff --git a/modules/bilibili_download.py b/modules/bilibili_download.py index 520e004..6a2dc8c 100644 --- a/modules/bilibili_download.py +++ b/modules/bilibili_download.py @@ -3,8 +3,8 @@ import re from pyrogram import filters, Client, ContinuePropagation from pyrogram.types import Message, CallbackQuery -from defs.bilibili import b23_extract, create_video -from defs.bilibili_download import go_download +from defs.bilibili import b23_extract, create_video, create_audio +from defs.bilibili_download import go_download, audio_download from defs.glover import bili_auth_user from init import bot @@ -33,6 +33,19 @@ async def bili_download_resolve(_: Client, message: Message): bot.loop.create_task(go_download(video, p_num, m)) +@bot.on_message(filters.incoming & filters.text & filters.regex(r"audio/au(\d{1,12})")) +async def bili_audio_download_resolve(_: Client, message: Message): + p = re.compile(r"au(\d{1,12})") + audio_number = p.search(message.text) + if audio_number: + audio_number = audio_number[0] + else: + raise ContinuePropagation + audio = create_audio(audio_number) + m = await message.reply("开始获取音频数据", quote=True) + bot.loop.create_task(audio_download(audio, 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: