From 62598c805ee3b574d38df7daf2d1e1779f1dbaa6 Mon Sep 17 00:00:00 2001 From: "Ricardo (XenGi) Band" Date: Mon, 29 Apr 2024 00:14:39 +0200 Subject: [PATCH] add search db feature --- NOTES.md | 2 +- mpd.go | 45 +++++++++++++++++++++++++++++++++++++-- server.go | 2 ++ static/index.html | 13 ++++++------ static/index.js | 54 +++++++++++++++++++++++++++++------------------ static/style.css | 9 ++++++++ 6 files changed, 94 insertions(+), 31 deletions(-) diff --git a/NOTES.md b/NOTES.md index b767ab9..7bdec18 100644 --- a/NOTES.md +++ b/NOTES.md @@ -79,7 +79,7 @@ - [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` + - [ ] GET `/api/database/:path` - [x] GET `/api/playlists` - [x] POST `/api/playlists/:name` - [x] GET `/api/playlists/:name` diff --git a/mpd.go b/mpd.go index 7983882..8a0e416 100644 --- a/mpd.go +++ b/mpd.go @@ -371,7 +371,7 @@ func deletePlaylist(c echo.Context) error { return c.String(http.StatusBadRequest, err.Error()) } - return c.JSON(http.StatusNoContent, "") + return c.String(http.StatusNoContent, "") } func savePlaylist(c echo.Context) error { @@ -390,5 +390,46 @@ func savePlaylist(c echo.Context) error { return c.String(http.StatusBadRequest, err.Error()) } - return c.JSON(http.StatusCreated, "") + return c.String(http.StatusCreated, "") +} + +func searchDatabase(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() + + pattern := c.Param("pattern") + + artistResult, err := conn.Search("artist", pattern) + if err != nil { + c.Logger().Error(err) + return c.String(http.StatusBadRequest, err.Error()) + } + albumResult, err := conn.Search("album", pattern) + if err != nil { + c.Logger().Error(err) + return c.String(http.StatusBadRequest, err.Error()) + } + titleResult, err := conn.Search("title", pattern) + if err != nil { + c.Logger().Error(err) + return c.String(http.StatusBadRequest, err.Error()) + } + + songs := append(append(artistResult, albumResult...), titleResult...) + + // make list unique + uniqueList := make([]mpd.Attrs, 0, len(songs)) + keep := make(map[string]bool) + for _, song := range songs { + if _, ok := keep[song["file"]]; !ok { + keep[song["file"]] = true + uniqueList = append(uniqueList, song) + } + } + + return c.JSON(http.StatusOK, uniqueList) } diff --git a/server.go b/server.go index e790503..d145897 100644 --- a/server.go +++ b/server.go @@ -99,6 +99,8 @@ func main() { g.GET("/playlists/:name", listPlaylist) g.DELETE("/playlists/:name", deletePlaylist) + g.GET("/database/:pattern", searchDatabase) + g.GET("/download", downloadTrack) e.GET("/ws", wsServe) diff --git a/static/index.html b/static/index.html index 5eb308b..15584db 100644 --- a/static/index.html +++ b/static/index.html @@ -1,11 +1,11 @@ - + Sanic - + - + @@ -14,7 +14,7 @@
- +
@@ -187,9 +187,8 @@