更换点歌API
This commit is contained in:
@@ -9,8 +9,10 @@ command-format = """
|
||||
"""
|
||||
|
||||
[GuessSong.api]
|
||||
music_list_api = "https://hhlqilongzhu.cn/api/dg_douyinmusic.php?msg={singer_name}&type=json&num=50"
|
||||
music_single_api = "https://hhlqilongzhu.cn/api/dg_douyinmusic.php?msg={search_query}&type=json&n=1"
|
||||
# 新的聚合搜索API,一次调用即可获取所有信息(包括播放链接)
|
||||
music_api = "https://music-dl.sayqz.com/api/?type=aggregateSearch&keyword={singer_name}&limit=30"
|
||||
|
||||
|
||||
|
||||
[GuessSong.popular_singers]
|
||||
list = [
|
||||
|
||||
@@ -143,8 +143,7 @@ class GuessSongPlugin(MessagePluginInterface):
|
||||
|
||||
# 加载API配置
|
||||
api_config = guess_song_config.get("api", {})
|
||||
self.music_list_api = api_config.get("music_list_api", "")
|
||||
self.music_single_api = api_config.get("music_single_api", "")
|
||||
self.music_api = api_config.get("music_api", "")
|
||||
|
||||
# 加载游戏配置
|
||||
game_config = guess_song_config.get("game", {})
|
||||
@@ -360,14 +359,16 @@ class GuessSongPlugin(MessagePluginInterface):
|
||||
async def _get_random_song(self, singer_name: Optional[str]) -> Dict[str, Any]:
|
||||
"""获取随机歌曲信息"""
|
||||
try:
|
||||
# 构建API请求URL - 使用抖音音乐API
|
||||
# 构建API请求URL - 使用新的聚合搜索API
|
||||
if singer_name:
|
||||
# 如果指定了歌手,搜索该歌手的歌曲
|
||||
api_url = f"{self.music_list_api}".format(singer_name=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_list_api}".format(singer_name=random_singer)
|
||||
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)
|
||||
@@ -378,12 +379,12 @@ class GuessSongPlugin(MessagePluginInterface):
|
||||
|
||||
# 解析响应数据
|
||||
json_data = response.json()
|
||||
if not json_data.get("data") or json_data.get("code") != 200:
|
||||
self.LOG.error(f"API 返回数据格式错误: {json_data}")
|
||||
if json_data.get("code") != 200:
|
||||
self.LOG.error(f"API 返回错误: {json_data.get('message')}")
|
||||
return {}
|
||||
|
||||
# 从歌曲列表中随机选择一首
|
||||
song_list = json_data.get("data")
|
||||
song_list = json_data.get("data", {}).get("results", [])
|
||||
if not song_list or not isinstance(song_list, list) or len(song_list) == 0:
|
||||
self.LOG.error(f"未找到歌曲列表或列表为空")
|
||||
return {}
|
||||
@@ -391,31 +392,26 @@ class GuessSongPlugin(MessagePluginInterface):
|
||||
# 随机选择一首歌曲
|
||||
random_song = random.choice(song_list)
|
||||
|
||||
# 根据随机选择的歌曲信息,再次调用API获取完整信息(包含播放链接)
|
||||
song_name = random_song.get('title', '')
|
||||
singer_name = random_song.get('singer', '')
|
||||
search_query = f"{singer_name}-{song_name}"
|
||||
# 从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作为数据链接
|
||||
|
||||
# 调用原有API获取播放链接
|
||||
detail_api = f"{self.music_single_api}".format(search_query=search_query)
|
||||
detail_response = requests.get(detail_api, verify=False)
|
||||
|
||||
if detail_response.status_code != 200:
|
||||
self.LOG.error(f"获取歌曲详情失败,状态码: {detail_response.status_code}")
|
||||
return {}
|
||||
|
||||
detail_data = detail_response.json().get("data")
|
||||
if not detail_data:
|
||||
self.LOG.error(f"获取歌曲详情数据为空")
|
||||
if not play_url:
|
||||
self.LOG.error(f"歌曲播放链接为空")
|
||||
return {}
|
||||
|
||||
# 返回完整的歌曲信息
|
||||
self.LOG.info(f"成功获取歌曲: {singer_name} - {song_name}")
|
||||
|
||||
return {
|
||||
"song_name": song_name,
|
||||
"singer_name": singer_name,
|
||||
"play_url": detail_data.get('url', ''),
|
||||
"singer_pic": detail_data.get('cover', '') or detail_data.get('cover', ''),
|
||||
"data_url": detail_data.get('link', '')
|
||||
"play_url": play_url,
|
||||
"singer_pic": singer_pic,
|
||||
"data_url": data_url
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
|
||||
@@ -6,4 +6,5 @@ command-format = """
|
||||
点歌 歌曲名
|
||||
"""
|
||||
|
||||
music_api_url ="https://hhlqilongzhu.cn/api/dg_douyinmusic.php?msg={song_name}&type=json&n=1"
|
||||
# 新的聚合搜索API,一次调用即可获取所有信息(包括播放链接)
|
||||
music_api_url = "https://music-dl.sayqz.com/api/?type=aggregateSearch&keyword={song_name}&limit=30"
|
||||
@@ -139,21 +139,51 @@ class MusicPlugin(MessagePluginInterface):
|
||||
def _search_song(self, song_name: str) -> Dict[str, Any]:
|
||||
"""搜索歌曲信息"""
|
||||
try:
|
||||
# 尝试QQ音乐API
|
||||
fallback_api = f"{self.music_api_url}".format(song_name=song_name)
|
||||
response = requests.get(fallback_api,verify=False)
|
||||
# 使用新的聚合搜索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}")
|
||||
return {}
|
||||
|
||||
json_data = response.json().get("data")
|
||||
json_data = response.json()
|
||||
|
||||
# 检查API返回状态
|
||||
if json_data.get("code") != 200:
|
||||
self.LOG.error(f"API 返回错误: {json_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:
|
||||
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"歌曲播放链接为空")
|
||||
return {}
|
||||
|
||||
self.LOG.info(f"成功获取歌曲: {result_singer_name} - {result_song_name}")
|
||||
|
||||
return {
|
||||
"song_name": json_data.get('title', ''),
|
||||
"singer_name": json_data.get('singer', ''),
|
||||
"play_url": json_data.get('url', ''),
|
||||
"singer_pic": json_data.get('cover', ''),
|
||||
"data_url": json_data.get('link', '')
|
||||
"song_name": result_song_name,
|
||||
"singer_name": result_singer_name,
|
||||
"play_url": play_url,
|
||||
"singer_pic": singer_pic,
|
||||
"data_url": data_url
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user