@@ -2,6 +2,8 @@ package bot
22
33import (
44 "bytes"
5+ "fmt"
6+
57 "io/ioutil"
68 "math/rand"
79 "os"
@@ -17,7 +19,9 @@ import (
1719
1820 "github.com/Mrs4s/MiraiGo/client"
1921 "github.com/Mrs4s/MiraiGo/message"
22+ "github.com/Mrs4s/MiraiGo/utils"
2023 log "github.com/sirupsen/logrus"
24+ "github.com/tidwall/gjson"
2125)
2226
2327func EmptyText () * message.TextElement {
@@ -74,6 +78,8 @@ func ProtoMsgToMiraiMsg(cli *client.QQClient, msgList []*onebot.Message, notConv
7478 messageChain = append (messageChain , ProtoTtsToMiraiTts (cli , protoMsg .Data ))
7579 case "video" :
7680 messageChain = append (messageChain , ProtoVideoToMiraiVideo (cli , protoMsg .Data ))
81+ case "music" :
82+ messageChain = append (messageChain , ProtoMusicToMiraiMusic (cli , protoMsg .Data ))
7783 default :
7884 log .Errorf ("不支持的消息类型 %+v" , protoMsg )
7985 }
@@ -373,7 +379,7 @@ func ProtoTtsToMiraiTts(cli *client.QQClient, data map[string]string) (m message
373379 return & message.VoiceElement {Data : b }
374380}
375381
376- func ProtoVideoToMiraiVideo (cli * client.QQClient , data map [string ]string ) (m message.IMessageElement ) {
382+ func ProtoVideoToMiraiVideo (_ * client.QQClient , data map [string ]string ) (m message.IMessageElement ) {
377383 elem := & clz.MyVideoElement {}
378384 coverUrl , ok := data ["cover" ]
379385 if ! ok {
@@ -439,3 +445,96 @@ func ProtoVideoToMiraiVideo(cli *client.QQClient, data map[string]string) (m mes
439445 elem .CoverUrl = coverUrl // 仅用于发送日志展示
440446 return elem
441447}
448+
449+ func ProtoMusicToMiraiMusic (_ * client.QQClient , data map [string ]string ) (m message.IMessageElement ) {
450+ if data ["type" ] == "qq" {
451+ info , err := util .QQMusicSongInfo (data ["id" ])
452+ if err != nil {
453+ log .Warnf ("failed to get qq music song info, %+v" , data ["id" ])
454+ return EmptyText ()
455+ }
456+ if ! info .Get ("track_info" ).Exists () {
457+ log .Warnf ("music track_info not found, %+v" , info .String ())
458+ return EmptyText ()
459+ }
460+ name := info .Get ("track_info.name" ).Str
461+ mid := info .Get ("track_info.mid" ).Str
462+ albumMid := info .Get ("track_info.album.mid" ).Str
463+ 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" )
464+ 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"
465+ purl := gjson .ParseBytes (pinfo ).Get ("url_mid.data.midurlinfo.0.purl" ).Str
466+ preview := "http://y.gtimg.cn/music/photo_new/T002R180x180M000" + albumMid + ".jpg"
467+ content := info .Get ("track_info.singer.0.name" ).Str
468+ if data ["content" ] != "" {
469+ content = data ["content" ]
470+ }
471+ return & message.MusicShareElement {
472+ MusicType : message .QQMusic ,
473+ Title : name ,
474+ Summary : content ,
475+ Url : jumpURL ,
476+ PictureUrl : preview ,
477+ MusicUrl : purl ,
478+ }
479+ }
480+ if data ["type" ] == "163" {
481+ info , err := util .NeteaseMusicSongInfo (data ["id" ])
482+ if err != nil {
483+ log .Warnf ("failed to get qq music song info, %+v" , data ["id" ])
484+ return EmptyText ()
485+ }
486+ if ! info .Exists () {
487+ log .Warnf ("netease song not fount" )
488+ return EmptyText ()
489+ }
490+ name := info .Get ("name" ).Str
491+ jumpURL := "https://y.music.163.com/m/song/" + data ["id" ]
492+ musicURL := "http://music.163.com/song/media/outer/url?id=" + data ["id" ]
493+ picURL := info .Get ("album.picUrl" ).Str
494+ artistName := ""
495+ if info .Get ("artists.0" ).Exists () {
496+ artistName = info .Get ("artists.0.name" ).Str
497+ }
498+ return & message.MusicShareElement {
499+ MusicType : message .CloudMusic ,
500+ Title : name ,
501+ Summary : artistName ,
502+ Url : jumpURL ,
503+ PictureUrl : picURL ,
504+ MusicUrl : musicURL ,
505+ }
506+ }
507+ if data ["type" ] == "custom" {
508+ if data ["subtype" ] != "" {
509+ var subType int
510+ switch data ["subtype" ] {
511+ default :
512+ subType = message .QQMusic
513+ case "163" :
514+ subType = message .CloudMusic
515+ case "migu" :
516+ subType = message .MiguMusic
517+ case "kugou" :
518+ subType = message .KugouMusic
519+ case "kuwo" :
520+ subType = message .KuwoMusic
521+ }
522+ return & message.MusicShareElement {
523+ MusicType : subType ,
524+ Title : data ["title" ],
525+ Summary : data ["content" ],
526+ Url : data ["url" ],
527+ PictureUrl : data ["image" ],
528+ MusicUrl : data ["audio" ],
529+ }
530+ }
531+ 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>` ,
532+ utils .XmlEscape (data ["title" ]), data ["url" ], data ["image" ], data ["audio" ], utils .XmlEscape (data ["title" ]), utils .XmlEscape (data ["content" ]))
533+ return & message.ServiceElement {
534+ Id : 60 ,
535+ Content : xml ,
536+ SubType : "music" ,
537+ }
538+ }
539+ return EmptyText ()
540+ }
0 commit comments