From aa295702fb41f285ec349e05eabc5749c7325418 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Sat, 9 Oct 2021 18:19:17 -0700 Subject: scrobbler: create a record on new song When we receive an event from the player, we look if the song is different from the previous one, and we create a new record if that's the case. If the song is similar, there's nothing to do. --- tools/mpd-stats/cmd/mpd-scrobbler/main.go | 32 ++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) (limited to 'tools/mpd-stats') diff --git a/tools/mpd-stats/cmd/mpd-scrobbler/main.go b/tools/mpd-stats/cmd/mpd-scrobbler/main.go index 9929225..40b1348 100644 --- a/tools/mpd-stats/cmd/mpd-scrobbler/main.go +++ b/tools/mpd-stats/cmd/mpd-scrobbler/main.go @@ -4,6 +4,7 @@ import ( "log" "golang.fcuny.net/mpd-stats/internal/mpd" + "golang.fcuny.net/mpd-stats/internal/scrobbler" ) func main() { @@ -18,6 +19,10 @@ func main() { defer c.Watcher.Close() defer c.Client.Close() + var ( + currentRecord *scrobbler.Record + previousRecord *scrobbler.Record + ) for { e := <-c.Watcher.Event if e != "" { @@ -25,11 +30,28 @@ func main() { if err != nil { log.Fatalf("could not get current song: %v", err) } - currentAlbum := attrs["Album"] - artist := attrs["Artist"] - song := attrs["Title"] - duration := attrs["duration"] - log.Printf("we're playing %s/%s/%s [%s]\n", artist, currentAlbum, song, duration) + + if currentRecord == nil { + currentRecord, err = scrobbler.NewRecord(attrs) + if err != nil { + log.Fatalf("could not create a log: %v", err) + } + log.Printf("we're playing %s/%s/%s [%s]\n", currentRecord.Artist, currentRecord.Album, currentRecord.Title, currentRecord.Duration) + previousRecord = currentRecord + continue + } + + if currentRecord.Title != attrs["Title"] || currentRecord.Artist != attrs["Artist"] || currentRecord.Album != attrs["Album"] { + currentRecord, err = scrobbler.NewRecord(attrs) + if err != nil { + log.Fatalf("could not create a log: %v", err) + } + } + + if currentRecord.Id != previousRecord.Id { + log.Printf("we're playing %s/%s/%s [%s]\n", currentRecord.Artist, currentRecord.Album, currentRecord.Title, currentRecord.Duration) + previousRecord = currentRecord + } } } } -- cgit 1.4.1