add media
This commit is contained in:
parent
a3b93a2059
commit
6962664fbb
56
main.go
56
main.go
|
@ -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
|
||||
})
|
||||
|
||||
|
|
Loading…
Reference in a new issue