ltc/cloudlyric.go

60 lines
1.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package ltc
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
type CloudLyricBase struct {
Version int `json:"version"`
Lyric string `json:"lyric"`
}
type CloudLyric struct {
Sgc bool `json:"sgc"`
Sfy bool `json:"sfy"`
Qfy bool `json:"qfy"`
TransUser interface{} `json:"transUser"`
Lrc CloudLyricBase `json:"lrc"`
TLyric CloudLyricBase `json:"tlyric"`
Code int `json:"code"`
}
func Get163Lyric(id string) (lyric, tLyric string) {
api := "https://music.163.com/api/song/lyric"
params := url.Values{}
params.Add("os", "pc")
//歌曲的id号
params.Add("id", id)
//包含原始歌词
params.Add("lv", "1")
//包含翻译歌词
params.Add("tv", "1")
api = fmt.Sprintf("%s?%s", api, params.Encode())
req, _ := http.NewRequest("GET", api, nil)
//必须设置Referer,否则会请求失败
req.Header.Add("Referer", "https://music.163.com")
req.Header.Add("User-Agent", CHROME_UA)
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()
var cloudLyric CloudLyric
err = json.NewDecoder(resp.Body).Decode(&cloudLyric)
if cloudLyric.Sgc {
fmt.Printf("获取歌词失败,返回的结果为:%+v请检查id是否正确\n", cloudLyric)
panic("id错误获取歌词失败")
}
return cloudLyric.Lrc.Lyric, cloudLyric.TLyric.Lyric
}