2023-10-19 18:01:48 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-12-10 21:33:54 +01:00
|
|
|
"encoding/json"
|
2023-12-02 17:53:44 +01:00
|
|
|
"fmt"
|
|
|
|
"github.com/fhs/gompd/v2/mpd"
|
2023-10-24 18:17:36 +02:00
|
|
|
"github.com/labstack/echo-contrib/echoprometheus"
|
2023-12-02 17:53:44 +01:00
|
|
|
"github.com/labstack/echo/v4"
|
2023-10-24 18:17:36 +02:00
|
|
|
"github.com/labstack/echo/v4/middleware"
|
|
|
|
"golang.org/x/net/websocket"
|
2023-12-02 17:53:44 +01:00
|
|
|
"log"
|
2023-10-19 18:01:48 +02:00
|
|
|
"net/http"
|
2023-12-10 21:33:54 +01:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2023-11-07 21:16:42 +01:00
|
|
|
"strings"
|
2023-10-19 18:01:48 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
e := echo.New()
|
2023-10-24 18:17:36 +02:00
|
|
|
e.Use(middleware.Logger())
|
|
|
|
e.Use(middleware.Recover())
|
|
|
|
e.Use(middleware.Gzip())
|
|
|
|
e.Pre(middleware.RemoveTrailingSlash())
|
|
|
|
e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
|
|
|
|
Root: "static",
|
|
|
|
HTML5: true, // SPA mode; not-found will be redirected to root
|
|
|
|
}))
|
|
|
|
e.Use(echoprometheus.NewMiddleware("sanic")) // adds middleware to gather metrics
|
|
|
|
|
|
|
|
e.GET("/metrics", echoprometheus.NewHandler())
|
|
|
|
|
|
|
|
e.GET("/", func(c echo.Context) (err error) {
|
2023-12-02 17:53:44 +01:00
|
|
|
// HTTP/2 Server Push
|
2023-10-24 18:17:36 +02:00
|
|
|
pusher, ok := c.Response().Writer.(http.Pusher)
|
|
|
|
if ok {
|
|
|
|
if err = pusher.Push("/style.css", nil); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = pusher.Push("/index.js", nil); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = pusher.Push("/favicon.ico", nil); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c.File("index.html")
|
|
|
|
})
|
|
|
|
|
2023-12-02 17:53:44 +01:00
|
|
|
g := e.Group("/api")
|
|
|
|
g.GET("/previous_track", previousTrack)
|
|
|
|
g.GET("/next_track", nextTrack)
|
|
|
|
g.GET("/stop", stopPlayback)
|
|
|
|
g.GET("/play", resumePlayback)
|
|
|
|
g.GET("/pause", pausePlayback)
|
|
|
|
g.GET("/seek/:seconds", seek)
|
|
|
|
g.GET("/repeat", toggleRepeat)
|
|
|
|
g.GET("/random", toggleRandom)
|
|
|
|
g.GET("/volume/:level", setVolume)
|
|
|
|
|
2023-12-10 21:33:54 +01:00
|
|
|
g.GET("/download", downloadTrack)
|
|
|
|
|
2023-10-24 18:17:36 +02:00
|
|
|
e.GET("/ws", wsServe)
|
2023-11-07 21:16:42 +01:00
|
|
|
|
2023-12-10 21:33:54 +01:00
|
|
|
//e.Logger.Fatal(e.StartTLS(":1323", "cert.pem", "key.pem"))
|
|
|
|
e.Logger.Fatal(e.Start(":1323"))
|
2023-10-19 18:01:48 +02:00
|
|
|
}
|
2023-10-24 18:17:36 +02:00
|
|
|
|
|
|
|
func wsServe(c echo.Context) error {
|
|
|
|
websocket.Handler(func(ws *websocket.Conn) {
|
|
|
|
defer ws.Close()
|
2023-12-10 21:33:54 +01:00
|
|
|
|
|
|
|
// Connect to MPD server
|
|
|
|
mpdConn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
defer mpdConn.Close()
|
|
|
|
|
2023-10-24 18:17:36 +02:00
|
|
|
for {
|
|
|
|
// Read
|
|
|
|
msg := ""
|
2023-11-07 21:16:42 +01:00
|
|
|
err := websocket.Message.Receive(ws, &msg)
|
2023-10-24 18:17:36 +02:00
|
|
|
if err != nil {
|
|
|
|
c.Logger().Error(err)
|
2023-12-02 17:53:44 +01:00
|
|
|
break
|
|
|
|
} else {
|
2023-12-10 21:33:54 +01:00
|
|
|
log.Println(msg)
|
|
|
|
if strings.ToLower(msg) == "#status" {
|
|
|
|
// TODO: Get current MPD status and return it
|
|
|
|
status, err := mpdConn.Status()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
jsonData, err := json.Marshal(status)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
err = websocket.Message.Send(ws, fmt.Sprintf("{\"mpd_status\":%s}", string(jsonData)))
|
2023-12-02 17:53:44 +01:00
|
|
|
if err != nil {
|
|
|
|
c.Logger().Error(err)
|
|
|
|
}
|
|
|
|
|
2023-12-10 21:33:54 +01:00
|
|
|
} else if strings.HasPrefix(strings.ToLower(msg), "#download ") {
|
2023-12-02 17:53:44 +01:00
|
|
|
// Download video link as audio file
|
2023-12-10 21:33:54 +01:00
|
|
|
uri := strings.SplitN(msg, " ", 2)[1]
|
2023-12-02 17:53:44 +01:00
|
|
|
// TODO: implement yt-dlp integration
|
2023-12-10 21:33:54 +01:00
|
|
|
err := websocket.Message.Send(ws, fmt.Sprintf("Downloading %s", uri))
|
2023-12-02 17:53:44 +01:00
|
|
|
if err != nil {
|
|
|
|
c.Logger().Error(err)
|
|
|
|
}
|
2023-11-07 21:16:42 +01:00
|
|
|
}
|
|
|
|
}
|
2023-10-24 18:17:36 +02:00
|
|
|
}
|
|
|
|
}).ServeHTTP(c.Response(), c.Request())
|
|
|
|
return nil
|
|
|
|
}
|
2023-12-02 17:53:44 +01:00
|
|
|
|
2023-12-10 21:33:54 +01:00
|
|
|
func downloadTrack(c echo.Context) error {
|
|
|
|
// yt-dlp \
|
|
|
|
// --no-wait-for-video \
|
|
|
|
// --no-playlist \
|
|
|
|
// --windows-filenames \
|
|
|
|
// --newline \
|
|
|
|
// --extract-audio \
|
|
|
|
// --audio-format mp3 \
|
|
|
|
// --audio-quality 0 \
|
|
|
|
// -f bestaudio/best \
|
|
|
|
// ${video_url}
|
|
|
|
|
|
|
|
cmd := exec.Command(
|
|
|
|
"yt-dlp",
|
|
|
|
"--no-wait-for-video",
|
|
|
|
"--no-playlist",
|
|
|
|
"--windows-filenames",
|
|
|
|
"--newline",
|
|
|
|
"--extract-audio",
|
|
|
|
"--audio-format", "mp3",
|
|
|
|
"--audio-quality", "0",
|
|
|
|
"--format", "bestaudio/best",
|
|
|
|
c.Param("url"),
|
|
|
|
)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.String(http.StatusAccepted, "")
|
2023-12-02 17:53:44 +01:00
|
|
|
}
|