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-01 05:43:52 +04:00
|
|
|
"time"
|
2023-09-30 23:59:48 +04:00
|
|
|
)
|
|
|
|
|
2023-10-01 05:43:52 +04:00
|
|
|
var songList radio.SongList
|
|
|
|
|
2023-09-30 23:59:48 +04:00
|
|
|
type DJHandlers struct {
|
2023-10-01 20:32:07 +04:00
|
|
|
listeners *radio.ListenerCounter
|
2023-10-01 01:32:57 +04:00
|
|
|
playlist *radio.Playlist
|
2023-09-30 23:59:48 +04:00
|
|
|
}
|
|
|
|
|
2023-10-01 20:32:07 +04:00
|
|
|
func NewDJHandlers(l *radio.ListenerCounter, p *radio.Playlist, slLen int) *DJHandlers {
|
2023-10-01 05:43:52 +04:00
|
|
|
songList = *radio.NewSongList(slLen)
|
2023-10-01 01:32:57 +04:00
|
|
|
return &DJHandlers{listeners: l, playlist: p}
|
2023-09-30 23:59:48 +04:00
|
|
|
}
|
|
|
|
|
2023-10-01 01:32:17 +04:00
|
|
|
func (dj *DJHandlers) ListenersGet(w http.ResponseWriter, _ *http.Request) {
|
2023-09-30 23:59:48 +04:00
|
|
|
w.Header().Add("Content-Type", "text/plain")
|
|
|
|
fmt.Fprint(w, dj.listeners.Current(), dj.listeners.Peak())
|
|
|
|
}
|
|
|
|
|
2023-10-01 01:32:17 +04:00
|
|
|
func (dj *DJHandlers) ListenersInc(w http.ResponseWriter, _ *http.Request) {
|
2023-09-30 23:59:48 +04:00
|
|
|
l := dj.listeners.Inc()
|
2023-10-01 05:43:52 +04:00
|
|
|
go func() {
|
|
|
|
if l > songList.Current().MaxListeners {
|
|
|
|
songList.Current().MaxListeners = l
|
|
|
|
}
|
|
|
|
}()
|
2023-09-30 23:59:48 +04:00
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
w.Header().Add("Content-Type", "text/plain")
|
|
|
|
fmt.Fprint(w, l)
|
|
|
|
}
|
|
|
|
|
2023-10-01 01:32:17 +04:00
|
|
|
func (dj *DJHandlers) ListenersDec(w http.ResponseWriter, _ *http.Request) {
|
2023-09-30 23:59:48 +04:00
|
|
|
l, err := dj.listeners.Dec()
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
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() {
|
2023-10-02 01:27:14 +04:00
|
|
|
bs, be, err := oggtag.ReadFile(nxt)
|
2023-10-01 05:43:52 +04:00
|
|
|
if err != nil {
|
2023-10-02 01:27:14 +04:00
|
|
|
log.Println("cannot read an OGG file", nxt, ":", err)
|
2023-10-01 05:43:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
song := radio.Song_{
|
2023-10-02 01:27:14 +04:00
|
|
|
Artist: oggtag.GetTag(bs, "artist"),
|
|
|
|
Title: oggtag.GetTag(bs, "title"),
|
|
|
|
Duration: oggtag.GetDuration(bs, be),
|
2023-10-01 05:43:52 +04:00
|
|
|
MaxListeners: dj.listeners.Current(),
|
|
|
|
StartAt: time.Now()}
|
|
|
|
// radio.CheckAndUpdateMostListenedSong(song, currentSong)
|
|
|
|
songList.Add(song)
|
|
|
|
}()
|
2023-10-01 01:32:57 +04:00
|
|
|
}
|
|
|
|
fmt.Fprint(w, nxt)
|
|
|
|
}
|
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")
|
2023-10-01 22:05:39 +04:00
|
|
|
err := json.NewEncoder(w).Encode(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(songList.List())
|
|
|
|
if err != nil {
|
|
|
|
log.Println("DJHandlers.Songs:", err)
|
|
|
|
http.Error(w, "cannot obtain list of last songs", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
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: songList.Current(),
|
|
|
|
Listeners: dj.listeners,
|
|
|
|
List: songList.List(),
|
|
|
|
Mls: radio.MostListened()})
|
|
|
|
if err != nil {
|
|
|
|
log.Println("DJHandlers.Status:", err)
|
|
|
|
http.Error(w, "status parsing failed", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
2023-10-01 03:35:33 +04:00
|
|
|
}
|
|
|
|
}
|