1
0
dwelling-radio/internal/radio/icecast.go

93 lines
2.2 KiB
Go
Raw Normal View History

2022-03-08 01:17:24 +04:00
package radio
import (
"encoding/json"
"fmt"
2022-03-08 03:41:19 +04:00
"log"
2022-03-08 01:17:24 +04:00
"net/http"
"os/exec"
"strings"
"time"
)
type IcecastStatusDTO struct {
Icestats struct {
ServerStartISO8601 string `json:"server_start_iso8601"`
ServerStartDate string `json:"server_start"`
Source struct {
Artist string `json:"artist"`
Title string `json:"title"`
ListenerPeak int `json:"listener_peak"`
Listeners int `json:"listeners"`
} `json:"source"`
} `json:"icestats"`
}
type IcecastStatus struct {
ServerStartISO8601 string `json:"server_start_iso8601"`
ServerStartDate string `json:"server_start"`
SongName string `json:"song"`
ListenerPeak int `json:"listener_peak"`
Listeners int `json:"listeners"`
}
type Song struct {
Time string `json:"time"`
Artist string `json:"artist"`
Title string `json:"title"`
}
func IcecastGetStatus(icecastURL string) (*IcecastStatus, error) {
resp, err := http.Get(icecastURL)
if err != nil {
return nil, err
}
iceStatDTO := &IcecastStatusDTO{}
if err := json.NewDecoder(resp.Body).Decode(iceStatDTO); err != nil {
return nil, err
}
iceStat := &IcecastStatus{
ServerStartISO8601: iceStatDTO.Icestats.ServerStartISO8601,
ServerStartDate: iceStatDTO.Icestats.ServerStartDate,
SongName: iceStatDTO.Song(),
ListenerPeak: iceStatDTO.Icestats.Source.ListenerPeak,
Listeners: iceStatDTO.Icestats.Source.Listeners,
}
return iceStat, nil
}
func (is *IcecastStatusDTO) Song() string {
return fmt.Sprintf("%s - %s", is.Icestats.Source.Artist, is.Icestats.Source.Title)
}
func IcecastLastPlayedSongs(lastNSongs int, playlistPath string) []Song {
songs := make([]Song, 0)
cmd := fmt.Sprintf("tail -n%d %s | head -n-1 | cut -d\" | \" -f1,4", lastNSongs+1, playlistPath)
o := exec.Command("bash", "-c", cmd)
out, _ := o.CombinedOutput()
if len(out) == 0 {
return songs
}
songs_ := strings.Split(string(out), "\n")
2022-03-09 23:59:25 +04:00
log.Println("songs_ =", len(songs_))
2022-03-08 01:17:24 +04:00
for _, song := range songs_ {
ts := strings.Split(song, "|")
tim, _ := time.Parse("02/01/2006:15:04:05 -0700", ts[0])
2022-03-09 23:49:01 +04:00
at := strings.Split(ts[1], "-")
2022-03-08 01:17:24 +04:00
songs = append(songs, Song{
Time: tim.UTC().Format("15:04"),
Artist: at[0],
Title: at[1]})
}
return songs
}