Skip to content

Commit 3182c8d

Browse files
Merge pull request #42 from 474r4x14/develop
Random songs & star song toggle
2 parents a852009 + 8902cbd commit 3182c8d

File tree

4 files changed

+215
-27
lines changed

4 files changed

+215
-27
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,5 @@ host = 'https://your-subsonic-host.tld'
6969
* n - Continue search forward
7070
* N - Continue search backwards
7171
* r - refresh the list (if in artist directory, only refreshes that artist)
72+
* s - add 50 random songs to the queue
73+
* y - toggle star on song

api.go

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ type SubsonicDirectory struct {
7474
Entities SubsonicEntities `json:"child"`
7575
}
7676

77+
type SubsonicSongs struct {
78+
Song SubsonicEntities `json:"song"`
79+
}
80+
81+
type SubsonicStarred struct {
82+
Starred SubsonicEntities `json:"starred"`
83+
}
84+
7785
type SubsonicEntity struct {
7886
Id string `json:"id"`
7987
IsDirectory bool `json:"isDir"`
@@ -130,13 +138,15 @@ type SubsonicPlaylist struct {
130138
}
131139

132140
type SubsonicResponse struct {
133-
Status string `json:"status"`
134-
Version string `json:"version"`
135-
Indexes SubsonicIndexes `json:"indexes"`
136-
Directory SubsonicDirectory `json:"directory"`
137-
Playlists SubsonicPlaylists `json:"playlists"`
138-
Playlist SubsonicPlaylist `json:"playlist"`
139-
Error SubsonicError `json:"error"`
141+
Status string `json:"status"`
142+
Version string `json:"version"`
143+
Indexes SubsonicIndexes `json:"indexes"`
144+
Directory SubsonicDirectory `json:"directory"`
145+
RandomSongs SubsonicSongs `json:"randomSongs"`
146+
Starred SubsonicSongs `json:"starred"`
147+
Playlists SubsonicPlaylists `json:"playlists"`
148+
Playlist SubsonicPlaylist `json:"playlist"`
149+
Error SubsonicError `json:"error"`
140150
}
141151

142152
type responseWrapper struct {
@@ -192,6 +202,52 @@ func (connection *SubsonicConnection) GetMusicDirectory(id string) (*SubsonicRes
192202
return resp, nil
193203
}
194204

205+
func (connection *SubsonicConnection) GetRandomSongs() (*SubsonicResponse, error) {
206+
query := defaultQuery(connection)
207+
// Let's get 50 random songs, default is 10
208+
query.Set("size", "50")
209+
requestUrl := connection.Host + "/rest/getRandomSongs" + "?" + query.Encode()
210+
resp, err := connection.getResponse("GetRandomSongs", requestUrl)
211+
if err != nil {
212+
return resp, err
213+
}
214+
return resp, nil
215+
}
216+
217+
func (connection *SubsonicConnection) GetStarred() (*SubsonicResponse, error) {
218+
query := defaultQuery(connection)
219+
requestUrl := connection.Host + "/rest/getStarred" + "?" + query.Encode()
220+
resp, err := connection.getResponse("GetStarred", requestUrl)
221+
if err != nil {
222+
return resp, err
223+
}
224+
return resp, nil
225+
}
226+
227+
func (connection *SubsonicConnection) ToggleStar(id string, starredItems map[string]struct{}) (*SubsonicResponse, error) {
228+
query := defaultQuery(connection)
229+
query.Set("id",id)
230+
231+
_, ok := starredItems[id]
232+
var action = "star"
233+
// If the key exists, we're unstarring
234+
if ok {
235+
action = "unstar"
236+
}
237+
238+
requestUrl := connection.Host + "/rest/" + action + "?" + query.Encode()
239+
resp, err := connection.getResponse("ToggleStar", requestUrl)
240+
if err != nil {
241+
if (ok) {
242+
delete(starredItems, id)
243+
} else {
244+
starredItems[id] = struct{}{}
245+
}
246+
return resp, err
247+
}
248+
return resp, nil
249+
}
250+
195251
func (connection *SubsonicConnection) GetPlaylists() (*SubsonicResponse, error) {
196252
query := defaultQuery(connection)
197253
requestUrl := connection.Host + "/rest/getPlaylists" + "?" + query.Encode()

0 commit comments

Comments
 (0)