about summary refs log tree commit diff
path: root/tools
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--tools/mpd-stats/cmd/mpd-scrobbler/main.go32
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
+			}
 		}
 	}
 }