From 09092f6a2ada8853b4122113d16bb8f33c9cd32e Mon Sep 17 00:00:00 2001 From: tkgs0 Date: Sat, 19 Oct 2024 10:26:58 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E4=B8=8D=E5=BF=85?= =?UTF-8?q?=E8=A6=81=E7=9A=84=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_plugin_voicemusic/__init__.py | 90 +++++---------------------- pyproject.toml | 4 +- 2 files changed, 16 insertions(+), 78 deletions(-) diff --git a/nonebot_plugin_voicemusic/__init__.py b/nonebot_plugin_voicemusic/__init__.py index c063cc3..1f00536 100644 --- a/nonebot_plugin_voicemusic/__init__.py +++ b/nonebot_plugin_voicemusic/__init__.py @@ -1,21 +1,12 @@ -import re -import shutil import httpx -from pathlib import Path -from pydub import AudioSegment -from graiax import silkcoder -from nonebot import require from nonebot import on_command from nonebot import get_plugin_config -from .config import Config from nonebot.params import CommandArg from nonebot.plugin import PluginMetadata from nonebot.log import logger -from nonebot.adapters.onebot.v11 import Bot, Message, MessageSegment, Event +from nonebot.adapters.onebot.v11 import Message, MessageSegment from .config import Config -require("nonebot_plugin_localstore") -import nonebot_plugin_localstore as store # 插件元数据 __plugin_meta__ = PluginMetadata( @@ -27,11 +18,6 @@ supported_adapters={"~onebot.v11"}, ) -# 获取插件的缓存和数据目录 -plugin_cache_dir: Path = store.get_plugin_cache_dir() -temp_folder = plugin_cache_dir / "temp" -temp_folder.mkdir(exist_ok=True) # 创建temp目录 - # 加载插件配置 plugin_config = get_plugin_config(Config) uin = plugin_config.uin @@ -44,92 +30,46 @@ music_handler = on_command("点歌", aliases={"点一首歌"}, priority=5) @music_handler.handle() -async def handle_music_request(bot: Bot, event: Event, args: Message = CommandArg()): +async def handle_music_request(args: Message = CommandArg()): """处理用户的点歌请求""" music_name = args.extract_plain_text().strip() # 获取指令参数 if not music_name: - await bot.send(event, "请提供歌曲名称,例如:点歌 告白气球") + await music_handler.send("请提供歌曲名称,例如:点歌 告白气球") return - await bot.send(event, f"收到点歌请稍等...") + await music_handler.send(f"收到点歌请稍等...") # 获取音乐源 URL src = await get_music_src(music_name) if src: - # 设置临时文件路径 - mp3_path = temp_folder / "temp.mp3" - wav_path = temp_folder / "temp.wav" - flac_path = temp_folder / "temp.flac" - - # 根据音频类型选择保存路径 - if re.search("flac", src): - save_path = flac_path - elif re.search("mp3", src): - save_path = mp3_path - else: - save_path = wav_path - # 下载音乐并转换为 SILK 格式 - if await download_audio(src, save_path): - silk_file = convert_to_silk(save_path) - if silk_file: - await bot.send(event, MessageSegment.record(silk_file)) - else: - await bot.send(event, "音频转换失败,请稍后再试。") + if content := (await download_audio(src)): + await music_handler.finish(MessageSegment.record(content)) else: - await bot.send(event, "音频下载失败,请稍后再试。") + await music_handler.finish("音频下载失败,请稍后再试。") else: - await bot.send(event, "未能找到该音乐,请检查名称是否正确。") + await music_handler.finish("未能找到该音乐,请检查名称是否正确。") # 音频下载函数 -async def download_audio(audio_url: str, save_path: str) -> bool: +async def download_audio(audio_url: str) -> bytes | None: async with httpx.AsyncClient() as client: try: response = await client.get(audio_url) if response.status_code == 200: - with open(save_path, "wb") as file: - file.write(response.content) - logger.info(f"音频文件已成功保存为 '{save_path}'") - return True + return response.content else: logger.error(f"下载音频文件失败,状态码: {response.status_code}") - return False + return None except Exception as e: - logger.error(f"下载音频文件发生异常: {str(e)}") - return False - -# 音频转换函数 -def convert_to_silk(save_path: Path) -> str: - silk_path = temp_folder / "temp.silk" - wav_path = str(save_path) # Convert Path to string - - if str(save_path).endswith(".mp3"): - logger.info(f"正在将 MP3 文件 {save_path} 转换为 WAV") - wav_path = temp_folder / "temp.wav" - audio = AudioSegment.from_mp3(str(save_path)) - audio.export(str(wav_path), format="wav") - logger.info(f"MP3 文件已成功转换为 WAV 文件 {wav_path}") - - elif str(save_path).endswith(".flac"): - logger.info(f"正在将 FLAC 文件 {save_path} 转换为 WAV") - wav_path = temp_folder / "temp.wav" - audio = AudioSegment.from_file(str(save_path), format="flac") - audio.export(str(wav_path), format="wav") - logger.info(f"FLAC 文件已成功转换为 WAV 文件 {wav_path}") - - try: - silkcoder.encode(str(wav_path), str(silk_path)) - logger.info(f"已将 WAV 文件 {wav_path} 转换为 SILK 文件 {silk_path}") - return str(silk_path) - except Exception as e: - logger.error(f"SILK 文件转换失败: {str(e)}") - return None + logger.error(f"下载音频文件发生异常: {repr(e)}") + return None + # 获取音乐直链函数 -async def get_music_src(keyword: str) -> str: +async def get_music_src(keyword: str) -> str | None: """根据关键词获取音乐直链""" url = "https://api.xingzhige.com/API/QQmusicVIP/" params = { diff --git a/pyproject.toml b/pyproject.toml index 36710c5..6554f6a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "nonebot-plugin-voicemusic" -version = "0.1.8" +version = "0.1.9" description = "nonebot2语音点歌插件" readme = "README.md" requires-python = ">=3.9" @@ -20,9 +20,7 @@ dependencies = [ "nonebot-adapter-onebot>=2.1.5", "nonebot_plugin_localstore>=0.7.0", "httpx>=0.27.2", - "pydub>=0.25.1", "pydantic>=1.10", - "graiax-silkcoder>=0.3.6", ] [project.urls] From b1cdcda8f13f18215a6664b72d94de8129877eb4 Mon Sep 17 00:00:00 2001 From: tkgs0 Date: Sat, 19 Oct 2024 10:29:01 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E4=B8=8D=E5=BF=85?= =?UTF-8?q?=E8=A6=81=E7=9A=84=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pyproject.toml | 2 -- 1 file changed, 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 6554f6a..6ef4e99 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,9 +18,7 @@ classifiers = [ dependencies = [ "nonebot2>=2.2.0", "nonebot-adapter-onebot>=2.1.5", - "nonebot_plugin_localstore>=0.7.0", "httpx>=0.27.2", - "pydantic>=1.10", ] [project.urls]