forked from winterssy/mxget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
playlist.go
60 lines (51 loc) · 1.28 KB
/
playlist.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
package baidu
import (
"context"
"errors"
"fmt"
"strings"
"github.com/luoqiz/mxget/pkg/api"
"github.com/winterssy/ghttp"
)
func (a *API) GetPlaylist(ctx context.Context, playlistId string) (*api.Collection, error) {
resp, err := a.GetPlaylistRaw(ctx, playlistId)
if err != nil {
return nil, err
}
n := len(resp.Result.SongList)
if n == 0 {
return nil, errors.New("get playlist: no data")
}
a.patchSongsURL(ctx, resp.Result.SongList...)
a.patchSongsLyric(ctx, resp.Result.SongList...)
songs := translate(resp.Result.SongList...)
return &api.Collection{
Id: resp.Result.Info.ListId,
Name: strings.TrimSpace(resp.Result.Info.ListTitle),
PicURL: resp.Result.Info.ListPic,
Songs: songs,
}, nil
}
// 获取歌单
func (a *API) GetPlaylistRaw(ctx context.Context, playlistId string) (*PlaylistResponse, error) {
params := ghttp.Params{
"list_id": playlistId,
"withcount": "1",
"withsong": "1",
}
resp := new(PlaylistResponse)
req, _ := ghttp.NewRequest(ghttp.MethodGet, apiGetPlaylist)
req.SetQuery(params)
req.SetContext(ctx)
r, err := a.SendRequest(req)
if err == nil {
err = r.JSON(resp)
}
if err != nil {
return nil, err
}
if resp.ErrorCode != 22000 {
return nil, fmt.Errorf("get playlist: %s", resp.errorMessage())
}
return resp, nil
}