aboutsummaryrefslogtreecommitdiff
path: root/cmd/mpd-scrobbler
diff options
context:
space:
mode:
authorFranck Cuny <franck@fcuny.net>2021-10-09 18:19:17 -0700
committerFranck Cuny <franck@fcuny.net>2021-10-09 18:22:00 -0700
commit6fde72a495edef3ed108fec5a3324638a8518361 (patch)
treea5df7ec8b46492ea970b5addf6d312f4c03dfbef /cmd/mpd-scrobbler
parentscrobbler: initial log record type (diff)
downloadx-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 'cmd/mpd-scrobbler')
-rw-r--r--cmd/mpd-scrobbler/main.go32
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
+ }
}
}
}