1
0

Updated logic in DJ handlers. Also let's give a try to not run this updating code in a PlaylistNext() handler in a separate thread. Also funnier handling of audio tag missing.

This commit is contained in:
Alexander Andreev 2024-05-11 04:36:23 +04:00
parent 454df3bb74
commit cf90bba297
Signed by: Arav
GPG Key ID: 25969B23DCB5CA34

View File

@ -42,11 +42,7 @@ func (dj *DJHandlers) ListenersUpdate(w http.ResponseWriter, r *http.Request) {
switch r.FormValue("action") { switch r.FormValue("action") {
case "listener_add": case "listener_add":
l := dj.listeners.Inc() _ = dj.listeners.Inc()
go func() {
dj.curSong.UpdateMaxListeners(l)
dj.curSong.IncListeners()
}()
case "listener_remove": case "listener_remove":
if _, err := dj.listeners.Dec(); err != nil { if _, err := dj.listeners.Dec(); err != nil {
log.Println("DJHandlers.ListenersUpdate:", err) log.Println("DJHandlers.ListenersUpdate:", err)
@ -63,6 +59,10 @@ func (dj *DJHandlers) ListenersUpdate(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
const defaultArtistTag = "[LOL, no artist tag]"
const defaultTitleTag = "[No title tag for you -_-]"
const defaultTitleTagNoArtist = "[And no title tag either! Pffft]"
func (dj *DJHandlers) PlaylistNext(w http.ResponseWriter, _ *http.Request) { func (dj *DJHandlers) PlaylistNext(w http.ResponseWriter, _ *http.Request) {
w.Header().Add("Content-Type", "text/plain") w.Header().Add("Content-Type", "text/plain")
nxt := dj.playlist.Next() nxt := dj.playlist.Next()
@ -75,10 +75,11 @@ func (dj *DJHandlers) PlaylistNext(w http.ResponseWriter, _ *http.Request) {
} }
} }
go func() { // go func() {
oggf, err := oggtag.NewOggFile(nxt) oggf, err := oggtag.NewOggFile(nxt)
if err != nil { if err != nil {
log.Println("cannot read an OGG file", nxt, ":", err) log.Println("cannot read an OGG file", nxt, ":", err)
http.Error(w, "cannot read an OGG file", http.StatusInternalServerError)
return return
} }
@ -86,29 +87,39 @@ func (dj *DJHandlers) PlaylistNext(w http.ResponseWriter, _ *http.Request) {
Artist: oggf.GetTag("artist"), Artist: oggf.GetTag("artist"),
Title: oggf.GetTag("title"), Title: oggf.GetTag("title"),
Duration: oggf.GetDuration(), Duration: oggf.GetDuration(),
Listeners: dj.listeners.Current(),
MaxListeners: dj.listeners.Current(),
// Here 5 seconds are being added because it is approximately the // Here 5 seconds are being added because it is approximately the
// time between the creation of this Song object and when ezstream // time between the creation of this Song object and when ezstream
// actually starts to play it. // actually starts to play it.
StartAt: time.Now().Add(5 * time.Second)} StartAt: time.Now().Add(5 * time.Second)}
if newSong.Artist == "" || newSong.Title == "" { if newSong.Artist == "" && newSong.Title == "" {
log.Println("Playlist:", nxt, "has no artist or title tags.") log.Println("Playlist:", nxt, "has no artist and title tags.")
newSong.Artist = defaultArtistTag
newSong.Title = defaultTitleTagNoArtist
} else if newSong.Artist == "" {
log.Println("Playlist:", nxt, "has no artist tag.")
newSong.Artist = defaultArtistTag
} else if newSong.Title == "" {
log.Println("Playlist:", nxt, "has no title tag.")
newSong.Title = defaultTitleTag
} }
if strings.HasSuffix(nxt, "/fallback.ogg") { if strings.HasSuffix(nxt, "/fallback.ogg") {
newSong.Artist = "Nothing to play. Playing a fallback: " + newSong.Artist newSong.Artist = "Nothing to play. Playing a fallback: " + newSong.Artist
} }
dj.curSong.Listeners, dj.curSong.MaxListeners = dj.listeners.Reset()
if dj.curSong.Artist != "" { if dj.curSong.Artist != "" {
if err := dj.stats.Add(dj.curSong); err != nil { if err := dj.stats.Add(dj.curSong); err != nil {
log.Println("cannot add a song to a stats DB:", err) log.Println("cannot add a song to a stats DB:", err)
} }
} }
dj.curSong.SetFrom(&newSong) *dj.curSong = newSong
}() dj.curSong.Listeners = 0
dj.curSong.MaxListeners = 0
// }()
fmt.Fprintln(w, nxt) fmt.Fprintln(w, nxt)
} }