aboutsummaryrefslogtreecommitdiff
path: root/cmd/mpd-scrobbler
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/mpd-scrobbler')
-rw-r--r--cmd/mpd-scrobbler/README.org22
-rw-r--r--cmd/mpd-scrobbler/main.go57
2 files changed, 79 insertions, 0 deletions
diff --git a/cmd/mpd-scrobbler/README.org b/cmd/mpd-scrobbler/README.org
new file mode 100644
index 0000000..8c0a7d9
--- /dev/null
+++ b/cmd/mpd-scrobbler/README.org
@@ -0,0 +1,22 @@
+#+TITLE: mpd-stats
+
+Log played songs to extract statistics. This is similar to what libre.fm used to do, but locally.
+
+* Logging
+Collect logs from mpd. A log record is composed of the following fields:
+- id: UUID
+- song's name: the name of the song
+- song's album: the name of the album
+- song's artist: the name of the artist
+- song's duration: the duration of the song
+- date: date the song was played
+
+The logs are recorded in a database (sqlite3 to start).
+* Install
+The Makefile assumes the system is running Linux and systemd.
+
+Run =make install=. This will:
+- install the binary in your =GOPATH= (using =go install=)
+- install a systemd unit file under =$HOME/.config/systemd/user=
+- reload systemd unit files
+- start the service
diff --git a/cmd/mpd-scrobbler/main.go b/cmd/mpd-scrobbler/main.go
new file mode 100644
index 0000000..c2693a4
--- /dev/null
+++ b/cmd/mpd-scrobbler/main.go
@@ -0,0 +1,57 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+ "log"
+ "os"
+ "path/filepath"
+
+ "golang.fcuny.net/mpd-stats/internal/scrobbler"
+)
+
+func main() {
+ var (
+ mpdHost = flag.String("host", "localhost", "The MPD server to connect to (default: localhost)")
+ mpdPort = flag.Int("port", 6600, "The TCP port of the MPD server to connect to (default: 6600)")
+ )
+ flag.Parse()
+
+ net := "tcp"
+ addr := fmt.Sprintf("%s:%d", *mpdHost, *mpdPort)
+
+ dbpath, err := getDbPath()
+ if err != nil {
+ log.Fatalf("failed to get the path to the database: %v", err)
+ }
+
+ s, err := scrobbler.NewScrobbler(net, addr, dbpath)
+ if err != nil {
+ log.Fatalf("failed to create a client: %v", err)
+ }
+
+ defer func() {
+ if err := s.Close(); err != nil {
+ log.Fatalf("failed to close the scrobbler: %v", err)
+ }
+ }()
+
+ s.Run()
+}
+
+func getDbPath() (string, error) {
+ xch := os.Getenv("XDG_CONFIG_HOME")
+ if xch == "" {
+ home := os.Getenv("HOME")
+ xch = filepath.Join(home, ".config")
+ }
+
+ scrobblerHome := filepath.Join(xch, "mpd-scrobbler")
+ if _, err := os.Stat(scrobblerHome); os.IsNotExist(err) {
+ if err := os.Mkdir(scrobblerHome, 0755); err != nil {
+ return "", err
+ }
+ }
+
+ return filepath.Join(scrobblerHome, "scrobbler.sql"), nil
+}