From 8ae2c1c9be36a90768215b55622a037e195a5e91 Mon Sep 17 00:00:00 2001
From: Mrs4s <1844812067@qq.com>
Date: Sun, 23 Aug 2020 03:16:48 +0800
Subject: [PATCH] music supported.
---
coolq/cqcode.go | 32 ++++++++++++++++++++++++++++++++
global/net.go | 9 +++++++++
go.mod | 2 +-
go.sum | 2 ++
4 files changed, 44 insertions(+), 1 deletion(-)
diff --git a/coolq/cqcode.go b/coolq/cqcode.go
index db02c3a5..5e8f6ae8 100644
--- a/coolq/cqcode.go
+++ b/coolq/cqcode.go
@@ -425,6 +425,38 @@ func (bot *CQBot) ToElement(t string, d map[string]string, group bool) (message.
return message.NewAt(t), nil
case "share":
return message.NewUrlShare(d["url"], d["title"], d["content"], d["image"]), nil
+ case "music":
+ if d["type"] == "qq" {
+ info, err := global.QQMusicSongInfo(d["id"])
+ if err != nil {
+ return nil, err
+ }
+ if !info.Get("track_info").Exists() {
+ return nil, errors.New("song not found")
+ }
+ aid := strconv.FormatInt(info.Get("track_info.album.id").Int(), 10)
+ name := info.Get("track_info.name").Str
+ if len(aid) < 2 {
+ return nil, errors.New("song error")
+ }
+ xml := fmt.Sprintf(`- %s%s
`,
+ name, d["id"], aid[:len(aid)-2], aid, name, "", info.Get("track_info.singer.name").Str)
+ return &message.ServiceElement{
+ Id: 60,
+ Content: xml,
+ SubType: "music",
+ }, nil
+ }
+ if d["type"] == "custom" {
+ xml := fmt.Sprintf(`- %s%s
`,
+ d["title"], d["url"], d["image"], d["audio"], d["title"], d["content"])
+ return &message.ServiceElement{
+ Id: 60,
+ Content: xml,
+ SubType: "music",
+ }, nil
+ }
+ return nil, errors.New("unsupported music type: " + d["type"])
default:
return nil, errors.New("unsupported cq code: " + t)
}
diff --git a/global/net.go b/global/net.go
index e250ef61..40aeab82 100644
--- a/global/net.go
+++ b/global/net.go
@@ -3,6 +3,7 @@ package global
import (
"bytes"
"compress/gzip"
+ "github.com/tidwall/gjson"
"io/ioutil"
"net/http"
"strings"
@@ -32,3 +33,11 @@ func GetBytes(url string) ([]byte, error) {
}
return body, nil
}
+
+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¬ice=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
+}
diff --git a/go.mod b/go.mod
index 6c2503ac..dde6bdfb 100644
--- a/go.mod
+++ b/go.mod
@@ -3,7 +3,7 @@ module github.com/Mrs4s/go-cqhttp
go 1.14
require (
- github.com/Mrs4s/MiraiGo v0.0.0-20200822102558-4dc018de2be7
+ github.com/Mrs4s/MiraiGo v0.0.0-20200822190748-ce670caee0a8
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect
github.com/gin-gonic/gin v1.6.3
github.com/gorilla/websocket v1.4.2
diff --git a/go.sum b/go.sum
index 8fa51998..1ab07764 100644
--- a/go.sum
+++ b/go.sum
@@ -2,6 +2,8 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Mrs4s/MiraiGo v0.0.0-20200822102558-4dc018de2be7 h1:X6ebQxHSXRXDa3ZkMSFYnUhEIKdn6iPUmcFf2pl46oA=
github.com/Mrs4s/MiraiGo v0.0.0-20200822102558-4dc018de2be7/go.mod h1:0je03wji/tSw4bUH4QCF2Z4/EjyNWjSJTyy5tliX6EM=
+github.com/Mrs4s/MiraiGo v0.0.0-20200822190748-ce670caee0a8 h1:29b2Y1TehWby+jEXC7kjOvXk/PxwZ1BeFAKwOzTaW7M=
+github.com/Mrs4s/MiraiGo v0.0.0-20200822190748-ce670caee0a8/go.mod h1:0je03wji/tSw4bUH4QCF2Z4/EjyNWjSJTyy5tliX6EM=
github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0=
github.com/bwmarrin/snowflake v0.3.0/go.mod h1:NdZxfVWX+oR6y2K0o6qAYv6gIOP9rjG0/E9WsDpxqwE=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=