about summary refs log tree commit diff
path: root/tools/mpd-stats/internal/scrobbler
diff options
context:
space:
mode:
Diffstat (limited to 'tools/mpd-stats/internal/scrobbler')
-rw-r--r--tools/mpd-stats/internal/scrobbler/scrobbler.go66
1 files changed, 66 insertions, 0 deletions
diff --git a/tools/mpd-stats/internal/scrobbler/scrobbler.go b/tools/mpd-stats/internal/scrobbler/scrobbler.go
new file mode 100644
index 0000000..061b909
--- /dev/null
+++ b/tools/mpd-stats/internal/scrobbler/scrobbler.go
@@ -0,0 +1,66 @@
+package scrobbler
+
+import (
+	"log"
+
+	"golang.fcuny.net/mpd-stats/internal/mpd"
+)
+
+type Scrobbler struct {
+	player *mpd.Player
+}
+
+func NewScrobbler(net string, addr string) (*Scrobbler, error) {
+	var s Scrobbler
+
+	p, err := mpd.NewPlayer(net, addr)
+	if err != nil {
+		return nil, err
+	}
+
+	s.player = p
+	return &s, nil
+}
+
+func (s *Scrobbler) Close() error {
+	return s.player.Close()
+}
+
+func (s *Scrobbler) Run() error {
+	var (
+		currentRecord  *Record
+		previousRecord *Record
+	)
+
+	for {
+		e := <-s.player.Watcher.Event
+		if e != "" {
+			attrs, err := s.player.Client.CurrentSong()
+			if err != nil {
+				log.Fatalf("could not get current song: %v", err)
+			}
+
+			if currentRecord == nil {
+				currentRecord, err = 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 = 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
+			}
+		}
+	}
+}