diff --git a/plugins/guess_song/main.py b/plugins/guess_song/main.py index 295cfe2..0a5d946 100644 --- a/plugins/guess_song/main.py +++ b/plugins/guess_song/main.py @@ -403,21 +403,50 @@ class GuessSongPlugin(MessagePluginInterface): self.LOG.error(f"歌曲播放链接为空") 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.debug(f"原始URL: {play_url}") + self.LOG.debug(f"真实URL: {real_play_url}") return { "song_name": song_name, "singer_name": singer_name, - "play_url": play_url, - "singer_pic": singer_pic, - "data_url": data_url + "play_url": real_play_url, + "singer_pic": real_singer_pic, + "data_url": real_data_url } except Exception as e: self.LOG.error(f"获取随机歌曲出错: {e}") 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: """发送歌曲片段""" try: diff --git a/plugins/music/main.py b/plugins/music/main.py index 2bc59c7..86071f1 100644 --- a/plugins/music/main.py +++ b/plugins/music/main.py @@ -176,20 +176,49 @@ class MusicPlugin(MessagePluginInterface): self.LOG.error(f"歌曲播放链接为空") 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.debug(f"原始URL: {play_url}") + self.LOG.debug(f"真实URL: {real_play_url}") return { "song_name": result_song_name, "singer_name": result_singer_name, - "play_url": play_url, - "singer_pic": singer_pic, - "data_url": data_url + "play_url": real_play_url, + "singer_pic": real_singer_pic, + "data_url": real_data_url } except Exception as e: self.LOG.error(f"搜索歌曲出错: {e}") 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 with aiohttp.ClientSession() as session: async with session.get(play_url) as resp: