1
0
dwelling-radio/internal/http/dj_handlers.go

124 lines
3.6 KiB
Go
Raw Normal View History

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"
"fmt"
"log"
"net/http"
2023-10-01 05:43:52 +04:00
"time"
)
type DJHandlers struct {
listeners *radio.ListenerCounter
2023-10-01 01:32:57 +04:00
playlist *radio.Playlist
songList *radio.SongList
}
func NewDJHandlers(l *radio.ListenerCounter, p *radio.Playlist, sl *radio.SongList, slLen int) *DJHandlers {
return &DJHandlers{listeners: l, playlist: p, songList: sl}
}
func (dj *DJHandlers) ListenersGet(w http.ResponseWriter, _ *http.Request) {
w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(dj.listeners)
}
func (dj *DJHandlers) ListenersInc(w http.ResponseWriter, _ *http.Request) {
l := dj.listeners.Inc()
go dj.songList.UpdateCurrentMaxListeners(l)
w.WriteHeader(http.StatusCreated)
w.Header().Add("Content-Type", "text/plain")
fmt.Fprint(w, l)
}
func (dj *DJHandlers) ListenersDec(w http.ResponseWriter, _ *http.Request) {
l, err := dj.listeners.Dec()
if err != nil {
log.Print(err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
w.Header().Add("Content-Type", "text/plain")
fmt.Fprint(w, l)
}
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-01 05:43:52 +04:00
} else {
go func() {
oggf, err := oggtag.NewOggFile(nxt)
2023-10-01 05:43:52 +04:00
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-01 05:43:52 +04:00
}
song := radio.Song{
Artist: oggf.GetTag("artist"),
Title: oggf.GetTag("title"),
Duration: oggf.GetDuration(),
2023-10-01 05:43:52 +04:00
MaxListeners: dj.listeners.Current(),
StartAt: time.Now()}
radio.CheckAndUpdateMostListenedSong(dj.songList.Current())
dj.songList.Add(song)
2023-10-01 05:43:52 +04:00
}()
2023-10-01 01:32:57 +04:00
}
fmt.Fprintln(w, nxt)
2023-10-01 01:32:57 +04:00
}
2023-10-01 03:35:33 +04:00
func (dj *DJHandlers) Song(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(dj.songList.Current())
if err != nil {
log.Println("DJHandlers.Song:", err)
http.Error(w, "cannot obtain current song", http.StatusInternalServerError)
}
}
func (dj *DJHandlers) Songs(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(dj.songList.List())
if err != nil {
log.Println("DJHandlers.Songs:", err)
http.Error(w, "cannot obtain list of last songs", http.StatusInternalServerError)
}
}
func (dj *DJHandlers) Status(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(&struct {
Current radio.Song `json:"current_song,omitempty"`
Listeners *radio.ListenerCounter `json:"listeners"`
List []radio.Song `json:"last_songs,omitempty"`
Mls *radio.MostListenedSong `json:"most_listened_song,omitempty"`
}{
Current: dj.songList.Current(),
Listeners: dj.listeners,
List: dj.songList.List(),
Mls: radio.MostListened()})
if err != nil {
log.Println("DJHandlers.Status:", err)
http.Error(w, "status parsing failed", http.StatusInternalServerError)
}
}
2023-10-02 02:28:46 +04:00
func (dj *DJHandlers) MostListenedSong(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
mls := radio.MostListened()
if mls == nil {
w.WriteHeader(http.StatusNotFound)
return
}
err := json.NewEncoder(w).Encode(mls)
if err != nil {
log.Println("DJHandlers.MostListenedSong:", err)
http.Error(w, "MostListenedSong parsing failed", http.StatusInternalServerError)
2023-10-01 03:35:33 +04:00
}
}