From a428cce6338e4d09ef6b036542ab0b0f9076ce22 Mon Sep 17 00:00:00 2001 From: DictumMortuum Date: Thu, 2 Jan 2025 22:26:04 +0200 Subject: [PATCH] [extapi] added start/stop to icecast playlist via mpd --- cmd/servus-extapi/main.go | 5 +++-- go.mod | 1 + go.sum | 2 ++ pkg/model/playlist.go | 41 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/cmd/servus-extapi/main.go b/cmd/servus-extapi/main.go index 1a9d15d..6c1e1d6 100644 --- a/cmd/servus-extapi/main.go +++ b/cmd/servus-extapi/main.go @@ -12,7 +12,7 @@ import ( func Version(c *gin.Context) { rs := map[string]any{ - "version": "v0.0.42", + "version": "v0.0.43", } c.AbortWithStatusJSON(200, rs) } @@ -62,7 +62,8 @@ func main() { g.GET("/eurovisionvotes/all", adapter.A(model.GetEurovisionVotes), middleware.ResultRa) g.GET("/eurovisionparticipations/user/:id", middleware.Id, adapter.A(model.GetEurovisionParticipationsByUserId), middleware.ResultRa) g.GET("/players/email/:id", middleware.Id, adapter.A(model.GetPlayerByEmail), middleware.ResultRa) - g.GET("/playlists/play/:id", middleware.Id, adapter.A(model.M3u), middleware.ResultRa) + g.GET("/playlists/play/:id", middleware.Id, adapter.A(model.PlaylistPlay), middleware.ResultRa) + g.GET("/playlists/stop", adapter.A(model.PlaylistStop), middleware.ResultRa) // cachedPrices := model.CachedPrice{} // g.GET("/cachedprices/search/:id", OpenDB, Id, LoadOne(cachedPrices.Get), adapter.G(bgg.SearchCachedPriceOnBgg), CloseDB) diff --git a/go.mod b/go.mod index 08ddfdc..7a67ff2 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/DictumMortuum/servus v0.7.23 github.com/MicahParks/keyfunc/v2 v2.1.0 github.com/boggydigital/yt_urls v0.1.47 + github.com/fhs/gompd v1.0.1 github.com/gin-contrib/cache v1.3.0 github.com/gin-contrib/cors v1.7.2 github.com/gin-gonic/gin v1.10.0 diff --git a/go.sum b/go.sum index c39533d..8ae1a35 100644 --- a/go.sum +++ b/go.sum @@ -90,6 +90,8 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= +github.com/fhs/gompd v1.0.1 h1:kBcAhjnAPJQAylZXR0TeH+d2vpjawXlTtKYguqNlF4A= +github.com/fhs/gompd v1.0.1/go.mod h1:b219/mNa9PvRqvkUip51b23hGL3iX4d4q3gNXdtrD04= github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/gabriel-vasile/mimetype v1.4.5 h1:J7wGKdGu33ocBOhGy0z653k/lFKLFDPJMG8Gql0kxn4= diff --git a/pkg/model/playlist.go b/pkg/model/playlist.go index d6659df..07e8988 100644 --- a/pkg/model/playlist.go +++ b/pkg/model/playlist.go @@ -6,6 +6,7 @@ import ( "os" "github.com/boggydigital/yt_urls" + "github.com/fhs/gompd/mpd" "gorm.io/gorm" ) @@ -105,6 +106,46 @@ func (obj Playlist) Delete(db *gorm.DB, id int64) (any, error) { return data, nil } +func PlaylistPlay(req *Map, res *Map) error { + playlist_id, err := req.GetString("id") + if err != nil { + return err + } + + conn, err := mpd.Dial("tcp", "localhost:6600") + if err != nil { + return err + } + defer conn.Close() + + err = conn.Add("playlists/" + playlist_id) + if err != nil { + return err + } + + err = conn.Play(-1) + if err != nil { + return err + } + + return nil +} + +func PlaylistStop(req *Map, res *Map) error { + conn, err := mpd.Dial("tcp", "localhost:6600") + if err != nil { + return err + } + defer conn.Close() + + err = conn.Clear() + if err != nil { + return err + } + + return nil +} + func M3u(req *Map, res *Map) error { playlist_id, err := req.GetString("id") if err != nil {