diff options
author | Franck Cuny <franck@fcuny.net> | 2021-10-09 18:19:17 -0700 |
---|---|---|
committer | Franck Cuny <franck@fcuny.net> | 2022-06-11 14:32:07 -0700 |
commit | aa295702fb41f285ec349e05eabc5749c7325418 (patch) | |
tree | 3c4ae89bbee7f096dfc1e1795a700322478dd864 /tools | |
parent | scrobbler: initial log record type (diff) | |
download | world-aa295702fb41f285ec349e05eabc5749c7325418.tar.gz |
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.
Diffstat (limited to 'tools')
-rw-r--r-- | tools/mpd-stats/cmd/mpd-scrobbler/main.go | 32 |
1 files changed, 27 insertions, 5 deletions
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 + } } } } |