Nonebot QQ机器人插件八:点歌(网易云音乐)

QQ机器人插件八:点歌(网易云音乐)

1. 搭建自己的网易云api接口或者使用现成的api(https://netease-cloud-music-api-iota-mocha.vercel.app/search?keywords=海阔天空)

  • 点击Binaryify/NeteaseCloudMusicApi: 网易云音乐 Node.js API service (github.com),将它fork到你自己的仓库。

    Nonebot QQ机器人插件八:点歌(网易云音乐)_第1张图片

  • 然后打开vercel官网https://vercel.com/,然后点击login。

Nonebot QQ机器人插件八:点歌(网易云音乐)_第2张图片

  • 使用刚才的github账号登陆。

    Nonebot QQ机器人插件八:点歌(网易云音乐)_第3张图片

  • 然后新建项目

    Nonebot QQ机器人插件八:点歌(网易云音乐)_第4张图片

  • 将刚才fork的项目导入

    Nonebot QQ机器人插件八:点歌(网易云音乐)_第5张图片
    Nonebot QQ机器人插件八:点歌(网易云音乐)_第6张图片
    Nonebot QQ机器人插件八:点歌(网易云音乐)_第7张图片

  • 最后得到自定义的api地址

Nonebot QQ机器人插件八:点歌(网易云音乐)_第8张图片

2. 通过刚才搭建的api获取歌曲id

Nonebot QQ机器人插件八:点歌(网易云音乐)_第9张图片

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']
    

3. 通过歌曲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']	#音乐链接

4. 完整代码

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


5. 效果展示

Nonebot QQ机器人插件八:点歌(网易云音乐)_第10张图片

你可能感兴趣的:(qq机器人,json,前端,javascript)