|
1 | | -// Package music QQ音乐、网易云、酷狗、酷我、咪咕 点歌 |
| 1 | +// Package music 整合多平台音乐点播能力 |
2 | 2 | package music |
3 | 3 |
|
4 | 4 | import ( |
5 | | - "crypto/md5" |
6 | | - "encoding/hex" |
7 | 5 | "fmt" |
8 | | - "io" |
9 | | - "net/http" |
10 | | - "net/url" |
11 | | - "strings" |
12 | | - "time" |
13 | | - |
14 | | - "github.com/FloatTech/floatbox/web" |
15 | 6 |
|
16 | 7 | ctrl "github.com/FloatTech/zbpctrl" |
17 | 8 | "github.com/FloatTech/zbputils/control" |
18 | 9 | "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" |
20 | 16 | zero "github.com/wdvxdr1123/ZeroBot" |
21 | 17 | "github.com/wdvxdr1123/ZeroBot/message" |
22 | 18 | ) |
23 | 19 |
|
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 | +} |
27 | 28 |
|
28 | 29 | func init() { |
29 | 30 | control.AutoRegister(&ctrl.Options[*zero.Ctx]{ |
30 | 31 | DisableOnDefault: false, |
31 | 32 | Brief: "点歌", |
32 | | - Help: "- 点歌[xxx]\n" + |
| 33 | + Help: "- 点歌[xxx] (默认酷我)\n" + |
33 | 34 | "- 网易点歌[xxx]\n" + |
34 | 35 | "- 酷我点歌[xxx]\n" + |
35 | 36 | "- 酷狗点歌[xxx]\n" + |
36 | 37 | "- 咪咕点歌[xxx]\n" + |
37 | 38 | "- qq点歌[xxx]\n", |
38 | 39 | }).OnRegex(`^(.{0,2})点歌\s?(.{1,25})$`).SetBlock(true).Limit(ctxext.LimitByUser). |
39 | 40 | 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 |
54 | 55 | } |
| 56 | + ctx.SendChain(seg) |
55 | 57 | }) |
56 | 58 | } |
57 | 59 |
|
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, "咪咕音乐搜索失败") |
69 | 64 | } |
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) |
76 | 67 | } |
| 68 | + song := songs[0] |
77 | 69 |
|
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 | + } |
80 | 77 |
|
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 | | - // 返回音乐卡片 |
100 | 78 | 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 |
105 | 83 | } |
106 | 84 |
|
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 | + |
134 | 103 | 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 |
139 | 108 | } |
140 | 109 |
|
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 | + |
188 | 128 | 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 |
193 | 133 | } |
194 | 134 |
|
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) |
199 | 137 | if err != nil { |
200 | | - msg = message.Text("ERROR: ", err) |
201 | | - return |
| 138 | + return message.Segment{}, errors.Wrap(err, "网易云音乐搜索失败") |
202 | 139 | } |
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] |
206 | 144 |
|
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) |
211 | 146 | 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("获取网易云播放链接失败:链接为空") |
214 | 151 | } |
215 | | - msg = message.Music("qq", gjson.ParseBytes(data).Get("data.song.itemlist.0.id").Int()) |
216 | | - return |
217 | | -} |
218 | 152 |
|
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 |
225 | 158 | } |
226 | 159 |
|
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) |
233 | 171 | 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音乐播放链接失败:链接为空") |
235 | 176 | } |
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 |
239 | 183 | } |
0 commit comments