mirror of
https://github.com/Xtao-Labs/iShotaBot.git
synced 2024-11-24 09:15:51 +00:00
✨ 支持获取 audio
This commit is contained in:
parent
8f0ab8746f
commit
72421dd150
@ -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:
|
||||
|
@ -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"<b>{info['title']}</b>\n\n{info.get('desc', '')}\n\nhttps://www.bilibili.com/audio/au{a.get_auid()}"
|
||||
if len(text) > 800:
|
||||
text = f"<b>{info['title']}</b>\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"<b>{info['title']}</b>\n\n{info['desc']}\n\nhttps://b23.tv/{v.get_bvid()}"
|
||||
if len(caption) > 800:
|
||||
caption = f"<b>{info['title']}</b>\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:
|
||||
|
@ -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):
|
||||
"""
|
||||
|
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user