aboutsummaryrefslogtreecommitdiff
path: root/internal/mpd
diff options
context:
space:
mode:
authorFranck Cuny <franck@fcuny.net>2021-10-09 17:20:22 -0700
committerFranck Cuny <franck@fcuny.net>2021-10-09 18:21:28 -0700
commit28dd6b6ae32e0cccdaf6486fb76c6b7f98647f27 (patch)
tree705bcaa9b2a1df7bfc099b505cf3f9f13ccec230 /internal/mpd
parentREADME: add some information about logging (diff)
downloadx-28dd6b6ae32e0cccdaf6486fb76c6b7f98647f27.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--internal/mpd/mpd.go38
1 files changed, 38 insertions, 0 deletions
diff --git a/internal/mpd/mpd.go b/internal/mpd/mpd.go
new file mode 100644
index 0000000..8991adc
--- /dev/null
+++ b/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
+}