更换点歌API
This commit is contained in:
@@ -403,21 +403,50 @@ class GuessSongPlugin(MessagePluginInterface):
|
|||||||
self.LOG.error(f"歌曲播放链接为空")
|
self.LOG.error(f"歌曲播放链接为空")
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
# 获取真实的播放链接(跟随重定向)
|
||||||
|
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.info(f"成功获取歌曲: {singer_name} - {song_name}")
|
||||||
|
self.LOG.debug(f"原始URL: {play_url}")
|
||||||
|
self.LOG.debug(f"真实URL: {real_play_url}")
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"song_name": song_name,
|
"song_name": song_name,
|
||||||
"singer_name": singer_name,
|
"singer_name": singer_name,
|
||||||
"play_url": play_url,
|
"play_url": real_play_url,
|
||||||
"singer_pic": singer_pic,
|
"singer_pic": real_singer_pic,
|
||||||
"data_url": data_url
|
"data_url": real_data_url
|
||||||
}
|
}
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.LOG.error(f"获取随机歌曲出错: {e}")
|
self.LOG.error(f"获取随机歌曲出错: {e}")
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
def _get_real_url(self, redirect_url: str) -> str:
|
||||||
|
"""跟随重定向获取真实URL"""
|
||||||
|
try:
|
||||||
|
if not redirect_url:
|
||||||
|
return ""
|
||||||
|
|
||||||
|
# 发送HEAD请求,不跟随重定向,获取真实URL
|
||||||
|
response = requests.head(redirect_url, allow_redirects=True, verify=False, timeout=10)
|
||||||
|
|
||||||
|
# 返回最终的URL(重定向后的真实地址)
|
||||||
|
if response.status_code in [200, 301, 302, 303, 307, 308]:
|
||||||
|
real_url = response.url
|
||||||
|
self.LOG.debug(f"URL重定向: {redirect_url} -> {real_url}")
|
||||||
|
return real_url
|
||||||
|
else:
|
||||||
|
self.LOG.warning(f"获取真实URL失败,状态码: {response.status_code}")
|
||||||
|
return redirect_url
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
self.LOG.error(f"获取真实URL出错: {e}")
|
||||||
|
return redirect_url
|
||||||
|
|
||||||
async def _send_song_clip(self, bot: WechatAPIClient, song_info: Dict[str, Any], session_id: str) -> bool:
|
async def _send_song_clip(self, bot: WechatAPIClient, song_info: Dict[str, Any], session_id: str) -> bool:
|
||||||
"""发送歌曲片段"""
|
"""发送歌曲片段"""
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -176,20 +176,49 @@ class MusicPlugin(MessagePluginInterface):
|
|||||||
self.LOG.error(f"歌曲播放链接为空")
|
self.LOG.error(f"歌曲播放链接为空")
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
# 获取真实的播放链接(跟随重定向)
|
||||||
|
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.info(f"成功获取歌曲: {result_singer_name} - {result_song_name}")
|
||||||
|
self.LOG.debug(f"原始URL: {play_url}")
|
||||||
|
self.LOG.debug(f"真实URL: {real_play_url}")
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"song_name": result_song_name,
|
"song_name": result_song_name,
|
||||||
"singer_name": result_singer_name,
|
"singer_name": result_singer_name,
|
||||||
"play_url": play_url,
|
"play_url": real_play_url,
|
||||||
"singer_pic": singer_pic,
|
"singer_pic": real_singer_pic,
|
||||||
"data_url": data_url
|
"data_url": real_data_url
|
||||||
}
|
}
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.LOG.error(f"搜索歌曲出错: {e}")
|
self.LOG.error(f"搜索歌曲出错: {e}")
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
def _get_real_url(self, redirect_url: str) -> str:
|
||||||
|
"""跟随重定向获取真实URL"""
|
||||||
|
try:
|
||||||
|
if not redirect_url:
|
||||||
|
return ""
|
||||||
|
|
||||||
|
# 发送HEAD请求,跟随重定向,获取真实URL
|
||||||
|
response = requests.head(redirect_url, allow_redirects=True, verify=False, timeout=10)
|
||||||
|
|
||||||
|
# 返回最终的URL(重定向后的真实地址)
|
||||||
|
if response.status_code in [200, 301, 302, 303, 307, 308]:
|
||||||
|
real_url = response.url
|
||||||
|
self.LOG.debug(f"URL重定向: {redirect_url} -> {real_url}")
|
||||||
|
return real_url
|
||||||
|
else:
|
||||||
|
self.LOG.warning(f"获取真实URL失败,状态码: {response.status_code}")
|
||||||
|
return redirect_url
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
self.LOG.error(f"获取真实URL出错: {e}")
|
||||||
|
return redirect_url
|
||||||
|
|
||||||
async def url_to_base64(self, play_url: str):
|
async def url_to_base64(self, play_url: str):
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with session.get(play_url) as resp:
|
async with session.get(play_url) as resp:
|
||||||
|
|||||||
Reference in New Issue
Block a user