2023-03-13 00:03:59 +04:00
|
|
|
package radio
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"strconv"
|
2023-10-08 00:52:40 +04:00
|
|
|
"sync"
|
2023-03-13 00:03:59 +04:00
|
|
|
"time"
|
|
|
|
|
2023-10-08 00:52:40 +04:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
2023-03-13 02:12:21 +04:00
|
|
|
|
2023-10-08 00:52:40 +04:00
|
|
|
const MostListenedDateFormat string = "02 January 2006"
|
2023-09-10 18:23:06 +04:00
|
|
|
|
2023-10-08 00:52:40 +04:00
|
|
|
// MostListenedSong holds a metadata for a most listened song.
|
2023-03-13 00:03:59 +04:00
|
|
|
type MostListenedSong struct {
|
2023-10-08 00:52:40 +04:00
|
|
|
sync.RWMutex
|
|
|
|
Date time.Time `json:"date"`
|
|
|
|
Listeners int `json:"listeners"`
|
|
|
|
Song string `json:"song"`
|
|
|
|
changed bool
|
2023-03-13 00:03:59 +04:00
|
|
|
}
|
|
|
|
|
2023-10-08 00:52:40 +04:00
|
|
|
func (mls *MostListenedSong) Update(song Song) {
|
|
|
|
mls.Lock()
|
|
|
|
defer mls.Unlock()
|
|
|
|
if song.Artist == "" {
|
2023-03-13 01:42:24 +04:00
|
|
|
return
|
|
|
|
}
|
2023-10-08 00:52:40 +04:00
|
|
|
if song.MaxListeners > mls.Listeners {
|
|
|
|
mls.Listeners = song.MaxListeners
|
|
|
|
mls.Date = song.StartAt
|
|
|
|
mls.Song = song.Artist + " - " + song.Title
|
|
|
|
mls.changed = true
|
2023-03-13 00:03:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-08 00:52:40 +04:00
|
|
|
func (mls *MostListenedSong) Get() *MostListenedSong {
|
|
|
|
mls.RLock()
|
|
|
|
defer mls.RUnlock()
|
|
|
|
|
|
|
|
if mls.Date.Year() == 1 {
|
2023-10-02 02:07:08 +04:00
|
|
|
return nil
|
|
|
|
}
|
2023-10-08 00:52:40 +04:00
|
|
|
|
|
|
|
return &MostListenedSong{
|
|
|
|
Date: mls.Date,
|
|
|
|
Listeners: mls.Listeners,
|
|
|
|
Song: mls.Song}
|
2023-03-13 00:03:59 +04:00
|
|
|
}
|
|
|
|
|
2023-10-08 00:52:40 +04:00
|
|
|
// Load parses given data and fill a MostListenedSong.
|
|
|
|
func (mls *MostListenedSong) Load(data []byte) (err error) {
|
|
|
|
mls.Lock()
|
|
|
|
defer mls.Unlock()
|
|
|
|
|
2023-03-13 00:03:59 +04:00
|
|
|
lines := bytes.Split(data, []byte{'\n'})
|
|
|
|
if len(lines) != 3 {
|
|
|
|
return errors.New("lines count mismatch, should be 3")
|
|
|
|
}
|
2023-10-08 00:52:40 +04:00
|
|
|
|
|
|
|
var date time.Time
|
|
|
|
if date, err = time.Parse(time.RFC3339, string(lines[0])); err != nil {
|
|
|
|
return errors.Wrap(err, "wrong date/time format")
|
|
|
|
}
|
|
|
|
|
|
|
|
var listeners int
|
|
|
|
if listeners, err = strconv.Atoi(string(lines[1])); err != nil {
|
|
|
|
return errors.Wrap(err, "a listeners number failed to parse")
|
2023-03-13 00:03:59 +04:00
|
|
|
}
|
2023-10-08 00:52:40 +04:00
|
|
|
|
|
|
|
if len(lines[2]) == 0 {
|
|
|
|
return errors.New("a song is empty")
|
2023-03-13 00:03:59 +04:00
|
|
|
}
|
2023-10-08 00:52:40 +04:00
|
|
|
|
|
|
|
mls.Date = date
|
|
|
|
mls.Listeners = listeners
|
|
|
|
mls.Song = string(lines[2])
|
2023-03-13 00:03:59 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-08 00:52:40 +04:00
|
|
|
// Store returns a byte slice of a marshalled to text MostListenedSong.
|
|
|
|
func (mls *MostListenedSong) Store() []byte {
|
|
|
|
if !mls.changed {
|
2023-09-10 18:23:06 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-08 00:52:40 +04:00
|
|
|
buf := make([]byte, 0, 30+len(mls.Song))
|
2023-03-13 00:03:59 +04:00
|
|
|
b := bytes.NewBuffer(buf)
|
|
|
|
|
2023-10-08 00:52:40 +04:00
|
|
|
b.WriteString(mls.Date.Format(time.RFC3339))
|
2023-03-13 00:03:59 +04:00
|
|
|
b.WriteByte('\n')
|
2023-10-08 00:52:40 +04:00
|
|
|
b.WriteString(strconv.Itoa(mls.Listeners))
|
2023-03-13 00:03:59 +04:00
|
|
|
b.WriteByte('\n')
|
2023-10-08 00:52:40 +04:00
|
|
|
b.WriteString(mls.Song)
|
2023-03-13 00:03:59 +04:00
|
|
|
|
|
|
|
return b.Bytes()
|
|
|
|
}
|