1
0

Added a fallback song option. Altered a PlaylistNext() header to use it.

This commit is contained in:
Alexander Andreev 2023-10-08 02:52:37 +04:00
parent e0e5e314f1
commit f0aa00b932
Signed by: Arav
GPG Key ID: D22A817D95815393
2 changed files with 38 additions and 29 deletions

View File

@ -20,6 +20,7 @@ var (
listenAddress = flag.String("listen", "/var/run/dwelling-radio/sock", "listen address (ip:port|unix_path)")
filelistPath = flag.String("filelist", "/mnt/data/appdata/radio/filelist.html", "path to a filelist.html file")
playlist = flag.String("playlist", "", "path to a playlist")
fallbackSong = flag.String("fallback-song", "", "path to a fallbacl song")
mostListenedSongPath = flag.String("mls-file", "/mnt/data/appdata/radio/mostlistenedsong", "path to a file that stores info about the most listened song")
songListLen = flag.Int("list-length", 10, "number of songs to show in last N songs table")
@ -65,7 +66,7 @@ func main() {
r.ServeStatic("/assets/*filepath", web.Assets())
djh := ihttp.NewDJHandlers(lstnrs, plylst, songList, &mostListenedSong)
djh := ihttp.NewDJHandlers(lstnrs, plylst, songList, &mostListenedSong, *fallbackSong)
s := r.Sub("/api/listener")
s.Handler(http.MethodGet, "/", djh.ListenersGet)

View File

@ -11,14 +11,17 @@ import (
)
type DJHandlers struct {
listeners *radio.ListenerCounter
playlist *radio.Playlist
songList *radio.SongList
mostLSong *radio.MostListenedSong
listeners *radio.ListenerCounter
playlist *radio.Playlist
songList *radio.SongList
mostLSong *radio.MostListenedSong
fallbackSong string
}
func NewDJHandlers(l *radio.ListenerCounter, p *radio.Playlist, sl *radio.SongList, mls *radio.MostListenedSong) *DJHandlers {
return &DJHandlers{listeners: l, playlist: p, songList: sl, mostLSong: mls}
func NewDJHandlers(l *radio.ListenerCounter, p *radio.Playlist,
sl *radio.SongList, mls *radio.MostListenedSong, fS string) *DJHandlers {
return &DJHandlers{listeners: l, playlist: p, songList: sl,
mostLSong: mls, fallbackSong: fS}
}
func (dj *DJHandlers) ListenersGet(w http.ResponseWriter, _ *http.Request) {
@ -51,29 +54,34 @@ func (dj *DJHandlers) PlaylistNext(w http.ResponseWriter, _ *http.Request) {
nxt := dj.playlist.Next()
if nxt == "" {
log.Println("the end of a playlist has been reached")
} else {
go func() {
oggf, err := oggtag.NewOggFile(nxt)
if err != nil {
log.Println("cannot read an OGG file", nxt, ":", err)
http.Error(w, "cannot read an OGG file", http.StatusInternalServerError)
return
}
song := radio.Song{
Artist: oggf.GetTag("artist"),
Title: oggf.GetTag("title"),
Duration: oggf.GetDuration(),
MaxListeners: dj.listeners.Current(),
StartAt: time.Now()}
if dj.songList.Current() != nil {
dj.mostLSong.Update(*dj.songList.Current())
}
dj.songList.Add(song)
}()
if nxt = dj.fallbackSong; nxt == "" {
log.Println("a fallback song is not set")
http.Error(w, "a playlist is empty and a fallback song is not set", http.StatusNotFound)
return
}
}
go func() {
oggf, err := oggtag.NewOggFile(nxt)
if err != nil {
log.Println("cannot read an OGG file", nxt, ":", err)
http.Error(w, "cannot read an OGG file", http.StatusInternalServerError)
return
}
song := radio.Song{
Artist: oggf.GetTag("artist"),
Title: oggf.GetTag("title"),
Duration: oggf.GetDuration(),
MaxListeners: dj.listeners.Current(),
StartAt: time.Now()}
if dj.songList.Current() != nil {
dj.mostLSong.Update(*dj.songList.Current())
}
dj.songList.Add(song)
}()
fmt.Fprintln(w, nxt)
}