From bddc82399b3f3e8387041b34f346e76392c5d84a Mon Sep 17 00:00:00 2001 From: "Ricardo (XenGi) Band" Date: Tue, 16 Apr 2024 23:37:48 +0200 Subject: [PATCH 1/2] 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 2/2] 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) }); }