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