-
Notifications
You must be signed in to change notification settings - Fork 4
/
playlist.go
128 lines (113 loc) · 3.47 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package jellyfin
import (
"fmt"
"strings"
)
type createPlaylistBody struct {
Name string `json:"Name"`
Ids []string `json:"Ids,omitempty"`
UserID string `json:"UserId"`
MediaType string `json:"MediaType"`
}
func (c *Client) CreatePlaylist(name string, trackIDs []string) error {
body := createPlaylistBody{
Name: name,
UserID: c.userID,
MediaType: "Audio",
Ids: trackIDs,
}
resp, err := c.post("/Playlists", c.defaultParams(), body)
if err != nil {
return fmt.Errorf("create playlist: %v", err)
}
resp.Close()
return nil
}
func (c *Client) GetPlaylistSongs(playlistID string) ([]*Song, error) {
params := c.defaultParams()
params.setIncludeFields(songIncludeFields...)
resp, err := c.get(fmt.Sprintf("/Playlists/%s/Items", playlistID), params)
if err != nil {
return nil, fmt.Errorf("get playlist songs: %v", err)
}
defer resp.Close()
return c.parseSongs(resp)
}
type updatePlaylistBody struct {
Name string `json:"Name"`
Overview string `json:"Overview"`
DateCreated string `json:"DateCreated"`
Genres []string `json:"Genres"`
PremiereDate string `json:"PremiereDate"`
ProviderIds map[string]string `json:"ProviderIds"`
Tags []string `json:"Tags"`
}
func (c *Client) UpdatePlaylistMetadata(playlistID, name, overview string) error {
pl, err := c.GetPlaylist(playlistID)
if err != nil {
return err
}
params := c.defaultParams()
body := updatePlaylistBody{
Name: name,
Overview: overview,
DateCreated: pl.DateCreated, // Required
Genres: pl.Genres, // Required
PremiereDate: pl.PremiereDate, // Required
Tags: pl.Tags, // Required
ProviderIds: pl.ProviderIds, // Required
}
resp, err := c.post(fmt.Sprintf("/Items/%s", playlistID), params, body)
if err != nil {
return fmt.Errorf("update playlist metadata: %v", err)
}
resp.Close()
return nil
}
func (c *Client) AddSongsToPlaylist(playlistID string, trackIDs []string) error {
params := c.defaultParams()
params["ids"] = strings.Join(trackIDs, ",")
resp, err := c.post(fmt.Sprintf("/Playlists/%s/Items", playlistID), params, struct{}{})
if err != nil {
return fmt.Errorf("add songs to playlist: %v", err)
}
resp.Close()
return nil
}
func (c *Client) RemoveSongsFromPlaylist(playlistID string, removeIndexes []int) error {
songs, err := c.GetPlaylistSongs(playlistID)
if err != nil {
return err
}
removeItemIds := make([]string, 0, len(removeIndexes))
for _, idx := range removeIndexes {
if idx < len(songs) {
removeItemIds = append(removeItemIds, songs[idx].PlaylistItemId)
}
}
params := c.defaultParams()
params["entryIds"] = strings.Join(removeItemIds, ",")
resp, err := c.delete(fmt.Sprintf("/Playlists/%s/Items", playlistID), params)
if err != nil {
return fmt.Errorf("remove songs from playlist: %v", err)
}
resp.Close()
return nil
}
func (c *Client) MovePlaylistSong(playlistID string, trackID string, newIdx int) error {
endpoint := fmt.Sprintf("/Playlists/%s/Items/%s/Move/%d", playlistID, trackID, newIdx)
resp, err := c.post(endpoint, c.defaultParams(), struct{}{})
if err != nil {
return fmt.Errorf("move playlist song: %v", err)
}
resp.Close()
return nil
}
func (c *Client) DeletePlaylist(playlistID string) error {
resp, err := c.delete(fmt.Sprintf("/Items/%s", playlistID), c.defaultParams())
if err != nil {
return fmt.Errorf("delete playlist: %v", err)
}
defer resp.Close()
return nil
}