Skip to content

Commit

Permalink
music
Browse files Browse the repository at this point in the history
  • Loading branch information
lz1998 committed Dec 24, 2021
1 parent c873c6f commit 4da30e8
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/sirupsen/logrus v1.8.1
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816
github.com/tidwall/gjson v1.11.0
github.com/zserge/lorca v0.1.10
golang.org/x/mobile v0.0.0-20211207041440-4e6c2922fdee
golang.org/x/net v0.0.0-20211123203042-d83791d6bcd9 // indirect
Expand All @@ -38,7 +39,6 @@ require (
github.com/leodido/go-urn v1.2.1 // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/pierrec/lz4/v4 v4.1.11 // indirect
github.com/tidwall/gjson v1.11.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/ugorji/go/codec v1.2.6 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Mrs4s/MiraiGo v0.0.0-20211219072930-afb81a32aa75 h1:4xgd1/lhAKHv/2sAbxbR38hDwt/HfOiWlVOqSJc544E=
github.com/Mrs4s/MiraiGo v0.0.0-20211219072930-afb81a32aa75/go.mod h1:HI05ardsa5HE1Z2xTw69dytmaqqRWv4ajD5Jpnycqqw=
github.com/Mrs4s/MiraiGo v0.0.0-20211221051559-21f8feb419c9 h1:py7g4UrnKWnn7LRYU1RAvKDSo9VVslanNJI5MV+4QXo=
github.com/Mrs4s/MiraiGo v0.0.0-20211221051559-21f8feb419c9/go.mod h1:HI05ardsa5HE1Z2xTw69dytmaqqRWv4ajD5Jpnycqqw=
github.com/RomiChan/protobuf v0.0.0-20211204042931-ff4f35848737 h1:p4o7/eSoP39jwnGZz08N1IpH/mNzg9SdCn7kPM9A9BE=
Expand Down
9 changes: 9 additions & 0 deletions pkg/bot/api_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ func preProcessGroupSendingMessage(cli *client.QQClient, groupCode int64, m *mes
newElements = append(newElements, gm)
continue
}
if i, ok := element.(*message.MusicShareElement); ok {
// TODO 撤回?
_, err := cli.SendGroupMusicShare(groupCode, i)
if err != nil {
log.Errorf("failed to send music, %+v", err)
continue
}
continue
}
newElements = append(newElements, element)
}
m.Elements = newElements
Expand Down
101 changes: 100 additions & 1 deletion pkg/bot/proto2mirai.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package bot

