2023-09-30 23:59:48 +04:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"dwelling-radio/internal/radio"
|
2023-10-01 05:43:52 +04:00
|
|
|
"dwelling-radio/pkg/oggtag"
|
2023-10-01 03:35:33 +04:00
|
|
|
"encoding/json"
|
2023-09-30 23:59:48 +04:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2023-10-08 02:56:19 +04:00
|
|
|
"strings"
|
2023-10-01 05:43:52 +04:00
|
|
|
"time"
|
2023-09-30 23:59:48 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
type DJHandlers struct {
|
2023-10-08 02:52:37 +04:00
|
|
|
listeners *radio.ListenerCounter
|
|
|
|
playlist *radio.Playlist
|
|
|
|
songList *radio.SongList
|
|
|
|
mostLSong *radio.MostListenedSong
|
|
|
|
fallbackSong string
|
2023-09-30 23:59:48 +04:00
|
|
|
}
|
|
|
|
|
2023-10-08 02:52:37 +04:00
|
|
|
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}
|
2023-09-30 23:59:48 +04:00
|
|
|
}
|
|
|
|
|
2023-10-08 22:17:56 +04:00
|
|
|
func (dj *DJHandlers) ListenersUpdate(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
log.Println("DJHandlers.ListenersUpdate panic:", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
log.Println("DJHandlers.ListenersUpdate:", err)
|
|
|
|
http.Error(w, "cannot parse form", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r.FormValue("action") {
|
|
|
|
case "listener_add":
|
|
|
|
l := dj.listeners.Inc()
|
|
|
|
go dj.songList.UpdateCurrentMaxListeners(l)
|
|
|
|
case "listener_remove":
|
|
|
|
if _, err := dj.listeners.Dec(); err != nil {
|
|
|
|
log.Println("DJHandlers.ListenersUpdate:", err)
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
w.WriteHeader(http.StatusNotAcceptable)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "text/plain")
|
|
|
|
w.Header().Add("Icecast-Auth-User", "1")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2023-10-01 01:32:57 +04:00
|
|
|
func (dj *DJHandlers) PlaylistNext(w http.ResponseWriter, _ *http.Request) {
|
|
|
|
w.Header().Add("Content-Type", "text/plain")
|
|
|
|
nxt := dj.playlist.Next()
|
|
|
|
if nxt == "" {
|
|
|
|
log.Println("the end of a playlist has been reached")
|
2023-10-08 02:52:37 +04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2023-10-01 05:43:52 +04:00
|
|
|
|
2023-10-08 02:52:37 +04:00
|
|
|
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
|
|
|
|
}
|
2023-10-08 00:52:40 +04:00
|
|
|
|
2023-10-08 02:52:37 +04:00
|
|
|
song := radio.Song{
|
|
|
|
Artist: oggf.GetTag("artist"),
|
|
|
|
Title: oggf.GetTag("title"),
|
|
|
|
Duration: oggf.GetDuration(),
|
|
|
|
MaxListeners: dj.listeners.Current(),
|
|
|
|
StartAt: time.Now()}
|
2023-10-08 00:52:40 +04:00
|
|
|
|
2023-10-08 02:56:19 +04:00
|
|
|
if strings.HasSuffix(nxt, "/fallback.ogg") {
|
|
|
|
song.Artist = "Nothing to play. Playing a fallback: " + song.Artist
|
|
|
|
}
|
|
|
|
|
2023-10-08 02:52:37 +04:00
|
|
|
if dj.songList.Current() != nil {
|
|
|
|
dj.mostLSong.Update(*dj.songList.Current())
|
|
|
|
}
|
|
|
|
|
|
|
|
dj.songList.Add(song)
|
|
|
|
}()
|
2023-10-02 15:03:47 +04:00
|
|
|
fmt.Fprintln(w, nxt)
|
2023-10-01 01:32:57 +04:00
|
|
|
}
|
2023-10-01 03:35:33 +04:00
|
|
|
|
2023-10-01 22:07:04 +04:00
|
|
|
func (dj *DJHandlers) Status(w http.ResponseWriter, r *http.Request) {
|
2023-10-02 02:22:14 +04:00
|
|
|
w.Header().Add("Content-Type", "application/json")
|
2023-10-07 05:23:45 +04:00
|
|
|
var curSong *radio.Song = nil
|
|
|
|
if dj.songList.Current() != nil {
|
|
|
|
curSong = &radio.Song{}
|
|
|
|
*curSong = *dj.songList.Current()
|
|
|
|
}
|
2023-10-02 02:22:14 +04:00
|
|
|
err := json.NewEncoder(w).Encode(&struct {
|
2023-10-07 05:23:45 +04:00
|
|
|
Current *radio.Song `json:"current_song,omitempty"`
|
2023-10-02 02:22:14 +04:00
|
|
|
Listeners *radio.ListenerCounter `json:"listeners"`
|
2023-10-02 03:18:30 +04:00
|
|
|
List []radio.Song `json:"last_songs,omitempty"`
|
2023-10-02 02:22:14 +04:00
|
|
|
Mls *radio.MostListenedSong `json:"most_listened_song,omitempty"`
|
|
|
|
}{
|
2023-10-07 05:23:45 +04:00
|
|
|
Current: curSong,
|
2023-10-02 02:22:14 +04:00
|
|
|
Listeners: dj.listeners,
|
2023-10-02 03:18:54 +04:00
|
|
|
List: dj.songList.List(),
|
2023-10-08 00:52:40 +04:00
|
|
|
Mls: dj.mostLSong.Get()})
|
2023-10-02 02:22:14 +04:00
|
|
|
if err != nil {
|
|
|
|
log.Println("DJHandlers.Status:", err)
|
|
|
|
http.Error(w, "status parsing failed", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|