From 4f176ac4a02d8424d21b597b516e69cbec341a2c Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Sat, 9 Oct 2021 17:20:22 -0700 Subject: 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. --- tools/mpd-stats/cmd/mpd-scrobbler/main.go | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 tools/mpd-stats/cmd/mpd-scrobbler/main.go (limited to 'tools/mpd-stats/cmd/mpd-scrobbler') 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) + } + } +} -- cgit 1.4.1