diff options
| author | Franck Cuny <franck@fcuny.net> | 2021-10-09 18:19:17 -0700 |
|---|---|---|
| committer | Franck Cuny <franck@fcuny.net> | 2021-10-09 18:22:00 -0700 |
| commit | 6fde72a495edef3ed108fec5a3324638a8518361 (patch) | |
| tree | a5df7ec8b46492ea970b5addf6d312f4c03dfbef /cmd/mpd-scrobbler/main.go | |
| parent | scrobbler: initial log record type (diff) | |
| download | x-6fde72a495edef3ed108fec5a3324638a8518361.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 '')
| -rw-r--r-- | cmd/mpd-scrobbler/main.go | 32 |
1 files changed, 27 insertions, 5 deletions
diff --git a/cmd/mpd-scrobbler/main.go b/cmd/mpd-scrobbler/main.go index 9929225..40b1348 100644 --- a/cmd/mpd-scrobbler/main.go +++ b/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 + } } } } |
