about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFranck Cuny <franck@fcuny.net>2021-10-10 11:47:52 -0700
committerFranck Cuny <franck@fcuny.net>2022-06-11 14:32:07 -0700
commit5bc82c03c6910e9205ef118a55e18a5967a9ebb3 (patch)
tree20a9d7b5d4bcc9572e917271ccd7b4fff5c21972
parentscrobbler: add functions to create and run it (diff)
downloadworld-5bc82c03c6910e9205ef118a55e18a5967a9ebb3.tar.gz
mpd-stats: create and run the scrobbler
-rw-r--r--tools/mpd-stats/cmd/mpd-scrobbler/main.go43
1 files changed, 4 insertions, 39 deletions
diff --git a/tools/mpd-stats/cmd/mpd-scrobbler/main.go b/tools/mpd-stats/cmd/mpd-scrobbler/main.go
index ba7bb05..3540807 100644
--- a/tools/mpd-stats/cmd/mpd-scrobbler/main.go
+++ b/tools/mpd-stats/cmd/mpd-scrobbler/main.go
@@ -3,7 +3,6 @@ package main
 import (
 	"log"
 
-	"golang.fcuny.net/mpd-stats/internal/mpd"
 	"golang.fcuny.net/mpd-stats/internal/scrobbler"
 )
 
@@ -11,50 +10,16 @@ func main() {
 	net := "tcp"
 	addr := "localhost:6600"
 
-	c, err := mpd.NewPlayer(net, addr)
+	s, err := scrobbler.NewScrobbler(net, addr)
 	if err != nil {
 		log.Fatalf("failed to create a client: %v", err)
 	}
 
 	defer func() {
-		if err := c.Close(); err != nil {
-			log.Fatalf("failed to close the player: %v", err)
+		if err := s.Close(); err != nil {
+			log.Fatalf("failed to close the scrobbler: %v", err)
 		}
 	}()
 
-	var (
-		currentRecord  *scrobbler.Record
-		previousRecord *scrobbler.Record
-	)
-	for {
-		e := <-c.Watcher.Event
-		if e != "" {
-			attrs, err := c.Client.CurrentSong()
-			if err != nil {
-				log.Fatalf("could not get current song: %v", err)
-			}
-
-			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
-			}
-		}
-	}
+	s.Run()
 }