MostListenedSong was rewritten.
This commit is contained in:
parent
d84d985962
commit
0d8032da46
@ -37,8 +37,9 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var mostListenedSong radio.MostListenedSong
|
||||||
if data, err := os.ReadFile(*mostListenedSongPath); err == nil {
|
if data, err := os.ReadFile(*mostListenedSongPath); err == nil {
|
||||||
if err := radio.LoadMostListenedSong(data); err != nil {
|
if err := mostListenedSong.Load(data); err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -50,7 +51,7 @@ func main() {
|
|||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
hand := ihttp.NewHandlers(*filelistPath, songList, lstnrs)
|
hand := ihttp.NewHandlers(*filelistPath, songList, lstnrs, &mostListenedSong)
|
||||||
r := httpr.New()
|
r := httpr.New()
|
||||||
|
|
||||||
r.Handler(http.MethodGet, "/", hand.Index)
|
r.Handler(http.MethodGet, "/", hand.Index)
|
||||||
@ -64,7 +65,7 @@ func main() {
|
|||||||
|
|
||||||
r.ServeStatic("/assets/*filepath", web.Assets())
|
r.ServeStatic("/assets/*filepath", web.Assets())
|
||||||
|
|
||||||
djh := ihttp.NewDJHandlers(lstnrs, plylst, songList, *songListLen)
|
djh := ihttp.NewDJHandlers(lstnrs, plylst, songList, &mostListenedSong)
|
||||||
|
|
||||||
s := r.Sub("/api/listener")
|
s := r.Sub("/api/listener")
|
||||||
s.Handler(http.MethodGet, "/", djh.ListenersGet)
|
s.Handler(http.MethodGet, "/", djh.ListenersGet)
|
||||||
@ -86,7 +87,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
fileData := radio.StoreMostListenedSong()
|
fileData := mostListenedSong.Store()
|
||||||
if fileData != nil {
|
if fileData != nil {
|
||||||
err := os.WriteFile(*mostListenedSongPath, fileData, fs.ModePerm)
|
err := os.WriteFile(*mostListenedSongPath, fileData, fs.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -14,10 +14,11 @@ type DJHandlers struct {
|
|||||||
listeners *radio.ListenerCounter
|
listeners *radio.ListenerCounter
|
||||||
playlist *radio.Playlist
|
playlist *radio.Playlist
|
||||||
songList *radio.SongList
|
songList *radio.SongList
|
||||||
|
mostLSong *radio.MostListenedSong
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDJHandlers(l *radio.ListenerCounter, p *radio.Playlist, sl *radio.SongList, slLen int) *DJHandlers {
|
func NewDJHandlers(l *radio.ListenerCounter, p *radio.Playlist, sl *radio.SongList, mls *radio.MostListenedSong) *DJHandlers {
|
||||||
return &DJHandlers{listeners: l, playlist: p, songList: sl}
|
return &DJHandlers{listeners: l, playlist: p, songList: sl, mostLSong: mls}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dj *DJHandlers) ListenersGet(w http.ResponseWriter, _ *http.Request) {
|
func (dj *DJHandlers) ListenersGet(w http.ResponseWriter, _ *http.Request) {
|
||||||
@ -65,8 +66,12 @@ func (dj *DJHandlers) PlaylistNext(w http.ResponseWriter, _ *http.Request) {
|
|||||||
Duration: oggf.GetDuration(),
|
Duration: oggf.GetDuration(),
|
||||||
MaxListeners: dj.listeners.Current(),
|
MaxListeners: dj.listeners.Current(),
|
||||||
StartAt: time.Now()}
|
StartAt: time.Now()}
|
||||||
|
|
||||||
|
if dj.songList.Current() != nil {
|
||||||
|
dj.mostLSong.Update(*dj.songList.Current())
|
||||||
|
}
|
||||||
|
|
||||||
dj.songList.Add(song)
|
dj.songList.Add(song)
|
||||||
radio.CheckAndUpdateMostListenedSong(*dj.songList.Current())
|
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
fmt.Fprintln(w, nxt)
|
fmt.Fprintln(w, nxt)
|
||||||
@ -106,7 +111,7 @@ func (dj *DJHandlers) Status(w http.ResponseWriter, r *http.Request) {
|
|||||||
Current: curSong,
|
Current: curSong,
|
||||||
Listeners: dj.listeners,
|
Listeners: dj.listeners,
|
||||||
List: dj.songList.List(),
|
List: dj.songList.List(),
|
||||||
Mls: radio.MostListened()})
|
Mls: dj.mostLSong.Get()})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("DJHandlers.Status:", err)
|
log.Println("DJHandlers.Status:", err)
|
||||||
http.Error(w, "status parsing failed", http.StatusInternalServerError)
|
http.Error(w, "status parsing failed", http.StatusInternalServerError)
|
||||||
@ -115,7 +120,7 @@ func (dj *DJHandlers) Status(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
func (dj *DJHandlers) MostListenedSong(w http.ResponseWriter, r *http.Request) {
|
func (dj *DJHandlers) MostListenedSong(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Add("Content-Type", "application/json")
|
w.Header().Add("Content-Type", "application/json")
|
||||||
mls := radio.MostListened()
|
mls := dj.mostLSong.Get()
|
||||||
if mls == nil {
|
if mls == nil {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
return
|
return
|
||||||
|
@ -11,18 +11,20 @@ import (
|
|||||||
type Handlers struct {
|
type Handlers struct {
|
||||||
songList *radio.SongList
|
songList *radio.SongList
|
||||||
listeners *radio.ListenerCounter
|
listeners *radio.ListenerCounter
|
||||||
|
mostLSong *radio.MostListenedSong
|
||||||
filelistPath string
|
filelistPath string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHandlers(filelistPath string, songList *radio.SongList, listeners *radio.ListenerCounter) *Handlers {
|
func NewHandlers(filelistPath string, songList *radio.SongList, listeners *radio.ListenerCounter, mls *radio.MostListenedSong) *Handlers {
|
||||||
return &Handlers{
|
return &Handlers{
|
||||||
songList: songList,
|
songList: songList,
|
||||||
filelistPath: filelistPath,
|
filelistPath: filelistPath,
|
||||||
listeners: listeners}
|
listeners: listeners,
|
||||||
|
mostLSong: mls}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handlers) Index(w http.ResponseWriter, r *http.Request) {
|
func (h *Handlers) Index(w http.ResponseWriter, r *http.Request) {
|
||||||
web.Index(utils.MainSite(r.Host), h.songList, h.listeners, r, w)
|
web.Index(utils.MainSite(r.Host), h.songList, h.listeners, h.mostLSong.Get(), r, w)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handlers) Playlist(w http.ResponseWriter, _ *http.Request) {
|
func (h *Handlers) Playlist(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
@ -2,101 +2,96 @@ package radio
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
const MostListenedDateFormat = "02 January 2006"
|
const MostListenedDateFormat string = "02 January 2006"
|
||||||
|
|
||||||
var mlsChanged = false
|
|
||||||
|
|
||||||
|
// MostListenedSong holds a metadata for a most listened song.
|
||||||
type MostListenedSong struct {
|
type MostListenedSong struct {
|
||||||
Listeners int
|
sync.RWMutex
|
||||||
Date time.Time
|
Date time.Time `json:"date"`
|
||||||
Song string
|
Listeners int `json:"listeners"`
|
||||||
|
Song string `json:"song"`
|
||||||
|
changed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mls *MostListenedSong) DateString() string {
|
func (mls *MostListenedSong) Update(song Song) {
|
||||||
return mls.Date.Format(MostListenedDateFormat)
|
mls.Lock()
|
||||||
}
|
defer mls.Unlock()
|
||||||
|
if song.Artist == "" {
|
||||||
func (mls *MostListenedSong) MarshalJSON() ([]byte, error) {
|
|
||||||
return json.Marshal(&struct {
|
|
||||||
Song string `json:"song"`
|
|
||||||
Listeners int `json:"listeners"`
|
|
||||||
Date string `json:"date"`
|
|
||||||
}{
|
|
||||||
Song: mls.Song,
|
|
||||||
Listeners: mls.Listeners,
|
|
||||||
Date: mls.Date.UTC().Format(time.RFC3339)})
|
|
||||||
}
|
|
||||||
|
|
||||||
var mostListened MostListenedSong
|
|
||||||
|
|
||||||
// CheckAndUpdateMostListenedSong compares current most played song with
|
|
||||||
// provided `cur`rent song's listeners, and if it is larger, then it takes
|
|
||||||
// `prev`ious song's name.
|
|
||||||
//
|
|
||||||
// Why we take a previous song's name? Experimentally I noticed that Icecast
|
|
||||||
// writes amount of listeners that was by the very start of a next song. So
|
|
||||||
// it means that it was actually amount of listeners by the end of
|
|
||||||
// the previous song.
|
|
||||||
//
|
|
||||||
// So it would be fairer to give these listeners back to a song they was
|
|
||||||
// listening to.
|
|
||||||
func CheckAndUpdateMostListenedSong(cur Song) {
|
|
||||||
if cur.Artist == "" {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if cur.MaxListeners > mostListened.Listeners {
|
if song.MaxListeners > mls.Listeners {
|
||||||
mostListened = MostListenedSong{
|
mls.Listeners = song.MaxListeners
|
||||||
Listeners: cur.MaxListeners,
|
mls.Date = song.StartAt
|
||||||
Date: cur.StartAt,
|
mls.Song = song.Artist + " - " + song.Title
|
||||||
Song: cur.Artist + " - " + cur.Title}
|
mls.changed = true
|
||||||
}
|
}
|
||||||
mlsChanged = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MostListened returns song that currently is the song with most simultaneous
|
func (mls *MostListenedSong) Get() *MostListenedSong {
|
||||||
// listeners.
|
mls.RLock()
|
||||||
func MostListened() *MostListenedSong {
|
defer mls.RUnlock()
|
||||||
if mostListened.Date.Year() == 1 {
|
|
||||||
|
if mls.Date.Year() == 1 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return &mostListened
|
|
||||||
|
return &MostListenedSong{
|
||||||
|
Date: mls.Date,
|
||||||
|
Listeners: mls.Listeners,
|
||||||
|
Song: mls.Song}
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadMostListenedSong(data []byte) (err error) {
|
// Load parses given data and fill a MostListenedSong.
|
||||||
|
func (mls *MostListenedSong) Load(data []byte) (err error) {
|
||||||
|
mls.Lock()
|
||||||
|
defer mls.Unlock()
|
||||||
|
|
||||||
lines := bytes.Split(data, []byte{'\n'})
|
lines := bytes.Split(data, []byte{'\n'})
|
||||||
if len(lines) != 3 {
|
if len(lines) != 3 {
|
||||||
return errors.New("lines count mismatch, should be 3")
|
return errors.New("lines count mismatch, should be 3")
|
||||||
}
|
}
|
||||||
mostListened = MostListenedSong{}
|
|
||||||
if mostListened.Date, err = time.Parse(time.RFC3339, string(lines[0])); err != nil {
|
var date time.Time
|
||||||
return err
|
if date, err = time.Parse(time.RFC3339, string(lines[0])); err != nil {
|
||||||
|
return errors.Wrap(err, "wrong date/time format")
|
||||||
}
|
}
|
||||||
if mostListened.Listeners, err = strconv.Atoi(string(lines[1])); err != nil {
|
|
||||||
return err
|
var listeners int
|
||||||
|
if listeners, err = strconv.Atoi(string(lines[1])); err != nil {
|
||||||
|
return errors.Wrap(err, "a listeners number failed to parse")
|
||||||
}
|
}
|
||||||
mostListened.Song = string(lines[2])
|
|
||||||
|
if len(lines[2]) == 0 {
|
||||||
|
return errors.New("a song is empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
mls.Date = date
|
||||||
|
mls.Listeners = listeners
|
||||||
|
mls.Song = string(lines[2])
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func StoreMostListenedSong() []byte {
|
// Store returns a byte slice of a marshalled to text MostListenedSong.
|
||||||
if !mlsChanged {
|
func (mls *MostListenedSong) Store() []byte {
|
||||||
|
if !mls.changed {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := make([]byte, 0, 30+len(mostListened.Song))
|
buf := make([]byte, 0, 30+len(mls.Song))
|
||||||
b := bytes.NewBuffer(buf)
|
b := bytes.NewBuffer(buf)
|
||||||
|
|
||||||
b.WriteString(mostListened.Date.Format(time.RFC3339))
|
b.WriteString(mls.Date.Format(time.RFC3339))
|
||||||
b.WriteByte('\n')
|
b.WriteByte('\n')
|
||||||
b.WriteString(strconv.Itoa(mostListened.Listeners))
|
b.WriteString(strconv.Itoa(mls.Listeners))
|
||||||
b.WriteByte('\n')
|
b.WriteByte('\n')
|
||||||
b.WriteString(mostListened.Song)
|
b.WriteString(mls.Song)
|
||||||
|
|
||||||
return b.Bytes()
|
return b.Bytes()
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
:go:func Index(mainSite string, songList *radio.SongList, listeners *radio.ListenerCounter, r *http.Request)
|
:go:func Index(mainSite string, songList *radio.SongList, listeners *radio.ListenerCounter, mls *radio.MostListenedSong, r *http.Request)
|
||||||
|
|
||||||
:go:import "dwelling-radio/internal/radio"
|
:go:import "dwelling-radio/internal/radio"
|
||||||
:go:import "dwelling-radio/pkg/utils"
|
:go:import "dwelling-radio/pkg/utils"
|
||||||
@ -65,8 +65,7 @@ html(lang='en')
|
|||||||
else
|
else
|
||||||
td
|
td
|
||||||
td #{song.Artist} - #{song.Title}
|
td #{song.Artist} - #{song.Title}
|
||||||
- ml := radio.MostListened()
|
if mls != nil
|
||||||
if ml.Song != ""
|
p.right Most listened song was "#{mls.Song}" on #{utils.ToClientTimezone(mls.Date, r).Format(radio.MostListenedDateFormat)} with #[b #{mls.Listeners}] listeners.
|
||||||
p.right Most listened song was "#{ml.Song}" on #{utils.ToClientTimezone(ml.Date, r).Format(radio.MostListenedDateFormat)} with #[b #{ml.Listeners}] listeners.
|
|
||||||
footer
|
footer
|
||||||
| 2017—2023 Alexander "Arav" Andreev <#[a(href='mailto:me@arav.su') me@arav.su]> #[a(href=mainSite+'/privacy') Privacy statements]
|
| 2017—2023 Alexander "Arav" Andreev <#[a(href='mailto:me@arav.su') me@arav.su]> #[a(href=mainSite+'/privacy') Privacy statements]
|
||||||
|
Loading…
Reference in New Issue
Block a user