2023-12-10 21:33:54 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-04-06 18:57:43 +02:00
|
|
|
"fmt"
|
2023-12-10 21:33:54 +01:00
|
|
|
"github.com/fhs/gompd/v2/mpd"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MPD API calls
|
|
|
|
|
2023-12-29 16:55:06 +01:00
|
|
|
func updateDb(c echo.Context) error {
|
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-29 16:55:06 +01:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
jobId, err := conn.Update("")
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-29 16:55:06 +01:00
|
|
|
}
|
|
|
|
|
2024-04-06 18:57:43 +02:00
|
|
|
return c.String(http.StatusOK, fmt.Sprintf("Database update started with job id %d", jobId))
|
2023-12-29 16:55:06 +01:00
|
|
|
}
|
|
|
|
|
2023-12-10 21:33:54 +01:00
|
|
|
func previousTrack(c echo.Context) error {
|
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
err = conn.Previous()
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
2024-04-06 18:57:43 +02:00
|
|
|
return c.String(http.StatusOK, "Playing previous track in queue")
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func nextTrack(c echo.Context) error {
|
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
err = conn.Next()
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
2024-04-06 18:57:43 +02:00
|
|
|
return c.String(http.StatusOK, "PLaying next track in queue")
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func stopPlayback(c echo.Context) error {
|
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
err = conn.Stop()
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
2024-04-06 18:57:43 +02:00
|
|
|
return c.String(http.StatusOK, "Playback stopped")
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func resumePlayback(c echo.Context) error {
|
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
2023-12-30 15:02:13 +01:00
|
|
|
status, err := conn.Status()
|
2023-12-10 21:33:54 +01:00
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
|
|
|
}
|
|
|
|
if status["state"] == "stop" {
|
|
|
|
err := conn.Play(-1)
|
|
|
|
if err != nil {
|
|
|
|
c.Logger().Error(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = conn.Pause(false)
|
|
|
|
if err != nil {
|
|
|
|
c.Logger().Error(err)
|
|
|
|
}
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
2024-04-06 18:57:43 +02:00
|
|
|
return c.String(http.StatusOK, "Playback resumed")
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func pausePlayback(c echo.Context) error {
|
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
err = conn.Pause(true)
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
2024-04-06 18:57:43 +02:00
|
|
|
return c.String(http.StatusOK, "Playback paused")
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func seek(c echo.Context) error {
|
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
seconds, err := strconv.Atoi(c.Param("seconds"))
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if seconds < 0 {
|
|
|
|
return c.String(http.StatusBadRequest, "seconds must be positive integer")
|
|
|
|
}
|
|
|
|
|
2024-04-06 18:57:43 +02:00
|
|
|
// TODO: Duration type seems to be used incorrectly
|
2023-12-10 21:33:54 +01:00
|
|
|
err = conn.SeekCur(time.Duration(seconds)*time.Second, false)
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
2024-04-06 18:57:43 +02:00
|
|
|
return c.String(http.StatusOK, fmt.Sprintf("Seeked current track to %d seconds", seconds))
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func toggleRepeat(c echo.Context) error {
|
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
status, err := conn.Status()
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
2024-04-06 18:57:43 +02:00
|
|
|
var msg string
|
2023-12-10 21:33:54 +01:00
|
|
|
if status["repeat"] == "1" {
|
|
|
|
err = conn.Repeat(false)
|
2024-04-06 18:57:43 +02:00
|
|
|
msg = "Toggled Repeat mode to off"
|
2023-12-10 21:33:54 +01:00
|
|
|
} else {
|
|
|
|
err = conn.Repeat(true)
|
2024-04-06 18:57:43 +02:00
|
|
|
msg = "Toggled Repeat mode to on"
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
2024-04-06 18:57:43 +02:00
|
|
|
return c.String(http.StatusOK, msg)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func toggleRandom(c echo.Context) error {
|
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
status, err := conn.Status()
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
2024-04-06 18:57:43 +02:00
|
|
|
var msg string
|
2023-12-30 15:02:13 +01:00
|
|
|
if status["random"] == "1" {
|
2023-12-10 21:33:54 +01:00
|
|
|
err = conn.Random(false)
|
2024-04-06 18:57:43 +02:00
|
|
|
msg = "Toggled Random mode to off"
|
2023-12-10 21:33:54 +01:00
|
|
|
} else {
|
|
|
|
err = conn.Random(true)
|
2024-04-06 18:57:43 +02:00
|
|
|
msg = "Toggled Random mode to on"
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
2024-04-06 18:57:43 +02:00
|
|
|
return c.String(http.StatusOK, msg)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func setVolume(c echo.Context) error {
|
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
level, err := strconv.Atoi(c.Param("level"))
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if level > 100 || level < 0 {
|
|
|
|
return c.String(http.StatusBadRequest, "Volume must be between 0 and 100")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = conn.SetVolume(level)
|
|
|
|
if err != nil {
|
2023-12-30 15:02:13 +01:00
|
|
|
c.Logger().Error(err)
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
2024-04-06 18:57:43 +02:00
|
|
|
return c.String(http.StatusOK, fmt.Sprintf("Set volume to %d", level))
|
2023-12-10 21:33:54 +01:00
|
|
|
}
|
2024-04-06 15:28:00 +02:00
|
|
|
|
|
|
|
// Queue
|
|
|
|
|
|
|
|
func deleteTrackFromQueue(c echo.Context) error {
|
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
|
|
|
c.Logger().Error(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
songId, err := strconv.Atoi(c.Param("song_id"))
|
|
|
|
if err != nil {
|
|
|
|
c.Logger().Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = conn.DeleteID(songId)
|
|
|
|
if err != nil {
|
|
|
|
c.Logger().Error(err)
|
|
|
|
return c.String(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-04-06 18:57:43 +02:00
|
|
|
return c.String(http.StatusOK, fmt.Sprintf("Removed song %d from queue", songId))
|
|
|
|
}
|
|
|
|
|
|
|
|
func moveTrackInQueue(c echo.Context) error {
|
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
|
|
|
c.Logger().Error(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
songId, err := strconv.Atoi(c.Param("song_id"))
|
|
|
|
if err != nil {
|
|
|
|
c.Logger().Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
position, err := strconv.Atoi(c.Param("position"))
|
|
|
|
if err != nil {
|
|
|
|
c.Logger().Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = conn.MoveID(songId, position)
|
|
|
|
if err != nil {
|
|
|
|
c.Logger().Error(err)
|
|
|
|
return c.String(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.String(http.StatusOK, fmt.Sprintf("Moved song %d to position %d", songId, position))
|
2024-04-06 15:28:00 +02:00
|
|
|
}
|
2024-04-16 23:37:48 +02:00
|
|
|
|
2024-04-18 14:28:02 +02:00
|
|
|
func attachPlaylist(c echo.Context) error {
|
2024-04-16 23:37:48 +02:00
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
|
|
|
c.Logger().Error(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
2024-04-18 14:28:02 +02:00
|
|
|
name := c.Param("playlist_name")
|
|
|
|
|
|
|
|
err = conn.PlaylistLoad(name, -1, -1)
|
2024-04-16 23:37:48 +02:00
|
|
|
if err != nil {
|
|
|
|
c.Logger().Error(err)
|
|
|
|
return c.String(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-04-18 14:28:02 +02:00
|
|
|
return c.JSON(http.StatusOK, "")
|
2024-04-16 23:37:48 +02:00
|
|
|
}
|
2024-04-17 00:24:18 +02:00
|
|
|
|
2024-04-18 14:28:02 +02:00
|
|
|
func replaceQueue(c echo.Context) error {
|
2024-04-17 00:24:18 +02:00
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
|
|
|
c.Logger().Error(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
2024-04-18 14:28:02 +02:00
|
|
|
name := c.Param("playlist_name")
|
2024-04-17 00:24:18 +02:00
|
|
|
|
2024-04-18 14:28:02 +02:00
|
|
|
err = conn.Clear()
|
|
|
|
if err != nil {
|
|
|
|
c.Logger().Error(err)
|
|
|
|
return c.String(http.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
err = conn.PlaylistLoad(name, -1, -1)
|
2024-04-17 00:24:18 +02:00
|
|
|
if err != nil {
|
|
|
|
c.Logger().Error(err)
|
|
|
|
return c.String(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-04-18 14:28:02 +02:00
|
|
|
return c.JSON(http.StatusOK, "")
|
2024-04-17 00:24:18 +02:00
|
|
|
}
|
2024-04-18 11:46:09 +02:00
|
|
|
|
2024-04-18 14:28:02 +02:00
|
|
|
// Playlists
|
|
|
|
|
|
|
|
func listPlaylists(c echo.Context) error {
|
2024-04-18 11:46:09 +02:00
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
|
|
|
c.Logger().Error(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
2024-04-18 14:28:02 +02:00
|
|
|
playlists, err := conn.ListPlaylists()
|
|
|
|
if err != nil {
|
|
|
|
c.Logger().Error(err)
|
|
|
|
return c.String(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
2024-04-18 11:46:09 +02:00
|
|
|
|
2024-04-18 14:28:02 +02:00
|
|
|
return c.JSON(http.StatusOK, playlists)
|
|
|
|
}
|
|
|
|
|
|
|
|
func listPlaylist(c echo.Context) error {
|
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
2024-04-18 11:46:09 +02:00
|
|
|
if err != nil {
|
|
|
|
c.Logger().Error(err)
|
|
|
|
}
|
2024-04-18 14:28:02 +02:00
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
name := c.Param("name")
|
|
|
|
|
|
|
|
playlist, err := conn.PlaylistContents(name)
|
2024-04-18 11:46:09 +02:00
|
|
|
if err != nil {
|
|
|
|
c.Logger().Error(err)
|
|
|
|
return c.String(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-04-18 14:28:02 +02:00
|
|
|
return c.JSON(http.StatusOK, playlist)
|
2024-04-18 11:46:09 +02:00
|
|
|
}
|
2024-04-18 12:12:52 +02:00
|
|
|
|
2024-04-18 14:28:02 +02:00
|
|
|
func deletePlaylist(c echo.Context) error {
|
2024-04-18 12:12:52 +02:00
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
|
|
|
c.Logger().Error(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
2024-04-18 14:28:02 +02:00
|
|
|
name := c.Param("name")
|
2024-04-18 12:12:52 +02:00
|
|
|
|
2024-04-18 14:28:02 +02:00
|
|
|
err = conn.PlaylistRemove(name)
|
2024-04-18 12:12:52 +02:00
|
|
|
if err != nil {
|
2024-04-18 14:28:02 +02:00
|
|
|
c.Logger().Error(fmt.Sprintf("Couldn't delete playlist %s", name))
|
2024-04-18 12:12:52 +02:00
|
|
|
c.Logger().Error(err)
|
|
|
|
return c.String(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-04-18 14:28:02 +02:00
|
|
|
return c.JSON(http.StatusNoContent, "")
|
2024-04-18 12:12:52 +02:00
|
|
|
}
|