about summary refs log tree commit diff
path: root/tools
diff options
context:
space:
mode:
authorFranck Cuny <franck@fcuny.net>2021-10-09 17:20:22 -0700
committerFranck Cuny <franck@fcuny.net>2022-06-11 14:32:06 -0700
commit4f176ac4a02d8424d21b597b516e69cbec341a2c (patch)
tree49a1333648342ecf981502f8051464d3a3ef01de /tools
parentREADME: add some information about logging (diff)
downloadworld-4f176ac4a02d8424d21b597b516e69cbec341a2c.tar.gz
scrobbler: watch for events and print song details
We create a module "mpd" to interact with our MPD instance. For now we
only have a single function to create a new client, which creates an
actual client for mpd (and we ping the instance every 30 seconds), and
a watcher to receive new events.

The tool "scrobbler" then wait for new events and display songs
information.
Diffstat (limited to 'tools')
-rw-r--r--tools/mpd-stats/cmd/mpd-scrobbler/main.go35
-rw-r--r--tools/mpd-stats/go.mod2
-rw-r--r--tools/mpd-stats/go.sum2
-rw-r--r--tools/mpd-stats/internal/mpd/mpd.go38
4 files changed, 77 insertions, 0 deletions
diff --git a/tools/mpd-stats/cmd/mpd-scrobbler/main.go b/tools/mpd-stats/cmd/mpd-scrobbler/main.go
new file mode 100644
index 0000000..9929225
--- /dev/null
+++ b/tools/mpd-stats/cmd/mpd-scrobbler/main.go
@@ -0,0 +1,35 @@
+package main
+
+import (
+	"log"
+
+	"golang.fcuny.net/mpd-stats/internal/mpd"
+)
+
+func main() {
+	net := "tcp"
+	addr := "localhost:6600"
+
+	c, err := mpd.NewMPD(net, addr)
+	if err != nil {
+		log.Fatalf("failed to create a client: %v", err)
+	}
+
+	defer c.Watcher.Close()
+	defer c.Client.Close()
+
+	for {
+		e := <-c.Watcher.Event
+		if e != "" {
+			attrs, err := c.Client.CurrentSong()
+			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)
+		}
+	}
+}
diff --git a/tools/mpd-stats/go.mod b/tools/mpd-stats/go.mod
index fc37193..cafa7bc 100644
--- a/tools/mpd-stats/go.mod
+++ b/tools/mpd-stats/go.mod
@@ -1,3 +1,5 @@
 module golang.fcuny.net/mpd-stats
 
 go 1.17
+
+require github.com/fhs/gompd/v2 v2.2.0
diff --git a/tools/mpd-stats/go.sum b/tools/mpd-stats/go.sum
new file mode 100644
index 0000000..1c3cda7
--- /dev/null
+++ b/tools/mpd-stats/go.sum
@@ -0,0 +1,2 @@
+github.com/fhs/gompd/v2 v2.2.0 h1:zdSYAAOzQ5cCCgYa5CoXkL0Vr0Cqb/b5JmTobirLc90=
+github.com/fhs/gompd/v2 v2.2.0/go.mod h1:nNdZtcpD5VpmzZbRl5rV6RhxeMmAWTxEsSIMBkmMIy4=
diff --git a/tools/mpd-stats/internal/mpd/mpd.go b/tools/mpd-stats/internal/mpd/mpd.go
new file mode 100644
index 0000000..8991adc
--- /dev/null
+++ b/tools/mpd-stats/internal/mpd/mpd.go
@@ -0,0 +1,38 @@
+package mpd
+
+import (
+	"log"
+	"time"
+
+	"github.com/fhs/gompd/v2/mpd"
+)
+
+type player struct {
+	Watcher *mpd.Watcher
+	Client  *mpd.Client
+}
+
+func NewMPD(net string, addr string) (*player, error) {
+	var (
+		p   player
+		err error
+	)
+
+	p.Watcher, err = mpd.NewWatcher(net, addr, "", "player")
+	if err != nil {
+		log.Fatalf("failed to create a watcher: %v", err)
+	}
+
+	p.Client, err = mpd.Dial(net, addr)
+	if err != nil {
+		log.Fatalf("failed to start mpd client: %v", err)
+	}
+
+	go func() {
+		for range time.Tick(30 * time.Second) {
+			p.Client.Ping()
+		}
+	}()
+
+	return &p, nil
+}