From efd07e7126db5e77b65bcfc068faf4d15d80d064 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Cuevas?= Date: Mon, 4 Dec 2023 11:15:05 +0100 Subject: [PATCH] feat: Retrieve broadcast data, fix https problem --- main.go | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 75 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 9dc2e79..64834fb 100644 --- a/main.go +++ b/main.go @@ -2,12 +2,18 @@ package main import ( "bufio" + "encoding/json" + "fmt" "io" + "net/http" "os/exec" "strings" + "time" + "fyne.io/fyne/v2" "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/container" + "fyne.io/fyne/v2/layout" "fyne.io/fyne/v2/theme" "fyne.io/fyne/v2/widget" ) @@ -19,6 +25,44 @@ func check(err error) { } } +// JSON data we receive from the wp-json/radio/broadcast endpoint +type BroadcastResponse struct { + Broadcast BroadcastInfo `json:"broadcast"` + Updated int `json:"updated"` + Success bool `json:"success"` +} + +type BroadcastInfo struct { + NowPlaying NowPlayingInfo `json:"now_playing"` + NextShow NextShowInfo `json:"next_show"` + CurrentShow bool `json:"current_show"` +} + +type NowPlayingInfo struct { + Text string `json:"text"` + Artist string `json:"artist"` + Title string `json:"title"` +} + +type NextShowInfo struct { + Day string `json:"day"` + Date string `json:"date"` + Start string `json:"start"` + End string `json:"end"` + Show ShowData `json:"show"` +} + +type ShowData struct { + Name string `json:"name"` + AvatarUrl string `json:"avatar_url"` + ImageUrl string `json:"image_url"` + Hosts []HostsData `json:"hosts"` +} + +type HostsData struct { + Name string `json:"name"` +} + // Radio player interface type RadioPlayer interface { Play(stream_url string) @@ -45,9 +89,10 @@ func (player *MPlayer) Play(stream_url string) { var err error is_playlist := strings.HasSuffix(stream_url, ".m3u") || strings.HasSuffix(stream_url, ".pls") if is_playlist { - player.command = exec.Command(player.player_name, "-quiet", "-playlist", stream_url) + // player.command = exec.Command(player.player_name, "-quiet", "-playlist", stream_url) + player.command = exec.Command(player.player_name, "-playlist", stream_url) } else { - player.command = exec.Command(player.player_name, "-quiet", stream_url) + player.command = exec.Command(player.player_name, stream_url) } player.in, err = player.command.StdinPipe() check(err) @@ -103,11 +148,14 @@ func (player *MPlayer) DecVolume() { } func main() { + RADIOSPIRAL_JSON_ENDPOINT := "https://radiospiral.net/wp-json/radio/broadcast" + status_chan := make(chan string) pipe_chan := make(chan io.ReadCloser) mplayer := MPlayer{player_name: "mplayer", is_playing: false, pipe_chan: pipe_chan} + // Process the output of Mplayer here go func() { for { out_pipe := <-pipe_chan @@ -127,15 +175,21 @@ func main() { app := app.New() window := app.NewWindow("RadioSpiral") + window.Resize(fyne.NewSize(400, 600)) + play_status := false radiospiral_label := widget.NewLabel("RadioSpiral") - nowplaying_label := widget.NewLabel("Author - Title") + nowplaying_label := widget.NewLabel("") + + radiospiral_label.Alignment = fyne.TextAlignCenter + nowplaying_label.Alignment = fyne.TextAlignCenter + var play_button *widget.Button play_button = widget.NewButtonWithIcon("", theme.MediaStopIcon(), func() { if !mplayer.is_playing { play_button.SetIcon(theme.MediaPlayIcon()) - mplayer.Play("http://radiospiral.radio/stream.mp3") + mplayer.Play("https://radiospiral.radio/stream.mp3") play_status = true } else { if play_status { @@ -151,10 +205,27 @@ func main() { window.SetContent(container.NewVBox( radiospiral_label, + layout.NewSpacer(), nowplaying_label, play_button, )) + go func() { + for { + fmt.Println("Retrieving broadcast data") + resp, err := http.Get(RADIOSPIRAL_JSON_ENDPOINT) + check(err) + + body, err := io.ReadAll(resp.Body) + check(err) + + var broadcastResponse BroadcastResponse + json.Unmarshal(body, &broadcastResponse) + nowplaying_label.SetText("Now playing: " + broadcastResponse.Broadcast.NowPlaying.Text) + time.Sleep(60 * time.Second) + } + }() + window.ShowAndRun() mplayer.Close() }