Skip to content

Commit e59ab9f

Browse files
authored
🐛修复点歌 (#1277)
1 parent 37dfad9 commit e59ab9f

3 files changed

Lines changed: 134 additions & 187 deletions

File tree

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ require (
3333
github.com/fumiama/unibase2n v0.0.0-20240530074540-ec743fd5a6d6
3434
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
3535
github.com/google/uuid v1.6.0
36+
github.com/guohuiyuan/music-lib v1.0.2-0.20260121020416-53f6cb24629d
3637
github.com/jinzhu/gorm v1.9.16
3738
github.com/jozsefsallai/gophersauce v1.0.1
3839
github.com/kanrichan/resvg-go v0.0.2-0.20231001163256-63db194ca9f5

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
117117
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
118118
github.com/gopxl/beep/v2 v2.1.1 h1:6FYIYMm2qPAdWkjX+7xwKrViS1x0Po5kDMdRkq8NVbU=
119119
github.com/gopxl/beep/v2 v2.1.1/go.mod h1:ZAm9TGQ9lvpoiFLd4zf5B1IuyxZhgRACMId1XJbaW0E=
120+
github.com/guohuiyuan/music-lib v1.0.2-0.20260121020416-53f6cb24629d h1:6Cw52c4JaYvq55yAa9ZgUQeBL6b3ZWErQqkbeMZiAYw=
121+
github.com/guohuiyuan/music-lib v1.0.2-0.20260121020416-53f6cb24629d/go.mod h1:D/6kQDwhQFDNZEMjN8y760DQSVYpOGlQXrYzhKz0rCQ=
120122
github.com/jfreymuth/oggvorbis v1.0.5 h1:u+Ck+R0eLSRhgq8WTmffYnrVtSztJcYrl588DM4e3kQ=
121123
github.com/jfreymuth/oggvorbis v1.0.5/go.mod h1:1U4pqWmghcoVsCJJ4fRBKv9peUJMBHixthRlBeD6uII=
122124
github.com/jfreymuth/vorbis v1.0.2 h1:m1xH6+ZI4thH927pgKD8JOH4eaGRm18rEE9/0WKjvNE=

plugin/music/selecter.go

Lines changed: 131 additions & 187 deletions
Original file line numberDiff line numberDiff line change
@@ -1,239 +1,183 @@
1-
// Package music QQ音乐、网易云、酷狗、酷我、咪咕 点歌
1+
// Package music 整合多平台音乐点播能力
22
package music
33

44
import (
5-
"crypto/md5"
6-
"encoding/hex"
75
"fmt"
8-
"io"
9-
"net/http"
10-
"net/url"
11-
"strings"
12-
"time"
13-
14-
"github.com/FloatTech/floatbox/web"
156

167
ctrl "github.com/FloatTech/zbpctrl"
178
"github.com/FloatTech/zbputils/control"
189
"github.com/FloatTech/zbputils/ctxext"
19-
"github.com/tidwall/gjson"
10+
"github.com/guohuiyuan/music-lib/kugou"
11+
"github.com/guohuiyuan/music-lib/kuwo"
12+
"github.com/guohuiyuan/music-lib/migu"
13+
"github.com/guohuiyuan/music-lib/netease"
14+
"github.com/guohuiyuan/music-lib/qq"
15+
"github.com/pkg/errors"
2016
zero "github.com/wdvxdr1123/ZeroBot"
2117
"github.com/wdvxdr1123/ZeroBot/message"
2218
)
2319

24-
var (
25-
longZhuURL = "https://www.hhlqilongzhu.cn/api/joox/juhe_music.php?msg=%v"
26-
)
20+
var platformMap = map[string]func(string) (message.Segment, error){
21+
"咪咕": getMiguMusic,
22+
"酷我": getKuwoMusic,
23+
"酷狗": getKugouMusic,
24+
"网易": getNeteaseMusic,
25+
"qq": getQQMusic,
26+
"": getKuwoMusic, // 默认点歌指向酷我
27+
}
2728

2829
func init() {
2930
control.AutoRegister(&ctrl.Options[*zero.Ctx]{
3031
DisableOnDefault: false,
3132
Brief: "点歌",
32-
Help: "- 点歌[xxx]\n" +
33+
Help: "- 点歌[xxx] (默认酷我)\n" +
3334
"- 网易点歌[xxx]\n" +
3435
"- 酷我点歌[xxx]\n" +
3536
"- 酷狗点歌[xxx]\n" +
3637
"- 咪咕点歌[xxx]\n" +
3738
"- qq点歌[xxx]\n",
3839
}).OnRegex(`^(.{0,2})点歌\s?(.{1,25})$`).SetBlock(true).Limit(ctxext.LimitByUser).
3940
Handle(func(ctx *zero.Ctx) {
40-
// switch 平台
41-
switch ctx.State["regex_matched"].([]string)[1] {
42-
case "咪咕":
43-
ctx.SendChain(migu(ctx.State["regex_matched"].([]string)[2]))
44-
case "酷我":
45-
ctx.SendChain(kuwo(ctx.State["regex_matched"].([]string)[2]))
46-
case "酷狗":
47-
ctx.SendChain(kugou(ctx.State["regex_matched"].([]string)[2]))
48-
case "网易":
49-
ctx.SendChain(cloud163(ctx.State["regex_matched"].([]string)[2]))
50-
case "qq":
51-
ctx.SendChain(qqmusic(ctx.State["regex_matched"].([]string)[2]))
52-
default: // 默认聚合点歌
53-
ctx.SendChain(longzhu(ctx.State["regex_matched"].([]string)[2]))
41+
matches := ctx.State["regex_matched"].([]string)
42+
platformPrefix := matches[1]
43+
keyword := matches[2]
44+
45+
processFunc, ok := platformMap[platformPrefix]
46+
if !ok {
47+
ctx.SendChain(message.Text("不支持的点播平台:", platformPrefix))
48+
return
49+
}
50+
51+
seg, err := processFunc(keyword)
52+
if err != nil {
53+
ctx.SendChain(message.Text("点歌失败:", err))
54+
return
5455
}
56+
ctx.SendChain(seg)
5557
})
5658
}
5759

58-
// longzhu 聚合平台
59-
func longzhu(keyword string) message.Segment {
60-
data, _ := web.GetData(fmt.Sprintf(longZhuURL, url.QueryEscape(keyword)))
61-
// 假设 data 是包含整个 JSON 数组的字节切片
62-
results := gjson.ParseBytes(data).Array()
63-
for _, result := range results {
64-
if strings.Contains(strings.ToLower(result.Get("title").String()), strings.ToLower(keyword)) {
65-
if musicURL := result.Get("full_track").String(); musicURL != "" {
66-
return message.Record(musicURL)
67-
}
68-
}
60+
func getMiguMusic(keyword string) (message.Segment, error) {
61+
songs, err := migu.Search(keyword)
62+
if err != nil {
63+
return message.Segment{}, errors.Wrap(err, "咪咕音乐搜索失败")
6964
}
70-
71-
results = gjson.GetBytes(data, "#.full_track").Array()
72-
if len(results) > 0 {
73-
if musicURL := results[0].String(); musicURL != "" {
74-
return message.Record(musicURL)
75-
}
65+
if len(songs) == 0 {
66+
return message.Segment{}, errors.New("咪咕音乐未找到相关歌曲:" + keyword)
7667
}
68+
song := songs[0]
7769

78-
return message.Text("点歌失败, 找不到 ", keyword, " 的相关结果")
79-
}
70+
playURL, err := migu.GetDownloadURL(&song)
71+
if err != nil {
72+
return message.Segment{}, errors.Wrap(err, "获取咪咕播放链接失败")
73+
}
74+
if playURL == "" {
75+
return message.Segment{}, errors.New("获取咪咕播放链接失败:链接为空")
76+
}
8077

81-
// migu 返回咪咕音乐卡片
82-
func migu(keyword string) message.Segment {
83-
headers := http.Header{
84-
"Cookie": []string{"audioplayer_exist=1; audioplayer_open=0; migu_cn_cookie_id=3ad476db-f021-4bda-ab91-c485ac3d56a0; Hm_lvt_ec5a5474d9d871cb3d82b846d861979d=1671119573; Hm_lpvt_ec5a5474d9d871cb3d82b846d861979d=1671119573; WT_FPC=id=279ef92eaf314cbb8d01671116477485:lv=1671119583092:ss=1671116477485"},
85-
"csrf": []string{"LWKACV45JSQ"},
86-
"User-Agent": []string{"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"},
87-
"Referer": []string{"http://m.music.migu.cn"},
88-
"proxy": []string{"false"},
89-
}
90-
// 搜索音乐信息 第一首歌
91-
search, _ := url.Parse("http://m.music.migu.cn/migu/remoting/scr_search_tag")
92-
search.RawQuery = url.Values{
93-
"keyword": []string{keyword},
94-
"type": []string{"2"},
95-
"pgc": []string{"1"},
96-
"rows": []string{"10"},
97-
}.Encode()
98-
info := gjson.ParseBytes(netGet(search.String(), headers)).Get("musics.0")
99-
// 返回音乐卡片
10078
return message.CustomMusic(
101-
fmt.Sprintf("https://music.migu.cn/v3/music/song/%s", info.Get("copyrightId").String()),
102-
info.Get("mp3").String(),
103-
info.Get("songName").String(),
104-
).Add("content", info.Get("artist").Str).Add("image", info.Get("cover").Str).Add("subtype", "migu")
79+
fmt.Sprintf("https://music.migu.cn/v3/music/song/%s", song.ID),
80+
playURL,
81+
song.Name,
82+
).Add("content", song.Artist).Add("image", song.Cover).Add("subtype", "migu"), nil
10583
}
10684

107-
// kuwo 返回酷我音乐卡片
108-
func kuwo(keyword string) message.Segment {
109-
headers := http.Header{
110-
"Cookie": []string{"Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1610284708,1610699237; _ga=GA1.2.1289529848.1591618534; kw_token=LWKACV45JSQ; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1610699468; _gid=GA1.2.1868980507.1610699238; _gat=1"},
111-
"csrf": []string{"LWKACV45JSQ"},
112-
"User-Agent": []string{"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0"},
113-
"Referer": []string{"https://www.kuwo.cn/search/list?key="},
114-
}
115-
// 搜索音乐信息 第一首歌
116-
search, _ := url.Parse("https://www.kuwo.cn/api/www/search/searchMusicBykeyWord")
117-
search.RawQuery = url.Values{
118-
"key": []string{keyword},
119-
"pn": []string{"1"},
120-
"rn": []string{"1"},
121-
"httpsStatus": []string{"1"},
122-
}.Encode()
123-
info := gjson.ParseBytes(netGet(search.String(), headers)).Get("data.list.0")
124-
// 获得音乐直链
125-
music, _ := url.Parse("http://www.kuwo.cn/api/v1/www/music/playUrl")
126-
music.RawQuery = url.Values{
127-
"mid": []string{fmt.Sprintf("%d", info.Get("rid").Int())},
128-
"type": []string{"convert_url3"},
129-
"br": []string{"320kmp3"},
130-
"httpsStatus": []string{"1"},
131-
}.Encode()
132-
audio := gjson.ParseBytes(netGet(music.String(), headers))
133-
// 返回音乐卡片
85+
func getKuwoMusic(keyword string) (message.Segment, error) {
86+
songs, err := kuwo.Search(keyword)
87+
if err != nil {
88+
return message.Segment{}, errors.Wrap(err, "酷我音乐搜索失败")
89+
}
90+
if len(songs) == 0 {
91+
return message.Segment{}, errors.New("酷我音乐未找到相关歌曲:" + keyword)
92+
}
93+
song := songs[0]
94+
95+
playURL, err := kuwo.GetDownloadURL(&song)
96+
if err != nil {
97+
return message.Segment{}, errors.Wrap(err, "获取酷我播放链接失败")
98+
}
99+
if playURL == "" {
100+
return message.Segment{}, errors.New("获取酷我播放链接失败:链接为空")
101+
}
102+
134103
return message.CustomMusic(
135-
fmt.Sprintf("https://www.kuwo.cn/play_detail/%d", info.Get("rid").Int()),
136-
audio.Get("data.url").Str,
137-
info.Get("name").Str,
138-
).Add("content", info.Get("artist").Str).Add("image", info.Get("pic").Str).Add("subtype", "kuwo")
104+
fmt.Sprintf("https://www.kuwo.cn/play_detail/%s", song.ID),
105+
playURL,
106+
song.Name,
107+
).Add("content", song.Artist).Add("image", song.Cover).Add("subtype", "kuwo"), nil
139108
}
140109

141-
// kugou 返回酷狗音乐卡片
142-
func kugou(keyword string) message.Segment {
143-
stamp := time.Now().UnixNano() / 1e6
144-
hash := md5str(
145-
fmt.Sprintf(
146-
"NVPh5oo715z5DIWAeQlhMDsWXXQV4hwtbitrate=0callback=callback123clienttime=%dclientver=2000dfid=-inputtype=0iscorrection=1isfuzzy=0keyword=%smid=%dpage=1pagesize=30platform=WebFilterprivilege_filter=0srcappid=2919tag=emuserid=-1uuid=%dNVPh5oo715z5DIWAeQlhMDsWXXQV4hwt",
147-
stamp, keyword, stamp, stamp,
148-
),
149-
)
150-
// 搜索音乐信息 第一首歌
151-
h1 := http.Header{
152-
"User-Agent": []string{"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0"},
153-
}
154-
search, _ := url.Parse("https://complexsearch.kugou.com/v2/search/song")
155-
search.RawQuery = url.Values{
156-
"callback": []string{"callback123"},
157-
"keyword": []string{keyword},
158-
"page": []string{"1"},
159-
"pagesize": []string{"30"},
160-
"bitrate": []string{"0"},
161-
"isfuzzy": []string{"0"},
162-
"tag": []string{"em"},
163-
"inputtype": []string{"0"},
164-
"platform": []string{"WebFilter"},
165-
"userid": []string{"-1"},
166-
"clientver": []string{"2000"},
167-
"iscorrection": []string{"1"},
168-
"privilege_filter": []string{"0"},
169-
"srcappid": []string{"2919"},
170-
"clienttime": []string{fmt.Sprintf("%d", stamp)},
171-
"mid": []string{fmt.Sprintf("%d", stamp)},
172-
"uuid": []string{fmt.Sprintf("%d", stamp)},
173-
"dfid": []string{"-"},
174-
"signature": []string{hash},
175-
}.Encode()
176-
res := netGet(search.String(), h1)
177-
info := gjson.ParseBytes(res[12 : len(res)-2]).Get("data.lists.0")
178-
// 获得音乐直链
179-
h2 := http.Header{
180-
"Cookie": []string{"kg_mid=d8e70a262c93d47599c6196c612d6f4f; Hm_lvt_aedee6983d4cfc62f509129360d6bb3d=1610278505,1611631363,1611722252; kg_dfid=33ZWee1kircl0jcJ1h0WF1fX; Hm_lpvt_aedee6983d4cfc62f509129360d6bb3d=1611727348; kg_dfid_collect=d41d8cd98f00b204e9800998ecf8427e"},
181-
"Host": []string{"wwwapi.kugou.com"},
182-
"TE": []string{"Trailers"},
183-
"User-Agent": []string{"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0"},
184-
}
185-
music := "https://wwwapi.kugou.com/yy/index.php?r=play%2Fgetdata&hash=" + info.Get("FileHash").Str + "&album_id=" + info.Get("AlbumID").Str
186-
audio := gjson.ParseBytes(netGet(music, h2)).Get("data")
187-
// 返回音乐卡片
110+
func getKugouMusic(keyword string) (message.Segment, error) {
111+
songs, err := kugou.Search(keyword)
112+
if err != nil {
113+
return message.Segment{}, errors.Wrap(err, "酷狗音乐搜索失败")
114+
}
115+
if len(songs) == 0 {
116+
return message.Segment{}, errors.New("酷狗音乐未找到相关歌曲:" + keyword)
117+
}
118+
song := songs[0]
119+
120+
playURL, err := kugou.GetDownloadURL(&song)
121+
if err != nil {
122+
return message.Segment{}, errors.Wrap(err, "获取酷狗播放链接失败")
123+
}
124+
if playURL == "" {
125+
return message.Segment{}, errors.New("获取酷狗播放链接失败:链接为空")
126+
}
127+
188128
return message.CustomMusic(
189-
"https://www.kugou.com/song/#hash="+audio.Get("hash").Str+"&album_id="+audio.Get("album_id").Str,
190-
strings.ReplaceAll(audio.Get("play_backup_url").Str, "\\/", "/"),
191-
audio.Get("audio_name").Str,
192-
).Add("content", audio.Get("author_name").Str).Add("image", audio.Get("img").Str).Add("subtype", "kugou")
129+
"https://www.kugou.com/",
130+
playURL,
131+
song.Name,
132+
).Add("content", song.Artist).Add("image", song.Cover).Add("subtype", "kugou"), nil
193133
}
194134

195-
// cloud163 返回网易云音乐卡片
196-
func cloud163(keyword string) (msg message.Segment) {
197-
requestURL := "http://music.163.com/api/search/get/web?type=1&limit=1&s=" + url.QueryEscape(keyword)
198-
data, err := web.GetData(requestURL)
135+
func getNeteaseMusic(keyword string) (message.Segment, error) {
136+
songs, err := netease.Search(keyword)
199137
if err != nil {
200-
msg = message.Text("ERROR: ", err)
201-
return
138+
return message.Segment{}, errors.Wrap(err, "网易云音乐搜索失败")
202139
}
203-
msg = message.Music("163", gjson.ParseBytes(data).Get("result.songs.0.id").Int())
204-
return
205-
}
140+
if len(songs) == 0 {
141+
return message.Segment{}, errors.New("网易云音乐未找到相关歌曲:" + keyword)
142+
}
143+
song := songs[0]
206144

207-
// qqmusic 返回QQ音乐卡片
208-
func qqmusic(keyword string) (msg message.Segment) {
209-
requestURL := "https://c.y.qq.com/splcloud/fcgi-bin/smartbox_new.fcg?platform=yqq.json&key=" + url.QueryEscape(keyword)
210-
data, err := web.RequestDataWith(web.NewDefaultClient(), requestURL, "GET", "", web.RandUA(), nil)
145+
playURL, err := netease.GetDownloadURL(&song)
211146
if err != nil {
212-
msg = message.Text("ERROR: ", err)
213-
return
147+
return message.Segment{}, errors.Wrap(err, "获取网易云播放链接失败")
148+
}
149+
if playURL == "" {
150+
return message.Segment{}, errors.New("获取网易云播放链接失败:链接为空")
214151
}
215-
msg = message.Music("qq", gjson.ParseBytes(data).Get("data.song.itemlist.0.id").Int())
216-
return
217-
}
218152

219-
// md5str 返回字符串 MD5
220-
func md5str(s string) string {
221-
h := md5.New()
222-
h.Write([]byte(s))
223-
result := strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
224-
return result
153+
return message.CustomMusic(
154+
fmt.Sprintf("https://music.163.com/#/song?id=%s", song.ID),
155+
playURL,
156+
song.Name,
157+
).Add("content", song.Artist).Add("image", song.Cover).Add("subtype", "163"), nil
225158
}
226159

227-
// netGet 返回请求数据
228-
func netGet(url string, header http.Header) []byte {
229-
client := &http.Client{}
230-
request, _ := http.NewRequest("GET", url, nil)
231-
request.Header = header
232-
res, err := client.Do(request)
160+
func getQQMusic(keyword string) (message.Segment, error) {
161+
songs, err := qq.Search(keyword)
162+
if err != nil {
163+
return message.Segment{}, errors.Wrap(err, "QQ音乐搜索失败")
164+
}
165+
if len(songs) == 0 {
166+
return message.Segment{}, errors.New("QQ音乐未找到相关歌曲:" + keyword)
167+
}
168+
song := songs[0]
169+
170+
playURL, err := qq.GetDownloadURL(&song)
233171
if err != nil {
234-
return nil
172+
return message.Segment{}, errors.Wrap(err, "获取QQ音乐播放链接失败")
173+
}
174+
if playURL == "" {
175+
return message.Segment{}, errors.New("获取QQ音乐播放链接失败:链接为空")
235176
}
236-
defer res.Body.Close()
237-
result, _ := io.ReadAll(res.Body)
238-
return result
177+
178+
return message.CustomMusic(
179+
fmt.Sprintf("https://y.qq.com/n/ryqq/songDetail/%s", song.ID),
180+
playURL,
181+
song.Name,
182+
).Add("content", song.Artist).Add("image", song.Cover).Add("subtype", "qq"), nil
239183
}

0 commit comments

Comments
 (0)