Skip to content

Commit

Permalink
Merge pull request #2 from tkgs0/master
Browse files Browse the repository at this point in the history
PR: 移除不必要的依赖
  • Loading branch information
Onimaimai authored Oct 19, 2024
2 parents 72ebd0f + b1cdcda commit b5ca6bf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 80 deletions.
90 changes: 15 additions & 75 deletions nonebot_plugin_voicemusic/__init__.py
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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
Expand All @@ -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 = {
Expand Down
6 changes: 1 addition & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -18,11 +18,7 @@ classifiers = [
dependencies = [
"nonebot2>=2.2.0",
"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]
Expand Down

0 comments on commit b5ca6bf

Please sign in to comment.