Skip to content

Commit

Permalink
Merge pull request #42 from 474r4x14/develop
Browse files Browse the repository at this point in the history
Random songs & star song toggle
  • Loading branch information
wildeyedskies authored Jul 16, 2023
2 parents a852009 + 8902cbd commit 3182c8d
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 27 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,5 @@ host = 'https://your-subsonic-host.tld'
* n - Continue search forward
* N - Continue search backwards
* r - refresh the list (if in artist directory, only refreshes that artist)
* s - add 50 random songs to the queue
* y - toggle star on song
70 changes: 63 additions & 7 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ type SubsonicDirectory struct {
Entities SubsonicEntities `json:"child"`
}

type SubsonicSongs struct {
Song SubsonicEntities `json:"song"`
}

type SubsonicStarred struct {
Starred SubsonicEntities `json:"starred"`
}

type SubsonicEntity struct {
Id string `json:"id"`
IsDirectory bool `json:"isDir"`
Expand Down Expand Up @@ -130,13 +138,15 @@ type SubsonicPlaylist struct {
}

type SubsonicResponse struct {
Status string `json:"status"`
Version string `json:"version"`
Indexes SubsonicIndexes `json:"indexes"`
Directory SubsonicDirectory `json:"directory"`
Playlists SubsonicPlaylists `json:"playlists"`
Playlist SubsonicPlaylist `json:"playlist"`
Error SubsonicError `json:"error"`
Status string `json:"status"`
Version string `json:"version"`
Indexes SubsonicIndexes `json:"indexes"`
Directory SubsonicDirectory `json:"directory"`
RandomSongs SubsonicSongs `json:"randomSongs"`
Starred SubsonicSongs `json:"starred"`
Playlists SubsonicPlaylists `json:"playlists"`
Playlist SubsonicPlaylist `json:"playlist"`
Error SubsonicError `json:"error"`
}

type responseWrapper struct {
Expand Down Expand Up @@ -192,6 +202,52 @@ func (connection *SubsonicConnection) GetMusicDirectory(id string) (*SubsonicRes
return resp, nil
}

func (connection *SubsonicConnection) GetRandomSongs() (*SubsonicResponse, error) {
query := defaultQuery(connection)
// Let's get 50 random songs, default is 10
query.Set("size", "50")
requestUrl := connection.Host + "/rest/getRandomSongs" + "?" + query.Encode()
resp, err := connection.getResponse("GetRandomSongs", requestUrl)
if err != nil {
return resp, err
}
return resp, nil
}

func (connection *SubsonicConnection) GetStarred() (*SubsonicResponse, error) {
query := defaultQuery(connection)
requestUrl := connection.Host + "/rest/getStarred" + "?" + query.Encode()
resp, err := connection.getResponse("GetStarred", requestUrl)
if err != nil {
return resp, err
}
return resp, nil
}

func (connection *SubsonicConnection) ToggleStar(id string, starredItems map[string]struct{}) (*SubsonicResponse, error) {
query := defaultQuery(connection)
query.Set("id",id)

_, ok := starredItems[id]
var action = "star"
// If the key exists, we're unstarring
if ok {
action = "unstar"
}

requestUrl := connection.Host + "/rest/" + action + "?" + query.Encode()
resp, err := connection.getResponse("ToggleStar", requestUrl)
if err != nil {
if (ok) {
delete(starredItems, id)
} else {
starredItems[id] = struct{}{}
}
return resp, err
}
return resp, nil
}

func (connection *SubsonicConnection) GetPlaylists() (*SubsonicResponse, error) {
query := defaultQuery(connection)
requestUrl := connection.Host + "/rest/getPlaylists" + "?" + query.Encode()
Expand Down
Loading

0 comments on commit 3182c8d

Please sign in to comment.