点歌功能再次优化

This commit is contained in:
liuwei
2026-01-26 14:38:13 +08:00
parent ec7d93aa4a
commit 512b94150d
2 changed files with 178 additions and 44 deletions

View File

@@ -70,7 +70,7 @@ class MusicPlugin(MessagePluginInterface):
self._commands = self._config.get("Music", {}).get("command", ["点歌", "音乐"])
self.command_format = self._config.get("Music", {}).get("command-format", "点歌 歌曲名")
self.enable = self._config.get("Music", {}).get("enable", True)
self.music_api_url = self._config.get("Music", {}).get("music_api_url", "")
self.music_api_url = self._config.get("Music", {}).get("music_api_url", "http://192.168.2.170:5000")
self.LOG.debug(f"[{self.name}] 插件初始化完成,指令:{self._commands}")
return True
@@ -139,57 +139,48 @@ class MusicPlugin(MessagePluginInterface):
def _search_song(self, song_name: str) -> Dict[str, Any]:
"""搜索歌曲信息"""
try:
# 使用新的聚合搜索API
api_url = f"{self.music_api_url}".format(song_name=song_name)
self.LOG.info(f"请求歌曲API: {api_url}")
response = requests.get(api_url, verify=False)
if response.status_code != 200:
self.LOG.error(f"API 请求失败,状态码: {response.status_code}")
base_url = self.music_api_url.rstrip("/")
search_url = f"{base_url}/Search"
self.LOG.info(f"请求歌曲API: {search_url}")
resp = requests.get(search_url, params={"keywords": song_name}, timeout=10)
if resp.status_code != 200:
self.LOG.error(f"API 请求失败,状态码: {resp.status_code}")
return {}
json_data = response.json()
# 检查API返回状态
if json_data.get("code") != 200:
self.LOG.error(f"API 返回错误: {json_data.get('message')}")
search_data = resp.json()
if not search_data.get("success") or search_data.get("status") != 200:
self.LOG.error(f"API 返回错误: {search_data.get('message')}")
return {}
# 从results中获取第一首歌
song_list = json_data.get("data", {}).get("results", [])
if not song_list or not isinstance(song_list, list) or len(song_list) == 0:
songs = search_data.get("data", [])
if not songs:
self.LOG.error(f"未找到歌曲: {song_name}")
return {}
# 获取第一首搜索结果
first_song = song_list[0]
# 从API响应中提取所需字段
result_song_name = first_song.get('name', '')
result_singer_name = first_song.get('artist', '')
play_url = first_song.get('url', '')
singer_pic = first_song.get('pic', '')
data_url = first_song.get('url', '') # 使用相同的URL作为数据链接
if not play_url:
self.LOG.error(f"歌曲播放链接为空")
first_song = songs[0]
song_id = str(first_song.get("id", ""))
detail_url = f"{base_url}/Song_V1"
detail_params = {"url": song_id, "level": "standard", "type": "json"}
detail_resp = requests.get(detail_url, params=detail_params, timeout=15)
if detail_resp.status_code != 200:
self.LOG.error(f"详情请求失败,状态码: {detail_resp.status_code}")
return {}
# 获取真实的播放链接(跟随重定向)
detail_data = detail_resp.json()
if not detail_data.get("success") or detail_data.get("status") != 200:
self.LOG.error(f"获取歌曲详情失败: {detail_data.get('message')}")
return {}
song_info = detail_data.get("data", {})
song_name_res = song_info.get("name", first_song.get("name", ""))
singer_name_res = song_info.get("ar_name", first_song.get("artist_string", first_song.get("artists", "")))
play_url = song_info.get("url", "")
pic_url = song_info.get("pic", first_song.get("picUrl", ""))
data_url = song_info.get("url", "")
real_play_url = self._get_real_url(play_url)
real_data_url = self._get_real_url(data_url)
real_singer_pic = self._get_real_url(singer_pic)
self.LOG.info(f"成功获取歌曲: {result_singer_name} - {result_song_name}")
self.LOG.debug(f"原始URL: {play_url}")
self.LOG.debug(f"真实URL: {real_play_url}")
real_pic_url = self._get_real_url(pic_url)
self.LOG.info(f"成功获取歌曲: {singer_name_res} - {song_name_res}")
return {
"song_name": result_song_name,
"singer_name": result_singer_name,
"song_name": song_name_res,
"singer_name": singer_name_res,
"play_url": real_play_url,
"singer_pic": real_singer_pic,
"pic_url": real_pic_url,
"data_url": real_data_url
}
@@ -233,7 +224,7 @@ class MusicPlugin(MessagePluginInterface):
song_name = song_info.get("song_name", "")
singer_name = song_info.get("singer_name", "")
play_url = song_info.get("play_url", "")
singer_pic = song_info.get("singer_pic", "")
singer_pic = song_info.get("pic_url", "")
data_url = song_info.get("data_url", "")
xml_message = f"{MUSIC_XML}".format(song_name=song_name, singer_name=singer_name, play_url=play_url,
data_url=data_url, singer_pic=singer_pic)