forked from kafuumi/ltc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqqlyric.go
68 lines (60 loc) · 1.83 KB
/
qqlyric.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package ltc
import (
"compress/gzip"
"encoding/json"
"fmt"
"net/http"
"net/url"
)
/**
从QQ音乐上获取歌词
*/
// QQLyric qq音乐获取歌词接口返回的数据结构
type QQLyric struct {
RetCode int `json:"retcode"`
Code int `json:"code"`
SubCode int `json:"subcode"`
//Lyric 原文歌词
Lyric string `json:"lyric"`
//Trans 译文歌词
Trans string `json:"trans"`
}
func GetQQLyric(id string) (lyric, tLyric string) {
api := "https://c.y.qq.com/lyric/fcgi-bin/fcg_query_lyric_new.fcg"
params := url.Values{}
//返回格式
params.Add("format", "json")
params.Add("inCharset", "utf-8")
params.Add("outCharset", "utf-8")
params.Add("platform", "yqq.json")
params.Add("g_tk", "5381")
//歌曲的id号
params.Add("songmid", id)
//返回结果为原始结果,而不是base64编码的结果(base64编码后数据量会增大)
params.Add("nobase64", "1")
api = fmt.Sprintf("%s?%s", api, params.Encode())
req, _ := http.NewRequest("GET", api, nil)
//必须设置Referer,否则会请求失败
req.Header.Add("Referer", "https://y.qq.com")
req.Header.Add("User-Agent", CHROME_UA)
req.Header.Add("accept-encoding", "gzip")
resp, err := client.Do(req)
if err != nil {
fmt.Printf("网络错误:%v\n", err)
panic("网络异常,请求失败。")
}
if resp == nil || resp.StatusCode != http.StatusOK {
fmt.Printf("网络请求失败,状态码为:%d\n", resp.StatusCode)
panic("获取失败,未能正确获取到数据")
}
defer resp.Body.Close()
//返回的数据是gzip压缩,需要解压
reader, _ := gzip.NewReader(resp.Body)
var qqLyric QQLyric
err = json.NewDecoder(reader).Decode(&qqLyric)
if qqLyric.RetCode != 0 {
fmt.Printf("获取歌词失败,返回的结果为:%+v,请检查id是否正确\n", qqLyric)
panic("id错误,获取歌词失败。")
}
return qqLyric.Lyric, qqLyric.Trans
}