-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathplaylist.go
More file actions
153 lines (135 loc) · 4.15 KB
/
Copy pathplaylist.go
File metadata and controls
153 lines (135 loc) · 4.15 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package jellyfin
import (
"encoding/json"
"fmt"
"io"
"strings"
)
type createPlaylistBody struct {
Name string `json:"Name"`
IsPublic bool `json:"IsPublic,omitempty"`
Ids []string `json:"Ids,omitempty"`
UserID string `json:"UserId"`
MediaType string `json:"MediaType"`
}
type createPlaylistResponse struct {
ID string `json:"Id"`
}
func (c *Client) CreatePlaylist(name, description string, public bool, trackIDs []string) error {
body := createPlaylistBody{
Name: name,
IsPublic: public,
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)
}
defer resp.Close()
// Jellyfin does not accept a description (Overview) in the CreatePlaylist call,
// so we need to add the description in a second request
if description != "" {
respBytes, err := io.ReadAll(resp)
if err != nil {
return err
}
var cpResp createPlaylistResponse
if err := json.Unmarshal(respBytes, &cpResp); err != nil {
return err
}
return c.UpdatePlaylistMetadata(cpResp.ID, name, description, public)
}
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"`
IsPublic bool `json:"IsPublic"`
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, public bool) error {
pl, err := c.GetPlaylist(playlistID)
if err != nil {
return err
}
params := c.defaultParams()
body := updatePlaylistBody{
Name: name,
Overview: overview,
IsPublic: public,
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
}