Add play CLI commands for albums, playlists and tracks
This commit is contained in:
@@ -5,6 +5,9 @@ import "fmt"
|
||||
const (
|
||||
PingPath = "/ping"
|
||||
PlayPath = "/transport/play"
|
||||
PlayAlbumPath = "/transport/play-album" // ?id=<album ID>&t=<firstTrack>&s=<shuffle>
|
||||
PlayPlaylistPath = "/transport/play-playlist" // ?id=<playlist ID>&t=<firstTrack>&s=<shuffle>
|
||||
PlayTrackPath = "/transport/play-track" // ?id=<track ID>
|
||||
PlayPausePath = "/transport/playpause"
|
||||
PausePath = "/transport/pause"
|
||||
StopPath = "/transport/stop"
|
||||
@@ -37,3 +40,15 @@ func SeekToSecondsPath(secs float64) string {
|
||||
func SeekBySecondsPath(secs float64) string {
|
||||
return fmt.Sprintf("%s?s=%0.2f", SeekByPath, secs)
|
||||
}
|
||||
|
||||
func BuildPlayAlbumPath(id string, firstTrack int, shuffle bool) string {
|
||||
return fmt.Sprintf("%s?id=%s&t=%d&s=%t", PlayAlbumPath, id, firstTrack, shuffle)
|
||||
}
|
||||
|
||||
func BuildPlayPlaylistPath(id string, firstTrack int, shuffle bool) string {
|
||||
return fmt.Sprintf("%s?id=%s&t=%d&s=%t", PlayPlaylistPath, id, firstTrack, shuffle)
|
||||
}
|
||||
|
||||
func BuildPlayTrackPath(id string) string {
|
||||
return fmt.Sprintf("%s?id=%s", PlayTrackPath, id)
|
||||
}
|
||||
|
||||
@@ -44,6 +44,18 @@ func (c *Client) Pause() error {
|
||||
return c.sendRequest(PausePath)
|
||||
}
|
||||
|
||||
func (c *Client) PlayAlbum(id string, firstTrack int, shuffle bool) error {
|
||||
return c.sendRequest(BuildPlayAlbumPath(id, firstTrack, shuffle))
|
||||
}
|
||||
|
||||
func (c *Client) PlayPlaylist(id string, firstTrack int, shuffle bool) error {
|
||||
return c.sendRequest(BuildPlayPlaylistPath(id, firstTrack, shuffle))
|
||||
}
|
||||
|
||||
func (c *Client) PlayTrack(id string) error {
|
||||
return c.sendRequest(BuildPlayTrackPath(id))
|
||||
}
|
||||
|
||||
func (c *Client) PlayPause() error {
|
||||
return c.sendRequest(PlayPausePath)
|
||||
}
|
||||
|
||||
@@ -19,6 +19,9 @@ type PlaybackHandler interface {
|
||||
SeekBySeconds(float64)
|
||||
Volume() int
|
||||
SetVolume(int)
|
||||
PlayAlbum(string, int, bool) error
|
||||
PlayPlaylist(string, int, bool) error
|
||||
PlayTrack(string) error
|
||||
}
|
||||
|
||||
type IPCServer interface {
|
||||
@@ -86,6 +89,13 @@ func (s *serverImpl) createHandler() http.Handler {
|
||||
vol = vol + int(float64(vol)*(pct/100))
|
||||
s.pbHandler.SetVolume(vol) // will clamp to range for us
|
||||
}))
|
||||
m.HandleFunc(PlayAlbumPath, s.makeTracklistEndpointHandler(s.pbHandler.PlayAlbum))
|
||||
m.HandleFunc(PlayPlaylistPath, s.makeTracklistEndpointHandler(s.pbHandler.PlayPlaylist))
|
||||
m.HandleFunc(PlayTrackPath, func(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.URL.Query().Get("id")
|
||||
s.pbHandler.PlayTrack(id)
|
||||
s.writeOK(w)
|
||||
})
|
||||
return m
|
||||
}
|
||||
|
||||
@@ -108,6 +118,25 @@ func (s *serverImpl) makeFloatEndpointHandler(queryParam string, f func(float64)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *serverImpl) makeTracklistEndpointHandler(f func(string, int, bool) error) func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
query := r.URL.Query()
|
||||
id := query.Get("id")
|
||||
firstTrack, err := strconv.Atoi(query.Get("t"))
|
||||
if err != nil {
|
||||
s.writeErr(w, err)
|
||||
return
|
||||
}
|
||||
shuffle, err := strconv.ParseBool(query.Get("s"))
|
||||
if err != nil {
|
||||
s.writeErr(w, err)
|
||||
return
|
||||
}
|
||||
f(id, firstTrack, shuffle)
|
||||
s.writeOK(w)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *serverImpl) writeOK(w http.ResponseWriter) (int, error) {
|
||||
var r Response
|
||||
b, err := json.Marshal(&r)
|
||||
|
||||
Reference in New Issue
Block a user