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

231 lines
5.2 KiB
Go

package radio
import (
"bytes"
"dwelling-radio/pkg/watcher"
"encoding/json"
"io"
"log"
"net/http"
"os"
"sync"
"syscall"
"time"
"github.com/pkg/errors"
)
const (
IcecastPlaylistDateFormat = "02/Jan/2006:15:04:05 -0700"
SongTimeFormat = "2006 15:04-0700"
bufferSizePerLine = 320
)
var (
currentlyPlaying Song
lastPlayedCache []Song
lastPlayedCacheMutex sync.Mutex
)
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"`
}
func (is *IcecastStatusDTO) SongName() string {
return is.Icestats.Source.Artist + " - " + is.Icestats.Source.Title
}
type IcecastStatus struct {
SongName string `json:"song"`
ListenerPeak int `json:"listener_peak"`
Listeners int `json:"listeners"`
}
type Song struct {
Time string `json:"time"`
Listeners string `json:"listeners"`
Song string `json:"song"`
}
func IcecastGetStatus(icecastURL string) (*IcecastStatus, error) {
resp, err := http.Get(icecastURL)
if err != nil {
return &IcecastStatus{SongName: "Offline"}, err
}
iceStatDTO := &IcecastStatusDTO{}
if err := json.NewDecoder(resp.Body).Decode(iceStatDTO); err != nil {
return &IcecastStatus{}, err
}
return &IcecastStatus{
SongName: iceStatDTO.SongName(),
ListenerPeak: iceStatDTO.Icestats.Source.ListenerPeak,
Listeners: iceStatDTO.Icestats.Source.Listeners,
}, nil
}
func IcecastLastSongs(playlistPath string) ([]Song, error) {
lastPlayedCacheMutex.Lock()
defer lastPlayedCacheMutex.Unlock()
if lpcLen := len(lastPlayedCache); lpcLen > 0 {
ret := make([]Song, 0, lpcLen)
ret = append(ret, lastPlayedCache...)
return ret, nil
}
return nil, nil
}
func IcecastLastSong(playlistPath string) (*Song, error) {
lastPlayedCacheMutex.Lock()
defer lastPlayedCacheMutex.Unlock()
if lpcLen := len(lastPlayedCache); lpcLen > 0 {
return &lastPlayedCache[lpcLen-1], nil
}
return nil, nil
}
func icecastLastPlayedSongs(playlistPath string, n int) ([]Song, error) {
var buf []byte
var offset int64 = 0
var bufferSize int64 = int64(n) * bufferSizePerLine
playlist, err := os.Open(playlistPath)
if err != nil {
return nil, err
}
defer playlist.Close()
playlist_stat, _ := playlist.Stat()
if playlist_stat.Size() == 0 {
return nil, nil
}
if playlist_stat.Size() < bufferSize {
buf = make([]byte, playlist_stat.Size())
} else {
buf = make([]byte, bufferSize)
offset = playlist_stat.Size() - bufferSize
}
_, err = playlist.ReadAt(buf, offset)
if err != nil && err != io.EOF {
return nil, err
}
lines := bytes.Split(buf, []byte("\n"))
if len(lines) < 2 {
return nil, nil
}
lines = lines[:len(lines)-1]
if len(lines) > n {
lines = lines[len(lines)-n:]
}
songs := make([]Song, 0, len(lines))
for _, line := range lines {
fields := bytes.Split(line, []byte("|"))
tim, _ := time.Parse(IcecastPlaylistDateFormat, string(fields[0]))
songs = append(songs, Song{
Time: tim.Format(SongTimeFormat),
Listeners: string(fields[2]),
Song: string(fields[3])})
}
return songs, nil
}
type PlaylistLogWatcher struct {
watcher *watcher.InotifyWatcher
changed chan uint32
}
func NewPlaylistLogWatcher() *PlaylistLogWatcher {
return &PlaylistLogWatcher{changed: make(chan uint32)}
}
func (pw *PlaylistLogWatcher) Watch(playlistPath string, n int) (err error) {
if pw.watcher != nil {
pw.watcher.Close()
}
pw.watcher, err = watcher.NewInotifyWatcher()
if err != nil {
return errors.Wrap(err, "cannot instantiate inotify watcher")
}
err = pw.watcher.AddWatch(playlistPath, watcher.ModIgnMask)
if err != nil {
return errors.Wrap(err, "cannot set a playlist to watch")
}
pw.watcher.WatchForMask(pw.changed, watcher.ModIgnMask)
if lastPlayedCache == nil {
lastPlayedCache = make([]Song, 0, n)
songs, err := icecastLastPlayedSongs(playlistPath, n)
if err == nil && len(songs) > 0 {
lastPlayedCache = append(lastPlayedCache, songs...)
} else if err != nil {
log.Fatalln("failed to retrieve last songs:", err)
}
}
go func() {
for {
mask := <-pw.changed
if mask&syscall.IN_MODIFY > 0 {
lastPlayedCacheMutex.Lock()
songs, err := icecastLastPlayedSongs(playlistPath, 1)
if err == nil && len(songs) > 0 {
CheckAndUpdateMostListenedSong(songs[0], currentlyPlaying)
if currentlyPlaying.Time == "" {
currentlyPlaying = songs[0]
} else {
currentlyPlaying.Listeners = songs[0].Listeners
if len(lastPlayedCache) == n {
lastPlayedCache = append(lastPlayedCache[1:], currentlyPlaying)
} else {
lastPlayedCache = append(lastPlayedCache, currentlyPlaying)
}
currentlyPlaying = songs[0]
}
} else if err != nil {
log.Println("failed to retrieve last songs:", err)
}
lastPlayedCacheMutex.Unlock()
} else if mask&syscall.IN_IGNORED > 0 {
pw.Close()
pw.Watch(playlistPath, n)
return
}
}
}()
return nil
}
func (pw *PlaylistLogWatcher) Close() {
pw.watcher.Close()
}