import (
"bytes"
"fmt"

"io/ioutil"
"math/rand"
"os"
Expand All @@ -17,7 +19,9 @@ import (

"github.com/Mrs4s/MiraiGo/client"
"github.com/Mrs4s/MiraiGo/message"
"github.com/Mrs4s/MiraiGo/utils"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
)

func EmptyText() *message.TextElement {
Expand Down Expand Up @@ -74,6 +78,8 @@ func ProtoMsgToMiraiMsg(cli *client.QQClient, msgList []*onebot.Message, notConv
messageChain = append(messageChain, ProtoTtsToMiraiTts(cli, protoMsg.Data))
case "video":
messageChain = append(messageChain, ProtoVideoToMiraiVideo(cli, protoMsg.Data))
case "music":
messageChain = append(messageChain, ProtoMusicToMiraiMusic(cli, protoMsg.Data))
default:
log.Errorf("不支持的消息类型 %+v", protoMsg)
}
Expand Down Expand Up @@ -373,7 +379,7 @@ func ProtoTtsToMiraiTts(cli *client.QQClient, data map[string]string) (m message
return &message.VoiceElement{Data: b}
}

func ProtoVideoToMiraiVideo(cli *client.QQClient, data map[string]string) (m message.IMessageElement) {
func ProtoVideoToMiraiVideo(_ *client.QQClient, data map[string]string) (m message.IMessageElement) {
elem := &clz.MyVideoElement{}
coverUrl, ok := data["cover"]
if !ok {
Expand Down Expand Up @@ -439,3 +445,96 @@ func ProtoVideoToMiraiVideo(cli *client.QQClient, data map[string]string) (m mes
elem.CoverUrl = coverUrl // 仅用于发送日志展示
return elem
}

func ProtoMusicToMiraiMusic(_ *client.QQClient, data map[string]string) (m message.IMessageElement) {
if data["type"] == "qq" {
info, err := util.QQMusicSongInfo(data["id"])
if err != nil {
log.Warnf("failed to get qq music song info, %+v", data["id"])
return EmptyText()
}
if !info.Get("track_info").Exists() {
log.Warnf("music track_info not found, %+v", info.String())
return EmptyText()
}
name := info.Get("track_info.name").Str
mid := info.Get("track_info.mid").Str
albumMid := info.Get("track_info.album.mid").Str
pinfo, _ := util.GetBytes("http://u.y.qq.com/cgi-bin/musicu.fcg?g_tk=2034008533&uin=0&format=json&data={\"comm\":{\"ct\":23,\"cv\":0},\"url_mid\":{\"module\":\"vkey.GetVkeyServer\",\"method\":\"CgiGetVkey\",\"param\":{\"guid\":\"4311206557\",\"songmid\":[\"" + mid + "\"],\"songtype\":[0],\"uin\":\"0\",\"loginflag\":1,\"platform\":\"23\"}}}&_=1599039471576")
jumpURL := "https://i.y.qq.com/v8/playsong.html?platform=11&appshare=android_qq&appversion=10030010&hosteuin=oKnlNenz7i-s7c**&songmid=" + mid + "&type=0&appsongtype=1&_wv=1&source=qq&ADTAG=qfshare"
purl := gjson.ParseBytes(pinfo).Get("url_mid.data.midurlinfo.0.purl").Str
preview := "http://y.gtimg.cn/music/photo_new/T002R180x180M000" + albumMid + ".jpg"
content := info.Get("track_info.singer.0.name").Str
if data["content"] != "" {
content = data["content"]
}
return &message.MusicShareElement{
MusicType: message.QQMusic,
Title: name,
Summary: content,
Url: jumpURL,
PictureUrl: preview,
MusicUrl: purl,
}
}
if data["type"] == "163" {
info, err := util.NeteaseMusicSongInfo(data["id"])
if err != nil {
log.Warnf("failed to get qq music song info, %+v", data["id"])
return EmptyText()
}
if !info.Exists() {
log.Warnf("netease song not fount")
return EmptyText()
}
name := info.Get("name").Str
jumpURL := "https://y.music.163.com/m/song/" + data["id"]
musicURL := "http://music.163.com/song/media/outer/url?id=" + data["id"]
picURL := info.Get("album.picUrl").Str
artistName := ""
if info.Get("artists.0").Exists() {
artistName = info.Get("artists.0.name").Str
}
return &message.MusicShareElement{
MusicType: message.CloudMusic,
Title: name,
Summary: artistName,
Url: jumpURL,
PictureUrl: picURL,
MusicUrl: musicURL,
}
}
if data["type"] == "custom" {
if data["subtype"] != "" {
var subType int
switch data["subtype"] {
default:
subType = message.QQMusic
case "163":
subType = message.CloudMusic
case "migu":
subType = message.MiguMusic
case "kugou":
subType = message.KugouMusic
case "kuwo":
subType = message.KuwoMusic
}
return &message.MusicShareElement{
MusicType: subType,
Title: data["title"],
Summary: data["content"],
Url: data["url"],
PictureUrl: data["image"],
MusicUrl: data["audio"],
}
}
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>`,
utils.XmlEscape(data["title"]), data["url"], data["image"], data["audio"], utils.XmlEscape(data["title"]), utils.XmlEscape(data["content"]))
return &message.ServiceElement{
Id: 60,
Content: xml,
SubType: "music",
}
}
return EmptyText()
}
2 changes: 2 additions & 0 deletions pkg/bot/raw2mirai.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ func RawMsgToMiraiMsg(cli *client.QQClient, str string) []message.IMessageElemen
elemList = append(elemList, ProtoTtsToMiraiTts(cli, attrMap))
case "video":
elemList = append(elemList, ProtoVideoToMiraiVideo(cli, attrMap))
case "music":
elemList = append(elemList, ProtoMusicToMiraiMusic(cli, attrMap))
default:
log.Warnf("不支持的类型 %s", code)
elemList = append(elemList, message.NewText(code))
Expand Down
19 changes: 19 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
)

var (
Expand Down Expand Up @@ -357,3 +358,21 @@ func DownloadFileMultiThreading(url, path string, limit int64, threadCount int,
wg.Wait()
return lastErr
}

// QQMusicSongInfo 通过给定id在QQ音乐上查找曲目信息
func QQMusicSongInfo(id string) (gjson.Result, error) {
d, err := GetBytes(`https://u.y.qq.com/cgi-bin/musicu.fcg?format=json&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq.json&needNewCode=0&data={%22comm%22:{%22ct%22:24,%22cv%22:0},%22songinfo%22:{%22method%22:%22get_song_detail_yqq%22,%22param%22:{%22song_type%22:0,%22song_mid%22:%22%22,%22song_id%22:` + id + `},%22module%22:%22music.pf_song_detail_svr%22}}`)
if err != nil {
return gjson.Result{}, err
}
return gjson.ParseBytes(d).Get("songinfo.data"), nil
}

// NeteaseMusicSongInfo 通过给定id在wdd音乐上查找曲目信息
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
}

0 comments on commit 4da30e8

Please sign in to comment.