Do not overwrite mostlistenedsong file if a song wasn't changed.
This commit is contained in:
parent
cbf8eb8747
commit
c020031127
@ -74,9 +74,12 @@ func main() {
|
||||
}
|
||||
|
||||
defer func() {
|
||||
err := os.WriteFile(*mostListenedSongPath, radio.StoreMostListenedSong(), fs.ModePerm)
|
||||
if err != nil {
|
||||
log.Println("failed to store a most listened song:", err)
|
||||
fileData := radio.StoreMostListenedSong()
|
||||
if fileData != nil {
|
||||
err := os.WriteFile(*mostListenedSongPath, fileData, fs.ModePerm)
|
||||
if err != nil {
|
||||
log.Println("failed to store a most listened song:", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := srv.Stop(); err != nil {
|
||||
|
@ -9,6 +9,8 @@ import (
|
||||
|
||||
const MostListenedDateFormat = "02 January 2006"
|
||||
|
||||
var mlsChanged = false
|
||||
|
||||
type MostListenedSong struct {
|
||||
Listeners int
|
||||
Date time.Time
|
||||
@ -43,6 +45,7 @@ func CheckAndUpdateMostListenedSong(cur, prev Song) {
|
||||
Date: time.Now().UTC(),
|
||||
Song: prev.Song}
|
||||
}
|
||||
mlsChanged = true
|
||||
}
|
||||
|
||||
// MostListened returns song that currently is the song with most simultaneous
|
||||
@ -68,6 +71,10 @@ func LoadMostListenedSong(data []byte) (err error) {
|
||||
}
|
||||
|
||||
func StoreMostListenedSong() []byte {
|
||||
if !mlsChanged {
|
||||
return nil
|
||||
}
|
||||
|
||||
buf := make([]byte, 0, 30+len(mostListened.Song))
|
||||
b := bytes.NewBuffer(buf)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user