点歌功能尝试
This commit is contained in:
@@ -1,11 +1,11 @@
|
|||||||
import logging
|
import logging
|
||||||
import tomllib
|
import tomllib
|
||||||
|
|
||||||
import aiohttp
|
|
||||||
import requests
|
import requests
|
||||||
from wcferry import WxMsg, Wcf
|
from wcferry import WxMsg, Wcf
|
||||||
|
|
||||||
from robot_cmd.robot_command import Feature, PermissionStatus, GroupBotManager
|
from robot_cmd.robot_command import Feature, PermissionStatus, GroupBotManager
|
||||||
|
import lz4.block as lb
|
||||||
|
|
||||||
|
|
||||||
class BotMusic:
|
class BotMusic:
|
||||||
@@ -44,60 +44,127 @@ class BotMusic:
|
|||||||
if self.gbm.get_group_permission(message.roomid, Feature.MUSIC) == PermissionStatus.DISABLED:
|
if self.gbm.get_group_permission(message.roomid, Feature.MUSIC) == PermissionStatus.DISABLED:
|
||||||
return
|
return
|
||||||
|
|
||||||
song_name = content[len(command[0]):].strip()
|
user_song_name = content[len(command[0]):].strip()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = requests.get(
|
short_play_api = f"https://qqmusic.qqovo.cn/getSearchByKey?key={user_song_name}&page=1&limit=1"
|
||||||
f"https://www.hhlqilongzhu.cn/api/dg_wyymusic.php?gm={song_name}&n=1&br=2&type=json",
|
fallback_api = f"https://www.hhlqilongzhu.cn/api/dg_wyymusic.php?gm={user_song_name}&n=1&num=1&type=json"
|
||||||
timeout=5 # 设置超时,防止阻塞
|
|
||||||
)
|
response = requests.get(short_play_api)
|
||||||
data = response.json()
|
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:
|
except requests.RequestException as e:
|
||||||
self.wcf.send_text(f"-----Bot-----\n❌请求出错:{e}",
|
self.wcf.send_text(f"-----Bot-----\n❌请求出错:{e}",
|
||||||
(message.roomid if message.from_group() else message.sender), message.sender)
|
(message.roomid if message.from_group() else message.sender), message.sender)
|
||||||
return
|
return
|
||||||
|
|
||||||
if data.get("code") != 200:
|
xml_message = f"""
|
||||||
self.wcf.send_text(f"-----Bot-----\n❌点歌失败!\n{data}",
|
<?xml version="1.0"?>
|
||||||
(message.roomid if message.from_group() else message.sender), message.sender)
|
<msg>
|
||||||
return
|
<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"]
|
# 修改消息数据库里面的消息content 内容
|
||||||
singer = data["singer"]
|
text_bytes = xml_message.encode('utf-8')
|
||||||
url = data["link"]
|
compressed_data = lb.compress(text_bytes, store_size=False).hex()
|
||||||
music_url = data["music_url"].split("?")[0]
|
|
||||||
cover_url = data["cover"]
|
|
||||||
lyric = data["lrc"]
|
|
||||||
|
|
||||||
xml = f"""<appmsg appid="wx79f2c4418704b4f8" sdkver="0">
|
data = self.wcf.query_sql('MSG0.db', "SELECT * FROM MSG where type = 49 limit 1")
|
||||||
<title>{title}</title>
|
self.wcf.query_sql('MSG0.db',
|
||||||
<des>{singer}</des>
|
f"""UPDATE MSG SET CompressContent = x'{compressed_data}', BytesExtra=x'', type=49, SubType=3,
|
||||||
<action>view</action>
|
IsSender=0, TalkerId=2 WHERE MsgSvrID={data[0]['MsgSvrID']}"""
|
||||||
<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/>"""
|
|
||||||
|
|
||||||
self.wcf.forward_msg(1251569424535376867, message.roomid)
|
result = self.wcf.forward_msg(data[0]["MsgSvrID"], message.roomid)
|
||||||
|
print(f"点歌发送:{result}")
|
||||||
|
|||||||
Reference in New Issue
Block a user