1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-05 19:43:49 +08:00

feat(music): add netease(163) music for [CQ:music]

This commit is contained in:
F0ur 2020-08-25 15:17:20 +08:00
parent e319f2645e
commit 174bb0bbe1
2 changed files with 44 additions and 12 deletions

View File

@ -6,11 +6,6 @@ import (
"encoding/hex"
"errors"
"fmt"
"github.com/Mrs4s/MiraiGo/binary"
"github.com/Mrs4s/MiraiGo/message"
"github.com/Mrs4s/go-cqhttp/global"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
"io/ioutil"
"net/url"
"path"
@ -18,6 +13,12 @@ import (
"runtime"
"strconv"
"strings"
"github.com/Mrs4s/MiraiGo/binary"
"github.com/Mrs4s/MiraiGo/message"
"github.com/Mrs4s/go-cqhttp/global"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
)
var matchReg = regexp.MustCompile(`\[CQ:\w+?.*?]`)
@ -453,6 +454,27 @@ func (bot *CQBot) ToElement(t string, d map[string]string, group bool) (message.
SubType: "music",
}, nil
}
if d["type"] == "163" {
info, err := global.NeteaseMusicSongInfo(d["id"])
if err != nil {
return nil, err
}
if !info.Exists() {
return nil, errors.New("song not found")
}
name := info.Get("name").Str
artistName := ""
if info.Get("artists.0").Exists() {
artistName = info.Get("artists.0.name").Str
}
xml := fmt.Sprintf(`<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="2" templateID="1" action="web" brief="[分享] %s" sourceMsgId="0" url="http://music.163.com/m/song/%s" flag="0" adverSign="0" multiMsgFlag="0"><item layout="2"><audio cover="%s?param=90y90" src="https://music.163.com/song/media/outer/url?id=%s.mp3" /><title>%s</title><summary>%s</summary></item><source name="网易云音乐" icon="https://pic.rmb.bdstatic.com/911423bee2bef937975b29b265d737b3.png" url="http://web.p.qq.com/qqmpmobile/aio/app.html?id=1101079856" action="app" a_actionData="com.netease.cloudmusic" i_actionData="tencent100495085://" appid="100495085" /></msg>`,
name, d["id"], info.Get("album.picUrl").Str, d["id"], name, artistName)
return &message.ServiceElement{
Id: 60,
Content: xml,
SubType: "music",
}, nil
}
if d["type"] == "custom" {
xml := fmt.Sprintf(`<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="2" templateID="1" action="web" brief="[分享] %s" sourceMsgId="0" url="%s" flag="0" adverSign="0" multiMsgFlag="0"><item layout="2"><audio cover="%s" src="%s"/><title>%s</title><summary>%s</summary></item><source name="音乐" icon="https://i.gtimg.cn/open/app_icon/01/07/98/56/1101079856_100_m.png" url="http://web.p.qq.com/qqmpmobile/aio/app.html?id=1101079856" action="app" a_actionData="com.tencent.qqmusic" i_actionData="tencent1101079856://" appid="1101079856" /></msg>`,
d["title"], d["url"], d["image"], d["audio"], d["title"], d["content"])

View File

@ -3,12 +3,14 @@ package global
import (
"bytes"
"compress/gzip"
"github.com/Mrs4s/MiraiGo/message"
"github.com/tidwall/gjson"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"github.com/Mrs4s/MiraiGo/message"
"github.com/tidwall/gjson"
)
func GetBytes(url string) ([]byte, error) {
@ -44,12 +46,20 @@ func QQMusicSongInfo(id string) (gjson.Result, error) {
return gjson.ParseBytes(d).Get("songinfo.data"), nil
}
func NewXmlMsg(template string,ResId int64) *message.ServiceElement{
func NeteaseMusicSongInfo(id string) (gjson.Result, error) {
d, err := GetBytes(fmt.Sprintf("http://music.163.com/api/song/detail/?id=%s&ids=%%5B%s%%5D", id, id))
if err != nil {
return gjson.Result{}, err
}
return gjson.ParseBytes(d).Get("songs.0"), nil
}
func NewXmlMsg(template string, ResId int64) *message.ServiceElement {
var serviceid string
if ResId == 0{
serviceid ="2" //默认值2
}else{
serviceid = strconv.FormatInt(ResId,10)
if ResId == 0 {
serviceid = "2" //默认值2
} else {
serviceid = strconv.FormatInt(ResId, 10)
}
//println(serviceid)
return &message.ServiceElement{