Skip to content

Commit

Permalink
新增 获取歌曲下载URL、获取歌曲歌词
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoMengXinX committed Nov 6, 2021
1 parent ce35e61 commit 1ccfdb7
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
43 changes: 43 additions & 0 deletions api/songDownloadURL.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package api

import (
"encoding/json"
"github.com/XiaoMengXinX/Music163Api-Go/types"
"github.com/XiaoMengXinX/Music163Api-Go/utils"
)

// SongDownloadURL 歌曲下载 URL API
const SongDownloadURL = "/api/song/enhance/download/url"

// SongDownloadURLReq 歌曲下载 URL 的参数配置
type SongDownloadURLReq struct {
// Br 码率,默认设置了 999000 即最大码率
Id int `json:"id"`
// Ids 歌曲 ID
Br int `json:"br"`
}

// CreateSongDownloadURLJson 创建请求 body json
func CreateSongDownloadURLJson(id int) string {
reqBody := SongDownloadURLReq{
Id: id,
Br: 999000,
}
reqBodyJson, _ := json.Marshal(reqBody)
return string(reqBodyJson)
}

// GetSongDownloadURL 获取歌曲下载 URL, 非 VIP 账号可通过此 API 获取部分歌曲的无损音质
func GetSongDownloadURL(data utils.RequestData, id int) (result types.SongDownloadURLData, err error) {
var options utils.EapiOption
options.Path = SongDownloadURL
options.Url = "https://music.163.com/eapi/song/enhance/download/url"
options.Json = CreateSongDownloadURLJson(id)
resBody, _, err := utils.ApiRequest(options, data)
if err != nil {
return result, err
}
err = json.Unmarshal([]byte(resBody), &result)
result.RawJson = resBody
return result, err
}
46 changes: 46 additions & 0 deletions api/songLyric.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package api

import (
"encoding/json"
"github.com/XiaoMengXinX/Music163Api-Go/types"
"github.com/XiaoMengXinX/Music163Api-Go/utils"
)

// SongLyricAPI 获取歌词 API
const SongLyricAPI = "/api/song/lyric"

// SongLyricReq SongLyric API 的 body json
type SongLyricReq struct {
Id int `json:"id"`
Lv int `json:"lv"`
Kv int `json:"kv"`
Tv int `json:"tv"`
}

// CreateSongLyricReqJson 创建请求 body json
func CreateSongLyricReqJson(id int) string {
reqBody := SongLyricReq{
Id: id,
Lv: -1,
Kv: -1,
Tv: -1,
}
reqBodyJson, _ := json.Marshal(reqBody)
return string(reqBodyJson)
}

// GetSongLyric 获取歌曲歌词
func GetSongLyric(data utils.RequestData, id int) (result types.SongLyricData, err error) {
var options utils.EapiOption
options.Path = SongLyricAPI
options.Url = "https://music.163.com/eapi/song/lyric"
reqBodyJson := CreateSongLyricReqJson(id)
options.Json = reqBodyJson
resBody, _, err := utils.ApiRequest(options, data)
if err != nil {
return result, err
}
err = json.Unmarshal([]byte(resBody), &result)
result.RawJson = resBody
return result, err
}
30 changes: 30 additions & 0 deletions types/songLyric.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package types

// SongLyricData 获取歌词 API 返回数据
type SongLyricData struct {
RawJson string
Sgc bool `json:"sgc"`
Sfy bool `json:"sfy"`
Qfy bool `json:"qfy"`
LyricUser struct {
Id int `json:"id"`
Status int `json:"status"`
Demand int `json:"demand"`
Userid int `json:"userid"`
Nickname string `json:"nickname"`
Uptime int64 `json:"uptime"`
} `json:"lyricUser"`
Lrc struct {
Version int `json:"version"`
Lyric string `json:"lyric"`
} `json:"lrc"`
Klyric struct {
Version int `json:"version"`
Lyric string `json:"lyric"`
} `json:"klyric"`
Tlyric struct {
Version int `json:"version"`
Lyric string `json:"lyric"`
} `json:"tlyric"`
Code int `json:"code"`
}
7 changes: 7 additions & 0 deletions types/songURL.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ type SongsURLData struct {
Code int `json:"code"`
}

// SongDownloadURLData SongDownloadURL API 的返回数据
type SongDownloadURLData struct {
RawJson string
Data SongURLData `json:"data"`
Code int `json:"code"`
}

// SongURLData 获取歌曲 URL API 的返回数据
type SongURLData struct {
Id int `json:"id"`
Expand Down

0 comments on commit 1ccfdb7

Please sign in to comment.