2023-03-13 00:03:59 +04:00
|
|
|
package radio
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-10-02 02:07:39 +04:00
|
|
|
"encoding/json"
|
2023-03-13 00:03:59 +04:00
|
|
|
"errors"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-03-13 02:12:21 +04:00
|
|
|
const MostListenedDateFormat = "02 January 2006"
|
|
|
|
|
2023-09-10 18:23:06 +04:00
|
|
|
var mlsChanged = false
|
|
|
|
|
2023-03-13 00:03:59 +04:00
|
|
|
type MostListenedSong struct {
|
|
|
|
Listeners int
|
|
|
|
Date time.Time
|
|
|
|
Song string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mls *MostListenedSong) DateString() string {
|
2023-03-13 02:12:21 +04:00
|
|
|
return mls.Date.Format(MostListenedDateFormat)
|
2023-03-13 00:03:59 +04:00
|
|
|
}
|
|
|
|
|
2023-10-02 02:07:39 +04:00
|
|
|
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)})
|
|
|
|
}
|
|
|
|
|
2023-03-13 00:03:59 +04:00
|
|
|
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.
|
2023-10-05 18:19:41 +04:00
|
|
|
func CheckAndUpdateMostListenedSong(cur Song) {
|
|
|
|
if cur.Artist == "" {
|
2023-03-13 01:42:24 +04:00
|
|
|
return
|
|
|
|
}
|
2023-10-02 03:19:23 +04:00
|
|
|
if cur.MaxListeners > mostListened.Listeners {
|
2023-03-13 00:03:59 +04:00
|
|
|
mostListened = MostListenedSong{
|
2023-10-02 03:19:23 +04:00
|
|
|
Listeners: cur.MaxListeners,
|
2023-10-02 15:04:51 +04:00
|
|
|
Date: cur.StartAt,
|
|
|
|
Song: cur.ArtistTitle()}
|
2023-03-13 00:03:59 +04:00
|
|
|
}
|
2023-09-10 18:23:06 +04:00
|
|
|
mlsChanged = true
|
2023-03-13 00:03:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// MostListened returns song that currently is the song with most simultaneous
|
|
|
|
// listeners.
|
|
|
|
func MostListened() *MostListenedSong {
|
2023-10-02 02:07:08 +04:00
|
|
|
if mostListened.Date.Year() == 1 {
|
|
|
|
return nil
|
|
|
|
}
|
2023-03-13 00:03:59 +04:00
|
|
|
return &mostListened
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadMostListenedSong(data []byte) (err error) {
|
|
|
|
lines := bytes.Split(data, []byte{'\n'})
|
|
|
|
if len(lines) != 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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if mostListened.Listeners, err = strconv.Atoi(string(lines[1])); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mostListened.Song = string(lines[2])
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func StoreMostListenedSong() []byte {
|
2023-09-10 18:23:06 +04:00
|
|
|
if !mlsChanged {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-13 00:03:59 +04:00
|
|
|
buf := make([]byte, 0, 30+len(mostListened.Song))
|
|
|
|
b := bytes.NewBuffer(buf)
|
|
|
|
|
|
|
|
b.WriteString(mostListened.Date.Format(time.RFC3339))
|
|
|
|
b.WriteByte('\n')
|
|
|
|
b.WriteString(strconv.Itoa(mostListened.Listeners))
|
|
|
|
b.WriteByte('\n')
|
|
|
|
b.WriteString(mostListened.Song)
|
|
|
|
|
|
|
|
return b.Bytes()
|
|
|
|
}
|