From ad55bb0701cb58585e5103bd54e2cffbbe91a894 Mon Sep 17 00:00:00 2001 From: liuwei Date: Fri, 7 Mar 2025 14:16:23 +0800 Subject: [PATCH] =?UTF-8?q?=E7=82=B9=E6=AD=8C=E5=8A=9F=E8=83=BD=E5=B0=9D?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- music/bot_music.py | 163 ++++++++++++++++++++++++++++++++------------- 1 file changed, 115 insertions(+), 48 deletions(-) diff --git a/music/bot_music.py b/music/bot_music.py index 3a3ac63..8ba2d26 100644 --- a/music/bot_music.py +++ b/music/bot_music.py @@ -1,11 +1,11 @@ import logging import tomllib -import aiohttp import requests from wcferry import WxMsg, Wcf from robot_cmd.robot_command import Feature, PermissionStatus, GroupBotManager +import lz4.block as lb class BotMusic: @@ -44,60 +44,127 @@ class BotMusic: if self.gbm.get_group_permission(message.roomid, Feature.MUSIC) == PermissionStatus.DISABLED: return - song_name = content[len(command[0]):].strip() + user_song_name = content[len(command[0]):].strip() try: - response = requests.get( - f"https://www.hhlqilongzhu.cn/api/dg_wyymusic.php?gm={song_name}&n=1&br=2&type=json", - timeout=5 # 设置超时,防止阻塞 - ) - data = response.json() + short_play_api = f"https://qqmusic.qqovo.cn/getSearchByKey?key={user_song_name}&page=1&limit=1" + fallback_api = f"https://www.hhlqilongzhu.cn/api/dg_wyymusic.php?gm={user_song_name}&n=1&num=1&type=json" + + response = requests.get(short_play_api) + if response.status_code == 400: + response = requests.get(fallback_api) + + if response.status_code != 200: + print(f"API 请求失败,状态码: {response.status_code}") + return + + json_data = response.json() + result = json_data.get('response', {}).get('data', {}).get('song', {}).get('list', []) + if not result: + print("未找到匹配的歌曲") + return + + first_song = result[0] + song_name = first_song.get('songname', '') + song_mid = first_song.get('songmid', '') + first_singer_name = first_song.get('singer', [{}])[0].get('name', '') + + zhida_singer = json_data.get('response', {}).get('data', {}).get('zhida', {}).get('zhida_singer', {}) + singer_pic = zhida_singer.get('singerPic', '') if zhida_singer else None + + music_play_api = f"https://qqmusic.qqovo.cn/getMusicPlay?songmid={song_mid}&quality=m4a" + music_response = requests.get(music_play_api) + + if music_response.status_code == 400: + print("获取播放链接失败,状态码 400,尝试备用接口") + music_response = requests.get(fallback_api) + + if music_response.status_code != 200: + print(f"获取播放链接失败,状态码: {music_response.status_code}") + return + + music_data = music_response.json() + play_url = music_data.get('data', {}).get('playUrl', {}).get(song_mid, {}).get('url', '') + + if not play_url: + print("主接口play_url为空,尝试备用接口") + music_response = requests.get(fallback_api).json() + song_name = music_response.get('title', song_name) + first_singer_name = music_response.get('singer', first_singer_name) + play_url = music_response.get('music_url', '') + singer_pic = music_response.get('cover', singer_pic) + dataurl = music_response.get('link', '') + else: + dataurl = f"https://y.qq.com/n/ryqq/songDetail/{song_mid}" + + if not play_url: + print("未获取到音乐播放链接") + return + except requests.RequestException as e: self.wcf.send_text(f"-----Bot-----\n❌请求出错:{e}", (message.roomid if message.from_group() else message.sender), message.sender) return - if data.get("code") != 200: - self.wcf.send_text(f"-----Bot-----\n❌点歌失败!\n{data}", - (message.roomid if message.from_group() else message.sender), message.sender) - return + xml_message = f""" + + + + {song_name} + {first_singer_name}\n❤Bot-祝您天天开心❤ + view + 3 + 0 + + {dataurl} + {play_url} + + + + + + + + + + + + 0 + + + + + + + + 0 + + + + + + 0 + + + {singer_pic} + + 0 + + 49 + 网易云音乐 + + + """ - title = data["title"] - singer = data["singer"] - url = data["link"] - music_url = data["music_url"].split("?")[0] - cover_url = data["cover"] - lyric = data["lrc"] + # 修改消息数据库里面的消息content 内容 + text_bytes = xml_message.encode('utf-8') + compressed_data = lb.compress(text_bytes, store_size=False).hex() - xml = f""" - {title} - {singer} - view - 3 - 0 - - {url} - {music_url} - {url} - {music_url} - - {cover_url} - - - - - - {lyric} - - 0 - 0 - 0 - - {cover_url} - - {message.sender} - 0 - 1 - """ + data = self.wcf.query_sql('MSG0.db', "SELECT * FROM MSG where type = 49 limit 1") + self.wcf.query_sql('MSG0.db', + f"""UPDATE MSG SET CompressContent = x'{compressed_data}', BytesExtra=x'', type=49, SubType=3, + IsSender=0, TalkerId=2 WHERE MsgSvrID={data[0]['MsgSvrID']}""" + ) - self.wcf.forward_msg(1251569424535376867, message.roomid) + result = self.wcf.forward_msg(data[0]["MsgSvrID"], message.roomid) + print(f"点歌发送:{result}")