点歌功能再次优化

This commit is contained in:
liuwei
2026-01-26 14:43:51 +08:00
parent 512b94150d
commit 025bfdfe62
3 changed files with 38 additions and 55 deletions

View File

@@ -10,7 +10,7 @@ command-format = """
[GuessSong.api]
# 新的聚合搜索API一次调用即可获取所有信息包括播放链接
music_api = "https://music-dl.sayqz.com/api/?type=aggregateSearch&keyword={singer_name}&limit=30"
music_api = "http://192.168.2.170:5000"

View File

@@ -143,7 +143,7 @@ class GuessSongPlugin(MessagePluginInterface):
# 加载API配置
api_config = guess_song_config.get("api", {})
self.music_api = api_config.get("music_api", "")
self.music_api = api_config.get("music_api", "http://192.168.2.170:5000")
# 加载游戏配置
game_config = guess_song_config.get("game", {})
@@ -258,7 +258,7 @@ class GuessSongPlugin(MessagePluginInterface):
"start_time": time.time(),
"hint_given": False,
"play_url": song_info.get("play_url"),
"singer_pic": song_info.get('singer_pic', '') or song_info.get('singer_pic', ''),
"singer_pic": song_info.get('pic_url', '') or song_info.get('pic_url', ''),
"data_url": song_info.get('data_url', '')
}
@@ -359,65 +359,48 @@ class GuessSongPlugin(MessagePluginInterface):
async def _get_random_song(self, singer_name: Optional[str]) -> Dict[str, Any]:
"""获取随机歌曲信息"""
try:
# 构建API请求URL - 使用新的聚合搜索API
if singer_name:
# 如果指定了歌手,搜索该歌手的歌曲
api_url = f"{self.music_api}".format(singer_name=singer_name)
else:
# 如果没有指定歌手,使用热门歌手列表
random_singer = random.choice(self.popular_singers)
api_url = f"{self.music_api}".format(singer_name=random_singer)
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.rstrip("/")
keyword = singer_name if singer_name else random.choice(self.popular_singers)
search_url = f"{base_url}/Search"
resp = requests.get(search_url, params={"keywords": keyword}, timeout=10)
if resp.status_code != 200:
self.LOG.error(f"API 请求失败,状态码: {resp.status_code}")
return {}
# 解析响应数据
json_data = response.json()
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 {}
# 从歌曲列表中随机选择一首
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"未找到歌曲列表或列表为空")
return {}
# 随机选择一首歌曲
random_song = random.choice(song_list)
# 从API响应中提取所需字段
song_name = random_song.get('name', '')
singer_name = random_song.get('artist', '')
play_url = random_song.get('url', '')
singer_pic = random_song.get('pic', '')
data_url = random_song.get('url', '') # 使用相同的URL作为数据链接
if not play_url:
self.LOG.error(f"歌曲播放链接为空")
random_song = random.choice(songs)
song_id = str(random_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", random_song.get("name", ""))
singer_name_res = song_info.get("ar_name", random_song.get("artist_string", random_song.get("artists", "")))
play_url = song_info.get("url", "")
pic_url = song_info.get("pic", random_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"成功获取歌曲: {singer_name} - {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": song_name,
"singer_name": 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
}
@@ -494,7 +477,7 @@ class GuessSongPlugin(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,

View File

@@ -7,4 +7,4 @@ command-format = """
"""
# 新的聚合搜索API一次调用即可获取所有信息包括播放链接
music_api_url = "https://music-dl.sayqz.com/api/?type=aggregateSearch&keyword={song_name}&limit=30"
music_api_url = "http://192.168.2.170:5000"