add media

This commit is contained in:
Simon Vieille 2023-08-24 22:11:40 +02:00
parent a3b93a2059
commit 6962664fbb
Signed by: deblan
GPG key ID: 579388D585F70417

56
main.go
View file

@ -186,6 +186,62 @@ func ws(c echo.Context) error {
})
actions.add("media", func(ws *websocket.Conn, msg string) error {
value := getSimpleMessageValue(msg)
if value == "" {
return errors.New("Invalid value")
}
var arg string
if value == "playpause" {
arg = "play-pause"
} else if value == "next" {
arg = "next"
} else if value == "prev" {
arg = "previous"
} else {
return errors.New("Invalid value")
}
cmd := exec.Command("playerctl", "-p", "spotify", arg)
err := cmd.Run()
if err != nil {
return err
}
time.Sleep(400 * time.Millisecond)
cmd = exec.Command("playerctl", "-p", "spotify", "status")
output, err := cmd.Output()
value = strings.TrimSpace(string(output))
if err != nil {
return err
}
if value == "Playing" {
cmd = exec.Command("playerctl", "-p", "spotify", "metadata", "xesam:title")
output, err := cmd.Output()
value = strings.TrimSpace(string(output))
if err != nil {
return err
}
sendMessageResponse(ws, MessageResponse{
Type: "response",
Value: fmt.Sprintf("Playing: %s", value),
})
} else {
sendMessageResponse(ws, MessageResponse{
Type: "response",
Value: "Paused",
})
}
return nil
})