about summary refs log tree commit diff
path: root/tools/mpd-stats/internal
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/mpd-stats/internal
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 '')
-rw-r--r--tools/mpd-stats/internal/mpd/mpd.go38
1 files changed, 38 insertions, 0 deletions
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
+}