2023-12-05 14:36:45 +01:00
|
|
|
//go:generate fyne bundle -o bundle.go res/icon.png
|
|
|
|
//go:generate fyne bundle -o bundle.go -append res/header.png
|
|
|
|
|
2023-12-04 01:24:00 +01:00
|
|
|
package main
|
|
|
|
|
2023-12-04 11:34:14 +01:00
|
|
|
/*
|
|
|
|
* This app is basically a frontend using MPlayer to stream, so we don't have to deal with
|
|
|
|
* complicated streaming stuff when there's usually a perfectly working program that will
|
|
|
|
* do this better than we can ever do.
|
|
|
|
*
|
|
|
|
* We keep control of MPlayer through the MPlayer object and pipes to send it commands to
|
|
|
|
* play, stop and which is the URL we want to stream (usually, RadioSpiral's).
|
|
|
|
*
|
|
|
|
* There is also a goroutine that checks the broadcast information every minute, updates
|
|
|
|
* the GUI with the currently playing information and also the next show coming up.
|
|
|
|
*/
|
|
|
|
|
2023-12-04 01:24:00 +01:00
|
|
|
import (
|
|
|
|
"bufio"
|
2023-12-04 11:15:05 +01:00
|
|
|
"encoding/json"
|
2023-12-05 01:08:51 +01:00
|
|
|
"image"
|
2023-12-04 01:24:00 +01:00
|
|
|
"io"
|
2023-12-05 12:42:18 +01:00
|
|
|
"log"
|
2023-12-04 11:15:05 +01:00
|
|
|
"net/http"
|
2023-12-05 12:42:18 +01:00
|
|
|
"os"
|
2023-12-04 01:24:00 +01:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2023-12-04 11:15:05 +01:00
|
|
|
"time"
|
2023-12-04 01:24:00 +01:00
|
|
|
|
2023-12-04 11:15:05 +01:00
|
|
|
"fyne.io/fyne/v2"
|
2023-12-04 01:24:00 +01:00
|
|
|
"fyne.io/fyne/v2/app"
|
2023-12-05 01:08:51 +01:00
|
|
|
"fyne.io/fyne/v2/canvas"
|
2023-12-04 01:24:00 +01:00
|
|
|
"fyne.io/fyne/v2/container"
|
2023-12-04 11:15:05 +01:00
|
|
|
"fyne.io/fyne/v2/layout"
|
2023-12-04 01:56:39 +01:00
|
|
|
"fyne.io/fyne/v2/theme"
|
2023-12-04 01:24:00 +01:00
|
|
|
"fyne.io/fyne/v2/widget"
|
|
|
|
)
|
|
|
|
|
|
|
|
// helper
|
|
|
|
func check(err error) {
|
|
|
|
if err != nil {
|
2023-12-05 12:42:18 +01:00
|
|
|
log.Panic(err)
|
2023-12-04 01:24:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-04 11:15:05 +01:00
|
|
|
// 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"`
|
|
|
|
}
|
|
|
|
|
2023-12-04 01:24:00 +01:00
|
|
|
// Radio player interface
|
|
|
|
type RadioPlayer interface {
|
|
|
|
Play(stream_url string)
|
|
|
|
Mute()
|
|
|
|
Pause()
|
|
|
|
IncVolume()
|
|
|
|
DecVolume()
|
|
|
|
Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MPlayer
|
|
|
|
type MPlayer struct {
|
|
|
|
player_name string
|
|
|
|
is_playing bool
|
|
|
|
stream_url string
|
|
|
|
command *exec.Cmd
|
|
|
|
in io.WriteCloser
|
|
|
|
out io.ReadCloser
|
|
|
|
pipe_chan chan io.ReadCloser
|
|
|
|
}
|
|
|
|
|
|
|
|
func (player *MPlayer) Play(stream_url string) {
|
|
|
|
if !player.is_playing {
|
|
|
|
var err error
|
|
|
|
is_playlist := strings.HasSuffix(stream_url, ".m3u") || strings.HasSuffix(stream_url, ".pls")
|
|
|
|
if is_playlist {
|
2023-12-04 11:15:05 +01:00
|
|
|
// player.command = exec.Command(player.player_name, "-quiet", "-playlist", stream_url)
|
2023-12-05 12:42:18 +01:00
|
|
|
player.command = exec.Command(player.player_name, "-v", "-playlist", stream_url)
|
2023-12-04 01:24:00 +01:00
|
|
|
} else {
|
2023-12-05 12:42:18 +01:00
|
|
|
player.command = exec.Command(player.player_name, "-v", stream_url)
|
2023-12-04 01:24:00 +01:00
|
|
|
}
|
|
|
|
player.in, err = player.command.StdinPipe()
|
|
|
|
check(err)
|
|
|
|
player.out, err = player.command.StdoutPipe()
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
err = player.command.Start()
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
player.is_playing = true
|
|
|
|
player.stream_url = stream_url
|
|
|
|
go func() {
|
|
|
|
player.pipe_chan <- player.out
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (player *MPlayer) Close() {
|
|
|
|
if player.is_playing {
|
|
|
|
player.is_playing = false
|
|
|
|
|
|
|
|
player.in.Write([]byte("q"))
|
|
|
|
player.in.Close()
|
|
|
|
player.out.Close()
|
|
|
|
player.command = nil
|
|
|
|
|
|
|
|
player.stream_url = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (player *MPlayer) Mute() {
|
|
|
|
if player.is_playing {
|
|
|
|
player.in.Write([]byte("m"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (player *MPlayer) Pause() {
|
|
|
|
if player.is_playing {
|
|
|
|
player.in.Write([]byte("p"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (player *MPlayer) IncVolume() {
|
|
|
|
if player.is_playing {
|
|
|
|
player.in.Write([]byte("*"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (player *MPlayer) DecVolume() {
|
|
|
|
if player.is_playing {
|
|
|
|
player.in.Write([]byte("/"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-05 01:08:51 +01:00
|
|
|
// Load images from URLs
|
|
|
|
func loadImageURL(url string) image.Image {
|
|
|
|
parts := strings.Split(url, "?")
|
|
|
|
resp, err := http.Get(parts[0])
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
img, _, err := image.Decode(resp.Body)
|
|
|
|
check(err)
|
|
|
|
return img
|
|
|
|
}
|
|
|
|
|
2023-12-04 01:24:00 +01:00
|
|
|
func main() {
|
2023-12-05 12:42:18 +01:00
|
|
|
RADIOSPIRAL_STREAM := "https://radiospiral.radio/stream.mp3"
|
2023-12-04 11:15:05 +01:00
|
|
|
RADIOSPIRAL_JSON_ENDPOINT := "https://radiospiral.net/wp-json/radio/broadcast"
|
|
|
|
|
2023-12-05 12:42:18 +01:00
|
|
|
logFile, err := os.OpenFile("radiospiral.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
|
|
|
check(err)
|
|
|
|
defer logFile.Close()
|
|
|
|
|
|
|
|
log.SetOutput(logFile)
|
|
|
|
log.Println("Starting the app")
|
|
|
|
|
2023-12-04 11:34:14 +01:00
|
|
|
// Create the status channel, to read from MPlayer and the pipe to send commands to it
|
2023-12-04 01:24:00 +01:00
|
|
|
pipe_chan := make(chan io.ReadCloser)
|
|
|
|
|
2023-12-04 11:34:14 +01:00
|
|
|
// Create our MPlayer instance
|
2023-12-04 01:24:00 +01:00
|
|
|
mplayer := MPlayer{player_name: "mplayer", is_playing: false, pipe_chan: pipe_chan}
|
|
|
|
|
2023-12-05 12:42:18 +01:00
|
|
|
// Make sure that MPlayer closes when the program ends
|
|
|
|
defer mplayer.Close()
|
2023-12-04 01:24:00 +01:00
|
|
|
|
2023-12-04 11:34:14 +01:00
|
|
|
// Create our app and window
|
2023-12-04 01:24:00 +01:00
|
|
|
app := app.New()
|
2023-12-05 12:58:10 +01:00
|
|
|
window := app.NewWindow("RadioSpiral Player")
|
2023-12-04 01:24:00 +01:00
|
|
|
|
2023-12-04 11:15:05 +01:00
|
|
|
window.Resize(fyne.NewSize(400, 600))
|
2023-12-05 14:36:45 +01:00
|
|
|
window.SetIcon(resourceIconPng)
|
2023-12-04 11:15:05 +01:00
|
|
|
|
2023-12-04 11:34:14 +01:00
|
|
|
// Keep the status of the player
|
2023-12-05 01:08:51 +01:00
|
|
|
playStatus := false
|
2023-12-04 01:56:39 +01:00
|
|
|
|
2023-12-05 12:42:18 +01:00
|
|
|
// Placeholder avatar
|
2023-12-05 01:08:51 +01:00
|
|
|
radioSpiralAvatar := loadImageURL("https://radiospiral.net/wp-content/uploads/2018/03/Radio-Spiral-Logo-1.png")
|
2023-12-05 12:42:18 +01:00
|
|
|
|
|
|
|
// Header section
|
2023-12-05 14:36:45 +01:00
|
|
|
radioSpiralImage := canvas.NewImageFromResource(resourceHeaderPng)
|
2023-12-05 12:42:18 +01:00
|
|
|
header := container.NewCenter(radioSpiralImage)
|
|
|
|
|
|
|
|
// Next show section
|
|
|
|
showAvatar := canvas.NewImageFromImage(radioSpiralAvatar)
|
|
|
|
showAvatar.SetMinSize(fyne.NewSize(200, 200))
|
|
|
|
showCard := widget.NewCard("RadioSpiral", "", showAvatar)
|
|
|
|
centerCardContainer := container.NewCenter(showCard)
|
|
|
|
|
|
|
|
radioSpiralImage.SetMinSize(fyne.NewSize(400, 120))
|
|
|
|
nowPlayingLabelHeader := widget.NewLabel("Now playing:")
|
2023-12-05 01:08:51 +01:00
|
|
|
nowPlayingLabel := widget.NewLabel("")
|
2023-12-05 12:42:18 +01:00
|
|
|
var playButton *widget.Button
|
|
|
|
volumeDown := widget.NewButtonWithIcon("", theme.VolumeDownIcon(), func() {
|
|
|
|
mplayer.DecVolume()
|
|
|
|
})
|
|
|
|
volumeUp := widget.NewButtonWithIcon("", theme.VolumeUpIcon(), func() {
|
|
|
|
mplayer.IncVolume()
|
|
|
|
})
|
|
|
|
controlContainer := container.NewHBox(
|
|
|
|
nowPlayingLabelHeader,
|
|
|
|
layout.NewSpacer(),
|
|
|
|
volumeDown,
|
|
|
|
volumeUp,
|
|
|
|
)
|
2023-12-04 11:15:05 +01:00
|
|
|
|
2023-12-05 01:08:51 +01:00
|
|
|
nowPlayingLabel.Alignment = fyne.TextAlignCenter
|
2023-12-05 12:42:18 +01:00
|
|
|
nowPlayingLabel.Wrapping = fyne.TextWrapWord
|
|
|
|
|
|
|
|
// Process the output of Mplayer here in a separate goroutine
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
out_pipe := <-pipe_chan
|
|
|
|
reader := bufio.NewReader(out_pipe)
|
|
|
|
for {
|
|
|
|
data, err := reader.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
log.Println("Reloading player")
|
|
|
|
mplayer.Close()
|
|
|
|
pipe_chan = make(chan io.ReadCloser)
|
|
|
|
mplayer = MPlayer{player_name: "mplayer", is_playing: false, pipe_chan: pipe_chan}
|
|
|
|
mplayer.Play(RADIOSPIRAL_STREAM)
|
|
|
|
playStatus = true
|
|
|
|
playButton.SetIcon(theme.MediaPlayIcon())
|
|
|
|
defer mplayer.Close()
|
|
|
|
} else {
|
|
|
|
// Check if there's an updated title and reflect it on the
|
|
|
|
// GUI
|
|
|
|
log.Print("[mplayer] " + data)
|
|
|
|
if strings.Contains(data, "StreamTitle: ") {
|
|
|
|
log.Println("Found new stream title, updating GUI")
|
|
|
|
newTitleParts := strings.Split(data, "StreamTitle: ")
|
|
|
|
nowPlayingLabel.SetText(newTitleParts[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2023-12-04 11:15:05 +01:00
|
|
|
|
2023-12-05 01:08:51 +01:00
|
|
|
playButton = widget.NewButtonWithIcon("", theme.MediaStopIcon(), func() {
|
2023-12-04 11:34:14 +01:00
|
|
|
// Here we control each time the button is pressed and update its
|
|
|
|
// appearance anytime it is clicked. We make the player start playing
|
|
|
|
// or pause.
|
2023-12-04 01:56:39 +01:00
|
|
|
if !mplayer.is_playing {
|
2023-12-05 01:08:51 +01:00
|
|
|
playButton.SetIcon(theme.MediaPlayIcon())
|
2023-12-05 12:42:18 +01:00
|
|
|
mplayer.Play(RADIOSPIRAL_STREAM)
|
2023-12-05 01:08:51 +01:00
|
|
|
playStatus = true
|
2023-12-04 01:56:39 +01:00
|
|
|
} else {
|
2023-12-05 01:08:51 +01:00
|
|
|
if playStatus {
|
|
|
|
playStatus = false
|
|
|
|
playButton.SetIcon(theme.MediaPauseIcon())
|
2023-12-04 01:56:39 +01:00
|
|
|
} else {
|
2023-12-05 01:08:51 +01:00
|
|
|
playStatus = true
|
|
|
|
playButton.SetIcon(theme.MediaPlayIcon())
|
2023-12-04 01:56:39 +01:00
|
|
|
}
|
|
|
|
mplayer.Pause()
|
|
|
|
}
|
|
|
|
})
|
2023-12-04 01:24:00 +01:00
|
|
|
|
2023-12-04 11:34:14 +01:00
|
|
|
// Layout the whole thing
|
2023-12-04 01:24:00 +01:00
|
|
|
window.SetContent(container.NewVBox(
|
2023-12-04 11:26:17 +01:00
|
|
|
header,
|
2023-12-05 01:08:51 +01:00
|
|
|
widget.NewLabel("Next show:"),
|
2023-12-05 12:42:18 +01:00
|
|
|
centerCardContainer,
|
2023-12-04 11:15:05 +01:00
|
|
|
layout.NewSpacer(),
|
2023-12-05 12:42:18 +01:00
|
|
|
controlContainer,
|
2023-12-05 01:08:51 +01:00
|
|
|
nowPlayingLabel,
|
|
|
|
playButton,
|
2023-12-04 01:24:00 +01:00
|
|
|
))
|
|
|
|
|
2023-12-04 11:34:14 +01:00
|
|
|
// Now that everything is laid out, we can start this
|
|
|
|
// small goroutine every minute, retrieve the stream data
|
|
|
|
// and the shows data, update the GUI accordingly
|
2023-12-04 11:15:05 +01:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
resp, err := http.Get(RADIOSPIRAL_JSON_ENDPOINT)
|
2023-12-05 12:42:18 +01:00
|
|
|
if err != nil {
|
|
|
|
// If we get an error fetching the data, await a minute and retry
|
|
|
|
log.Println("[ERROR] Error when querying broadcast endpoint")
|
|
|
|
log.Println(err)
|
|
|
|
time.Sleep(1 * time.Minute)
|
|
|
|
continue
|
|
|
|
}
|
2023-12-04 11:15:05 +01:00
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
2023-12-05 12:42:18 +01:00
|
|
|
if err != nil {
|
|
|
|
// We couldn't read the body, log the error, await a minute and retry
|
|
|
|
log.Println("[ERROR] Error when reading the body")
|
|
|
|
log.Println(err)
|
|
|
|
time.Sleep(1 * time.Minute)
|
|
|
|
continue
|
|
|
|
}
|
2023-12-04 11:15:05 +01:00
|
|
|
|
|
|
|
var broadcastResponse BroadcastResponse
|
2023-12-05 12:42:18 +01:00
|
|
|
|
2023-12-04 11:15:05 +01:00
|
|
|
json.Unmarshal(body, &broadcastResponse)
|
2023-12-05 12:42:18 +01:00
|
|
|
showCard.SetTitle(broadcastResponse.Broadcast.NextShow.Show.Name)
|
2023-12-05 01:08:51 +01:00
|
|
|
date := broadcastResponse.Broadcast.NextShow.Day + " " + broadcastResponse.Broadcast.NextShow.Date
|
2023-12-05 12:42:18 +01:00
|
|
|
host := "by: " + broadcastResponse.Broadcast.NextShow.Show.Hosts[0].Name
|
|
|
|
showCard.SetSubTitle(date + " " + host)
|
2023-12-05 01:08:51 +01:00
|
|
|
showAvatar.Image = loadImageURL(broadcastResponse.Broadcast.NextShow.Show.AvatarUrl)
|
|
|
|
showAvatar.Refresh()
|
2023-12-05 12:42:18 +01:00
|
|
|
time.Sleep(10 * time.Minute)
|
2023-12-04 11:15:05 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-12-04 11:34:14 +01:00
|
|
|
// Showtime!
|
2023-12-04 01:24:00 +01:00
|
|
|
window.ShowAndRun()
|
|
|
|
}
|