Do not overwrite mostlistenedsong file if a song wasn't changed.
This commit is contained in:
parent
cbf8eb8747
commit
c020031127
@ -74,10 +74,13 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
err := os.WriteFile(*mostListenedSongPath, radio.StoreMostListenedSong(), fs.ModePerm)
|
fileData := radio.StoreMostListenedSong()
|
||||||
|
if fileData != nil {
|
||||||
|
err := os.WriteFile(*mostListenedSongPath, fileData, fs.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("failed to store a most listened song:", err)
|
log.Println("failed to store a most listened song:", err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err := srv.Stop(); err != nil {
|
if err := srv.Stop(); err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
|
@ -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