From bddc82399b3f3e8387041b34f346e76392c5d84a Mon Sep 17 00:00:00 2001 From: "Ricardo (XenGi) Band" Date: Tue, 16 Apr 2024 23:37:48 +0200 Subject: [PATCH 01/10] add list playlists --- mpd.go | 19 +++++++++++++++++++ server.go | 2 ++ static/controls.js | 12 ++++++++++++ 3 files changed, 33 insertions(+) diff --git a/mpd.go b/mpd.go index e5c499c..f9ac923 100644 --- a/mpd.go +++ b/mpd.go @@ -238,3 +238,22 @@ func deleteTrackFromQueue(c echo.Context) error { return c.String(http.StatusOK, "") } + +// Playlists + +func listPlaylists(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() + + playlists, err := conn.ListPlaylists() + if err != nil { + c.Logger().Error(err) + return c.String(http.StatusBadRequest, err.Error()) + } + + return c.JSON(http.StatusOK, playlists) +} diff --git a/server.go b/server.go index 8c72c12..3a60c0e 100644 --- a/server.go +++ b/server.go @@ -91,6 +91,8 @@ func main() { g.GET("/queue/delete/:song_id", deleteTrackFromQueue) + g.GET("/playlists", listPlaylists) + g.GET("/download", downloadTrack) e.GET("/ws", wsServe) diff --git a/static/controls.js b/static/controls.js index 6a5ca17..8a2ced3 100644 --- a/static/controls.js +++ b/static/controls.js @@ -106,6 +106,18 @@ tab_search.addEventListener("click", e => { }); tab_playlists.addEventListener("click", e => { + fetch(`${API_URL}/playlists`).then(async r => { + if (r.status === 200) { + const playlists = await r.json(); + control_playlist_list.options.length = 0; // clear playlists + playlists.forEach(p => { + const option = document.createElement("option") + option.appendChild(document.createTextNode(p["playlist"])); + option.value = p["playlist"]; + control_playlist_list.appendChild(option) + }); + } + }); if (!tab_playlists.classList.contains("active")) { tab_browser.classList.remove("active"); tab_search.classList.remove("active") From bb19b095b606384f0c8f23e41436a01b497764c8 Mon Sep 17 00:00:00 2001 From: "Ricardo (XenGi) Band" Date: Wed, 17 Apr 2024 00:24:18 +0200 Subject: [PATCH 02/10] load playlist content into result --- mpd.go | 19 +++++++++++++++++++ server.go | 1 + static/controls.js | 31 ++++++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/mpd.go b/mpd.go index f9ac923..ea953af 100644 --- a/mpd.go +++ b/mpd.go @@ -257,3 +257,22 @@ func listPlaylists(c echo.Context) error { return c.JSON(http.StatusOK, playlists) } + +func listPlaylist(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() + + name := c.Param("name") + + playlist, err := conn.PlaylistContents(name) + if err != nil { + c.Logger().Error(err) + return c.String(http.StatusBadRequest, err.Error()) + } + + return c.JSON(http.StatusOK, playlist) +} diff --git a/server.go b/server.go index 3a60c0e..3e5810a 100644 --- a/server.go +++ b/server.go @@ -92,6 +92,7 @@ func main() { g.GET("/queue/delete/:song_id", deleteTrackFromQueue) g.GET("/playlists", listPlaylists) + g.GET("/playlists/:name", listPlaylist) g.GET("/download", downloadTrack) diff --git a/static/controls.js b/static/controls.js index 8a2ced3..77dfe80 100644 --- a/static/controls.js +++ b/static/controls.js @@ -37,6 +37,7 @@ const control_replace_playlist = document.getElementById("control-replace-playli const control_attach_playlist = document.getElementById("control-attach-playlist"); const control_save_playlist = document.getElementById("control-save-playlist"); const control_delete_playlist = document.getElementById("control-delete-playlist"); +const result_table = document.querySelector("#result tbody"); // UI controls @@ -112,8 +113,36 @@ tab_playlists.addEventListener("click", e => { control_playlist_list.options.length = 0; // clear playlists playlists.forEach(p => { const option = document.createElement("option") - option.appendChild(document.createTextNode(p["playlist"])); + option.innerText = p["playlist"]; option.value = p["playlist"]; + option.addEventListener("click", () => { + fetch(`${API_URL}/playlists/${p["playlist"]}`).then(async r => { + if (r.status === 200) { + const songs = await r.json(); + console.log(songs) + result_table.innerHTML = ""; + songs.forEach(song => { + const tr = document.createElement("tr"); + const artist = document.createElement("td"); + artist.innerText = song["Artist"]; + const title = document.createElement("td"); + title.innerText = song["Title"]; + const time = document.createElement("td"); + const seconds = parseInt(song["Time"]); + const time_hours = Math.floor(seconds / 3600); + const time_minutes = Math.floor((seconds - time_hours * 3600) / 60); + const time_seconds = Math.floor(seconds - time_hours * 3600 - time_minutes * 60); + time.innerText = `${time_hours}:${time_minutes.toString().padStart(2, '0')}:${time_seconds.toString().padStart(2, '0')}` + tr.appendChild(artist); + tr.appendChild(title); + tr.appendChild(document.createElement("td")); // album + tr.appendChild(document.createElement("td")); // genre + tr.appendChild(time); + result_table.appendChild(tr); + }); + } + }) + }); control_playlist_list.appendChild(option) }); } From 58f59017dda688f4922c1e887cc19e0a2b4df8f6 Mon Sep 17 00:00:00 2001 From: "Ricardo (XenGi) Band" Date: Thu, 18 Apr 2024 10:28:52 +0200 Subject: [PATCH 03/10] udate flake --- flake.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/flake.lock b/flake.lock index a634b1a..b110849 100644 --- a/flake.lock +++ b/flake.lock @@ -43,11 +43,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1710889954, - "narHash": "sha256-Pr6F5Pmd7JnNEMHHmspZ0qVqIBVxyZ13ik1pJtm2QXk=", + "lastModified": 1713284584, + "narHash": "sha256-rRuPBJD9+yjz7tY3xC/BvFUwloutynR9piiVE6fhGqo=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "7872526e9c5332274ea5932a0c3270d6e4724f3b", + "rev": "2b6ee326ad047870526d9a3ae88dfd0197da898d", "type": "github" }, "original": { From cfbb8a47f789951135fb02e03b5f3b87cda80bfa Mon Sep 17 00:00:00 2001 From: "Ricardo (XenGi) Band" Date: Thu, 18 Apr 2024 10:37:22 +0200 Subject: [PATCH 04/10] switch primary git repo --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index f432930..d51dbe2 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module git.berlin.ccc.de/cccb/sanic +module gitlab.com/XenGi/sanic go 1.21 From 44dd0ddd9c6d8d02b61debd356c598e32b8e91b6 Mon Sep 17 00:00:00 2001 From: "Ricardo (XenGi) Band" Date: Thu, 18 Apr 2024 10:38:52 +0200 Subject: [PATCH 05/10] update dependencies --- go.mod | 6 +++--- go.sum | 12 ++++++------ gomod2nix.toml | 12 ++++++------ 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/go.mod b/go.mod index d51dbe2..52f168e 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,8 @@ go 1.21 require ( github.com/fhs/gompd/v2 v2.3.0 - github.com/labstack/echo-contrib v0.16.0 - github.com/labstack/echo/v4 v4.11.4 + github.com/labstack/echo-contrib v0.17.0 + github.com/labstack/echo/v4 v4.12.0 golang.org/x/net v0.24.0 gopkg.in/ini.v1 v1.67.0 ) @@ -19,7 +19,7 @@ require ( github.com/mattn/go-isatty v0.0.20 // indirect github.com/prometheus/client_golang v1.19.0 // indirect github.com/prometheus/client_model v0.6.1 // indirect - github.com/prometheus/common v0.52.2 // indirect + github.com/prometheus/common v0.52.3 // indirect github.com/prometheus/procfs v0.13.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect diff --git a/go.sum b/go.sum index 42cb079..8e8f1d0 100644 --- a/go.sum +++ b/go.sum @@ -10,10 +10,10 @@ github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keL github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/labstack/echo-contrib v0.16.0 h1:vk5Kd+egpTOJxD3l+3IvZzQWPbrXiYxhkkgkJL99j/w= -github.com/labstack/echo-contrib v0.16.0/go.mod h1:mjX5VB3OqJcroIEycptBOY9Hr7rK+unq79W8QFKGNV0= -github.com/labstack/echo/v4 v4.11.4 h1:vDZmA+qNeh1pd/cCkEicDMrjtrnMGQ1QFI9gWN1zGq8= -github.com/labstack/echo/v4 v4.11.4/go.mod h1:noh7EvLwqDsmh/X/HWKPUl1AjzJrhyptRyEbQJfxen8= +github.com/labstack/echo-contrib v0.17.0 h1:xam8wakZOsiQYM14Z0og1xF3w/heWNeDF5AtC5PlX8E= +github.com/labstack/echo-contrib v0.17.0/go.mod h1:mjX5VB3OqJcroIEycptBOY9Hr7rK+unq79W8QFKGNV0= +github.com/labstack/echo/v4 v4.12.0 h1:IKpw49IMryVB2p1a4dzwlhP1O2Tf2E0Ir/450lH+kI0= +github.com/labstack/echo/v4 v4.12.0/go.mod h1:UP9Cr2DJXbOK3Kr9ONYzNowSh7HP0aG0ShAyycHSJvM= github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0= github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= @@ -27,8 +27,8 @@ github.com/prometheus/client_golang v1.19.0 h1:ygXvpU1AoN1MhdzckN+PyD9QJOSD4x7km github.com/prometheus/client_golang v1.19.0/go.mod h1:ZRM9uEAypZakd+q/x7+gmsvXdURP+DABIEIjnmDdp+k= github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E= github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY= -github.com/prometheus/common v0.52.2 h1:LW8Vk7BccEdONfrJBDffQGRtpSzi5CQaRZGtboOO2ck= -github.com/prometheus/common v0.52.2/go.mod h1:lrWtQx+iDfn2mbH5GUzlH9TSHyfZpHkSiG1W7y3sF2Q= +github.com/prometheus/common v0.52.3 h1:5f8uj6ZwHSscOGNdIQg6OiZv/ybiK2CO2q2drVZAQSA= +github.com/prometheus/common v0.52.3/go.mod h1:BrxBKv3FWBIGXw89Mg1AeBq7FSyRzXWI3l3e7W3RN5U= github.com/prometheus/procfs v0.13.0 h1:GqzLlQyfsPbaEHaQkO7tbDlriv/4o5Hudv6OXHGKX7o= github.com/prometheus/procfs v0.13.0/go.mod h1:cd4PFCR54QLnGKPaKGA6l+cfuNXtht43ZKY6tow0Y1g= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= diff --git a/gomod2nix.toml b/gomod2nix.toml index 0d7f7f4..9f3b3c0 100644 --- a/gomod2nix.toml +++ b/gomod2nix.toml @@ -14,11 +14,11 @@ schema = 3 version = "v3.2.2+incompatible" hash = "sha256-LOkpuXhWrFayvVf1GOaOmZI5YKEsgqVSb22aF8LnCEM=" [mod."github.com/labstack/echo-contrib"] - version = "v0.16.0" - hash = "sha256-YnO4Ngu+gb/upIo856FDCtcTev36Vs/xUvP2qMiSGnA=" + version = "v0.17.0" + hash = "sha256-J5S8vO8Zg9uV9A3zbILswn/oPdwLKRXncsXdEjDOmU8=" [mod."github.com/labstack/echo/v4"] - version = "v4.11.4" - hash = "sha256-pVKfkZtxi5e/1MTK2RcKWSgNpEbRDo3lKUVKo01WYO0=" + version = "v4.12.0" + hash = "sha256-TPXJv/6C53bnmcEYxa9g5Mft8u/rLT96q64tQ9+RtKU=" [mod."github.com/labstack/gommon"] version = "v0.4.2" hash = "sha256-395+BETDpv15L2lsCiEccwakXgEQxKHlYBAU0Ot3qhY=" @@ -35,8 +35,8 @@ schema = 3 version = "v0.6.1" hash = "sha256-rIDyUzNfxRA934PIoySR0EhuBbZVRK/25Jlc/r8WODw=" [mod."github.com/prometheus/common"] - version = "v0.52.2" - hash = "sha256-XQUvk9/Kwf9NDlDUVl7mOWRD7z7z9QEbLH/rNU4D2nI=" + version = "v0.52.3" + hash = "sha256-JzNAt7pimXZMnPxv8droAHJq+5OKWi9BYkGtMvqpyd0=" [mod."github.com/prometheus/procfs"] version = "v0.13.0" hash = "sha256-J31K36TkIiQU2EGOcmqDa+dkoKXiVuxafPVT4rKbEsg=" From aeaef11341e60ab66196f44259828dd12df2fb14 Mon Sep 17 00:00:00 2001 From: "Ricardo (XenGi) Band" Date: Thu, 18 Apr 2024 11:46:09 +0200 Subject: [PATCH 06/10] add replace playlist feature --- NOTES.md | 8 +++++--- mpd.go | 24 ++++++++++++++++++++++++ server.go | 1 + static/index.js | 14 +++++++------- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/NOTES.md b/NOTES.md index 03f80fc..b8f027b 100644 --- a/NOTES.md +++ b/NOTES.md @@ -45,8 +45,8 @@ - [ ] Select tracks in results - [ ] `Add` selected tracks to queue button - Playlist browser - - [ ] Show current playlists - - [ ] `Replace` current queue with playlist button + - [x] Show current playlists + - [x] `Replace` current queue with playlist button - [ ] `Attach` playlist to current queue button - [ ] `Save` current queue as playlist button - [x] Show dialog @@ -77,8 +77,10 @@ - [ ] POST `/api/queue` `{"song_id": 123}` - [x] GET `/api/queue/:song_id/delete` - [x] GET `/api/queue/:song_id/move/:position` + - [x] GET `/api/queue/replace/:playlist_name` - [ ] `/api/list_database/:path` - - [ ] `/api/list_playlists` + - [x] GET `/api/playlists` + - [x] GET `/api/playlists/:name` - [ ] `/api/save_playlist` - [ ] `/api/delete_playlist` diff --git a/mpd.go b/mpd.go index 0ad537b..5ea21fe 100644 --- a/mpd.go +++ b/mpd.go @@ -311,3 +311,27 @@ func listPlaylist(c echo.Context) error { return c.JSON(http.StatusOK, playlist) } + +func replaceQueue(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() + + name := c.Param("playlist_name") + + err = conn.Clear() + if err != nil { + c.Logger().Error(err) + return c.String(http.StatusInternalServerError, err.Error()) + } + err = conn.PlaylistLoad(name, -1, -1) + if err != nil { + c.Logger().Error(err) + return c.String(http.StatusBadRequest, err.Error()) + } + + return c.JSON(http.StatusOK, "") +} diff --git a/server.go b/server.go index 4784dc1..67baad6 100644 --- a/server.go +++ b/server.go @@ -91,6 +91,7 @@ func main() { g.GET("/queue/:song_id/delete", deleteTrackFromQueue) g.GET("/queue/:song_id/move/:position", moveTrackInQueue) + g.GET("/queue/replace/:playlist_name", replaceQueue) g.GET("/playlists", listPlaylists) g.GET("/playlists/:name", listPlaylist) diff --git a/static/index.js b/static/index.js index 6acf687..4ffcb18 100644 --- a/static/index.js +++ b/static/index.js @@ -118,15 +118,15 @@ dialog_save_playlist_close.addEventListener("click", () => { // Add API calls to controls -control_replace_playlist.addEventListener("click", e => { - fetch(`${API_URL}/`).then(async r => { +control_replace_playlist.addEventListener("click", () => { + fetch(`${API_URL}/queue/replace/${control_playlist_list.value}`).then(async r => { if (r.status !== 200) { console.error(`API returned ${r.status}: ${r.statusText}`); } }); }); -control_attach_playlist.addEventListener("click", e => { +control_attach_playlist.addEventListener("click", () => { fetch(`${API_URL}/`).then(async r => { if (r.status !== 200) { console.error(`API returned ${r.status}: ${r.statusText}`); @@ -153,7 +153,7 @@ control_delete_playlist.addEventListener("click", () => { }); }); -tab_browser.addEventListener("click", e => { +tab_browser.addEventListener("click", () => { if (!tab_browser.classList.contains("active")) { tab_browser.classList.add("active"); tab_search.classList.remove("active") @@ -164,7 +164,7 @@ tab_browser.addEventListener("click", e => { } }); -tab_search.addEventListener("click", e => { +tab_search.addEventListener("click", () => { if (!tab_search.classList.contains("active")) { tab_browser.classList.remove("active"); tab_search.classList.add("active") @@ -175,7 +175,7 @@ tab_search.addEventListener("click", e => { } }); -tab_playlists.addEventListener("click", e => { +tab_playlists.addEventListener("click", () => { fetch(`${API_URL}/playlists`).then(async r => { if (r.status === 200) { const playlists = await r.json(); @@ -224,7 +224,7 @@ tab_playlists.addEventListener("click", e => { // Add API calls to controls -control_update_db.addEventListener("click", e => { +control_update_db.addEventListener("click", () => { console.log("Issuing database update") fetch(`${API_URL}/update_db`).then(async r => { if (r.status === 200) { From 1fc5dc2bf8e2c56c8be3ba14643004b5a278da39 Mon Sep 17 00:00:00 2001 From: "Ricardo (XenGi) Band" Date: Thu, 18 Apr 2024 12:12:52 +0200 Subject: [PATCH 07/10] add attach playlist feature --- NOTES.md | 3 ++- mpd.go | 19 +++++++++++++++++++ server.go | 1 + static/index.js | 2 +- 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/NOTES.md b/NOTES.md index b8f027b..dde6d84 100644 --- a/NOTES.md +++ b/NOTES.md @@ -47,7 +47,7 @@ - Playlist browser - [x] Show current playlists - [x] `Replace` current queue with playlist button - - [ ] `Attach` playlist to current queue button + - [x] `Attach` playlist to current queue button - [ ] `Save` current queue as playlist button - [x] Show dialog - [ ] `Delete` selected playlist button @@ -78,6 +78,7 @@ - [x] GET `/api/queue/:song_id/delete` - [x] GET `/api/queue/:song_id/move/:position` - [x] GET `/api/queue/replace/:playlist_name` + - [x] GET `/api/queue/attach/:playlist_name` - [ ] `/api/list_database/:path` - [x] GET `/api/playlists` - [x] GET `/api/playlists/:name` diff --git a/mpd.go b/mpd.go index 5ea21fe..9b76102 100644 --- a/mpd.go +++ b/mpd.go @@ -335,3 +335,22 @@ func replaceQueue(c echo.Context) error { return c.JSON(http.StatusOK, "") } + +func attachPlaylist(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() + + name := c.Param("playlist_name") + + err = conn.PlaylistLoad(name, -1, -1) + if err != nil { + c.Logger().Error(err) + return c.String(http.StatusBadRequest, err.Error()) + } + + return c.JSON(http.StatusOK, "") +} diff --git a/server.go b/server.go index 67baad6..2a8881c 100644 --- a/server.go +++ b/server.go @@ -92,6 +92,7 @@ func main() { g.GET("/queue/:song_id/delete", deleteTrackFromQueue) g.GET("/queue/:song_id/move/:position", moveTrackInQueue) g.GET("/queue/replace/:playlist_name", replaceQueue) + g.GET("/queue/attach/:playlist_name", attachPlaylist) g.GET("/playlists", listPlaylists) g.GET("/playlists/:name", listPlaylist) diff --git a/static/index.js b/static/index.js index 4ffcb18..17369df 100644 --- a/static/index.js +++ b/static/index.js @@ -127,7 +127,7 @@ control_replace_playlist.addEventListener("click", () => { }); control_attach_playlist.addEventListener("click", () => { - fetch(`${API_URL}/`).then(async r => { + fetch(`${API_URL}/queue/attach/${control_playlist_list.value}`).then(async r => { if (r.status !== 200) { console.error(`API returned ${r.status}: ${r.statusText}`); } From 9248852713a1fdd56ab7fd86aad0854281b64883 Mon Sep 17 00:00:00 2001 From: "Ricardo (XenGi) Band" Date: Thu, 18 Apr 2024 14:28:02 +0200 Subject: [PATCH 08/10] add delete playlist feature --- mpd.go | 76 +++++++++++++++++++++++++++----------------- server.go | 1 + static/index.js | 84 ++++++++++++++++++++++++++----------------------- 3 files changed, 94 insertions(+), 67 deletions(-) diff --git a/mpd.go b/mpd.go index 9b76102..ca3c033 100644 --- a/mpd.go +++ b/mpd.go @@ -274,6 +274,49 @@ func moveTrackInQueue(c echo.Context) error { return c.String(http.StatusOK, fmt.Sprintf("Moved song %d to position %d", songId, position)) } +func attachPlaylist(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() + + name := c.Param("playlist_name") + + err = conn.PlaylistLoad(name, -1, -1) + if err != nil { + c.Logger().Error(err) + return c.String(http.StatusBadRequest, err.Error()) + } + + return c.JSON(http.StatusOK, "") +} + +func replaceQueue(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() + + name := c.Param("playlist_name") + + err = conn.Clear() + if err != nil { + c.Logger().Error(err) + return c.String(http.StatusInternalServerError, err.Error()) + } + err = conn.PlaylistLoad(name, -1, -1) + if err != nil { + c.Logger().Error(err) + return c.String(http.StatusBadRequest, err.Error()) + } + + return c.JSON(http.StatusOK, "") +} + // Playlists func listPlaylists(c echo.Context) error { @@ -312,7 +355,7 @@ func listPlaylist(c echo.Context) error { return c.JSON(http.StatusOK, playlist) } -func replaceQueue(c echo.Context) error { +func deletePlaylist(c echo.Context) error { // Connect to MPD server conn, err := mpd.Dial("tcp", "localhost:6600") if err != nil { @@ -320,37 +363,14 @@ func replaceQueue(c echo.Context) error { } defer conn.Close() - name := c.Param("playlist_name") + name := c.Param("name") - err = conn.Clear() - if err != nil { - c.Logger().Error(err) - return c.String(http.StatusInternalServerError, err.Error()) - } - err = conn.PlaylistLoad(name, -1, -1) + err = conn.PlaylistRemove(name) if err != nil { + c.Logger().Error(fmt.Sprintf("Couldn't delete playlist %s", name)) c.Logger().Error(err) return c.String(http.StatusBadRequest, err.Error()) } - return c.JSON(http.StatusOK, "") -} - -func attachPlaylist(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() - - name := c.Param("playlist_name") - - err = conn.PlaylistLoad(name, -1, -1) - if err != nil { - c.Logger().Error(err) - return c.String(http.StatusBadRequest, err.Error()) - } - - return c.JSON(http.StatusOK, "") + return c.JSON(http.StatusNoContent, "") } diff --git a/server.go b/server.go index 2a8881c..bace2a9 100644 --- a/server.go +++ b/server.go @@ -96,6 +96,7 @@ func main() { g.GET("/playlists", listPlaylists) g.GET("/playlists/:name", listPlaylist) + g.DELETE("/playlists/:name", deletePlaylist) g.GET("/download", downloadTrack) diff --git a/static/index.js b/static/index.js index 17369df..bf54193 100644 --- a/static/index.js +++ b/static/index.js @@ -69,6 +69,46 @@ moveTrackInQueue = (event, direction) => { }); } +refreshPlaylists = () => { + console.log("Refreshing playlists from MPD server") + fetch(`${API_URL}/playlists`).then(async r => { + if (r.status === 200) { + const playlists = await r.json(); + control_playlist_list.options.length = 0; // clear playlists + playlists.forEach(p => { + const option = document.createElement("option") + option.innerText = p["playlist"]; + option.value = p["playlist"]; + option.addEventListener("click", () => { + fetch(`${API_URL}/playlists/${p["playlist"]}`).then(async r => { + if (r.status === 200) { + const songs = await r.json(); + console.log(songs) + result_table.innerHTML = ""; + songs.forEach(song => { + const tr = document.createElement("tr"); + const artist = document.createElement("td"); + artist.innerText = song["Artist"]; + const title = document.createElement("td"); + title.innerText = song["Title"]; + const time = document.createElement("td"); + time.innerText = secondsToTrackTime(parseInt(song["Time"])) + tr.appendChild(artist); + tr.appendChild(title); + tr.appendChild(document.createElement("td")); // album + tr.appendChild(document.createElement("td")); // genre + tr.appendChild(time); + result_table.appendChild(tr); + }); + } + }) + }); + control_playlist_list.appendChild(option) + }); + } + }); +} + // UI controls tab_browser.addEventListener("click", () => { @@ -143,10 +183,11 @@ dialog_save_playlist_submit.addEventListener("click", () => { }); control_delete_playlist.addEventListener("click", () => { - const playlist_id = control_playlist_list.value; - fetch(`${API_URL}/playlists/${playlist_id}`, {method: "DELETE"}).then(r => { + const playlist_name = control_playlist_list.value; + fetch(`${API_URL}/playlists/${control_playlist_list.value}`, {method: "DELETE"}).then(r => { if (r.status === 204) { - console.log(`Playlist ${playlist_id} successfully deleted.`); + console.log(`Playlist "${playlist_name}" successfully deleted.`); + refreshPlaylists(); } else { console.error(`API returned ${r.status}: ${r.statusText}`); } @@ -176,42 +217,7 @@ tab_search.addEventListener("click", () => { }); tab_playlists.addEventListener("click", () => { - fetch(`${API_URL}/playlists`).then(async r => { - if (r.status === 200) { - const playlists = await r.json(); - control_playlist_list.options.length = 0; // clear playlists - playlists.forEach(p => { - const option = document.createElement("option") - option.innerText = p["playlist"]; - option.value = p["playlist"]; - option.addEventListener("click", () => { - fetch(`${API_URL}/playlists/${p["playlist"]}`).then(async r => { - if (r.status === 200) { - const songs = await r.json(); - console.log(songs) - result_table.innerHTML = ""; - songs.forEach(song => { - const tr = document.createElement("tr"); - const artist = document.createElement("td"); - artist.innerText = song["Artist"]; - const title = document.createElement("td"); - title.innerText = song["Title"]; - const time = document.createElement("td"); - time.innerText = secondsToTrackTime(parseInt(song["Time"])) - tr.appendChild(artist); - tr.appendChild(title); - tr.appendChild(document.createElement("td")); // album - tr.appendChild(document.createElement("td")); // genre - tr.appendChild(time); - result_table.appendChild(tr); - }); - } - }) - }); - control_playlist_list.appendChild(option) - }); - } - }); + refreshPlaylists(); if (!tab_playlists.classList.contains("active")) { tab_browser.classList.remove("active"); tab_search.classList.remove("active") From 26f230e039853d32d5079a4c38d2128fe4f9e815 Mon Sep 17 00:00:00 2001 From: "Ricardo (XenGi) Band" Date: Thu, 18 Apr 2024 14:28:37 +0200 Subject: [PATCH 09/10] update docs --- NOTES.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/NOTES.md b/NOTES.md index dde6d84..6fed823 100644 --- a/NOTES.md +++ b/NOTES.md @@ -50,7 +50,7 @@ - [x] `Attach` playlist to current queue button - [ ] `Save` current queue as playlist button - [x] Show dialog - - [ ] `Delete` selected playlist button + - [x] `Delete` selected playlist button ## backend @@ -82,6 +82,7 @@ - [ ] `/api/list_database/:path` - [x] GET `/api/playlists` - [x] GET `/api/playlists/:name` + - [x] DELETE `/api/playlists/:name` - [ ] `/api/save_playlist` - [ ] `/api/delete_playlist` From cdf12411a24c8f67091c32e8f532c01cdf4fc0ca Mon Sep 17 00:00:00 2001 From: "Ricardo (XenGi) Band" Date: Thu, 18 Apr 2024 15:06:12 +0200 Subject: [PATCH 10/10] add save playlist feature --- NOTES.md | 6 ++---- mpd.go | 20 +++++++++++++++++++- server.go | 1 + static/index.js | 10 ++++++++-- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/NOTES.md b/NOTES.md index 6fed823..b767ab9 100644 --- a/NOTES.md +++ b/NOTES.md @@ -48,7 +48,7 @@ - [x] Show current playlists - [x] `Replace` current queue with playlist button - [x] `Attach` playlist to current queue button - - [ ] `Save` current queue as playlist button + - [x] `Save` current queue as playlist button - [x] Show dialog - [x] `Delete` selected playlist button @@ -81,8 +81,6 @@ - [x] GET `/api/queue/attach/:playlist_name` - [ ] `/api/list_database/:path` - [x] GET `/api/playlists` + - [x] POST `/api/playlists/:name` - [x] GET `/api/playlists/:name` - [x] DELETE `/api/playlists/:name` - - [ ] `/api/save_playlist` - - [ ] `/api/delete_playlist` - diff --git a/mpd.go b/mpd.go index ca3c033..7983882 100644 --- a/mpd.go +++ b/mpd.go @@ -367,10 +367,28 @@ func deletePlaylist(c echo.Context) error { err = conn.PlaylistRemove(name) if err != nil { - c.Logger().Error(fmt.Sprintf("Couldn't delete playlist %s", name)) c.Logger().Error(err) return c.String(http.StatusBadRequest, err.Error()) } return c.JSON(http.StatusNoContent, "") } + +func savePlaylist(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() + + name := c.Param("name") + + err = conn.PlaylistSave(name) + if err != nil { + c.Logger().Error(err) + return c.String(http.StatusBadRequest, err.Error()) + } + + return c.JSON(http.StatusCreated, "") +} diff --git a/server.go b/server.go index bace2a9..e790503 100644 --- a/server.go +++ b/server.go @@ -95,6 +95,7 @@ func main() { g.GET("/queue/attach/:playlist_name", attachPlaylist) g.GET("/playlists", listPlaylists) + g.POST("/playlists/:name", savePlaylist) g.GET("/playlists/:name", listPlaylist) g.DELETE("/playlists/:name", deletePlaylist) diff --git a/static/index.js b/static/index.js index bf54193..86bffcb 100644 --- a/static/index.js +++ b/static/index.js @@ -7,7 +7,7 @@ const VOLUME_STEP = 5; const dialog_save_playlist = document.getElementById("save-playlist"); const control_playlist_name = document.getElementById("control-playlist-name"); -const dialog_save_playlist_submit = document.querySelector("#save-playlist button"); +const dialog_save_playlist_submit = document.querySelector("#save-playlist form button"); const dialog_save_playlist_close = document.querySelector("#save-playlist .close"); const connection_state = document.getElementById("connection-state"); @@ -144,10 +144,12 @@ tab_playlists.addEventListener("click", () => { } }); +// Show "Save playlist" modal control_save_playlist.addEventListener("click", () => { dialog_save_playlist.showModal() }); +// Close "Save playlist" modal dialog_save_playlist_close.addEventListener("click", () => { dialog_save_playlist.close() }); @@ -174,10 +176,14 @@ control_attach_playlist.addEventListener("click", () => { }); }); +// Save current queue as new playlist and refresh playlist list dialog_save_playlist_submit.addEventListener("click", () => { - fetch(`${API_URL}/playlists`, {method: "PUT"}).then(async r => { + fetch(`${API_URL}/playlists/${control_playlist_name.value}`, {method: "POST"}).then(async r => { if (r.status === 201) { console.log(`Playlist "${control_playlist_name.value}" saved`) + refreshPlaylists() + } else { + console.error(`API returned ${r.status}: ${r.statusText}`); } }); });