点歌功能尝试

This commit is contained in:
liuwei
2025-03-07 14:16:23 +08:00
parent 33a5196ce5
commit ad55bb0701

View File

@@ -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"""
<?xml version="1.0"?>
<msg>
<appmsg appid="wx8dd6ecd81906fd84" sdkver="0">
<title>{song_name}</title>
<des>{first_singer_name}\n❤Bot-祝您天天开心❤</des>
<action>view</action>
<type>3</type>
<showtype>0</showtype>
<content />
<url>{dataurl}</url>
<dataurl>{play_url}</dataurl>
<lowurl/>
<lowdataurl/>
<recorditem />
<thumburl />
<messageaction />
<laninfo />
<extinfo />
<sourceusername />
<sourcedisplayname />
<commenturl />
<appattach>
<totallen>0</totallen>
<attachid />
<emoticonmd5></emoticonmd5>
<fileext />
<aeskey></aeskey>
</appattach>
<webviewshared>
<publisherId />
<publisherReqId>0</publisherReqId>
</webviewshared>
<weappinfo>
<pagepath />
<username />
<appid />
<appservicetype>0</appservicetype>
</weappinfo>
<websearch />
<songalbumurl>{singer_pic}</songalbumurl>
</appmsg>
<scene>0</scene>
<appinfo>
<version>49</version>
<appname>网易云音乐</appname>
</appinfo>
<commenturl />
</msg>"""
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"""<appmsg appid="wx79f2c4418704b4f8" sdkver="0">
<title>{title}</title>
<des>{singer}</des>
<action>view</action>
<type>3</type>
<showtype>0</showtype>
<content/>
<url>{url}</url>
<dataurl>{music_url}</dataurl>
<lowurl>{url}</lowurl>
<lowdataurl>{music_url}</lowdataurl>
<recorditem/>
<thumburl>{cover_url}</thumburl>
<messageaction/>
<laninfo/>
<extinfo/>
<sourceusername/>
<sourcedisplayname/>
<songlyric>{lyric}</songlyric>
<commenturl/>
<appattach><totallen>0</totallen><attachid/><emoticonmd5/><fileext/><aeskey/></appattach>
<webviewshared><publisherId/><publisherReqId>0</publisherReqId></webviewshared>
<weappinfo><pagepath/><username/><appid/><appservicetype>0</appservicetype></weappinfo>
<websearch/>
<songalbumurl>{cover_url}</songalbumurl>
</appmsg>
<fromusername>{message.sender}</fromusername>
<scene>0</scene>
<appinfo><version>1</version><appname/></appinfo>
<commenturl/>"""
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}")