https://netease-cloud-music-api-iota-mocha.vercel.app/search?keywords=海阔天空
)点击Binaryify/NeteaseCloudMusicApi: 网易云音乐 Node.js API service (github.com),将它fork到你自己的仓库。
然后打开vercel
官网https://vercel.com/,然后点击login。
https://netease-cloud-music-wtc4i4rp7-mzy0727.vercel.app/search?keywords=庐州月
keywords
后面的歌曲名,获得不同的json
。{"result":{"songs":[{"id":167850,"name":"庐州月","artists":[{"id":5771,"name":"许嵩","picUrl":null,"alias":[],"albumSize":0,"picId":0,"img1v1Url":"https://p1.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg","img1v1":0,"trans":null}],"album":{"id":16951,"name":"寻雾启示","artist":{"id":0,"name":"","picUrl":null,"alias":[],"albumSize":0,"picId":0,"img1v1Url":"https://p1.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg","img1v1":0,"trans":null},"publishTime":1262707200007,"size":9,"copyrightId":14026,"status":1,"picId":34084860473122,"mark":0},"duration":255000,"copyrightId":14026,"status":0,"alias":[],"rtype":0,"ftype":0,"mvid":0,"fee":8,"rUrl":null,"mark":8192},{"id":1969777581,"name":"庐州月","artists":[{"id":53559442,"name":"暴躁X先生","picUrl":null,"alias":[],"albumSize":0,"picId":0,"img1v1Url":"https://p1.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg","img1v1":0,"trans":null}],"album":{"id":149120781,"name":"抬头看,天上就是光","artist":{"id":0,"name":"","picUrl":null,"alias":
...
提取歌曲id
id = result['result']['songs'][0]['id']
song_id = await get_song_id(song_name)
url = f'https://api.vvhan.com/api/music?id={song_id}&type=song&media=netease'
res = requests.get(url)
result = json.loads(res.text)
mp3= result['mp3url'] #音乐链接
import nonebot
from nonebot import on_keyword # 事件响应器函数
from nonebot.typing import T_State #bot使用的对象和字典
from nonebot.adapters.onebot.v11 import Bot,Message,MessageSegment,Event # #Message是使用cq码的必要函数
from nonebot.rule import to_me
from nonebot.matcher import Matcher
from nonebot.adapters import Message
from nonebot.params import Arg, CommandArg, ArgPlainText
import json,urllib,requests # 处理api返回的json数据
from urllib.parse import urlencode
from urllib.request import urlopen
__plugin_name__ = 'music_v2'
__plugin_usage__ = '用法: 点歌,返回随机一首音乐卡片。'
music_v2 = on_keyword({"点歌"}, rule = to_me())
@music_v2.handle()
async def handle_first_receive(matcher: Matcher, args: Message = CommandArg()):
plain_text = args.extract_plain_text() # 首次发送命令时跟随的参数,例:/点歌 有何不可,则args为有何不可
if plain_text:
music_v2.set_arg(("singer",args)) # 如果用户发送了参数则直接赋值
@music_v2.got("singer", prompt="你想点什么歌呢?")
async def handle_song(singer: Message = Arg(), song_name: str = ArgPlainText("singer")):
song_id = await get_song_id(song_name)
url = f'https://api.vvhan.com/api/music?id={song_id}&type=song&media=netease'
res = requests.get(url)
result = json.loads(res.text)
mp3= result['mp3url']
await music_v2.send(MessageSegment.record(mp3))
# 在这里编写获取歌曲id的函数
async def get_song_id(singer: str) -> str:
params = {
'keywords': '',
}
url = 'https://netease-cloud-music-wtc4i4rp7-mzy0727.vercel.app/search'
params['keywords'] = singer
params = urlencode(params)
url = urlopen('%s?%s' % (url, params))
html= url.read()
result = json.loads(html)
id = result['result']['songs'][0]['id']
return id