diff options
Diffstat (limited to '')
-rw-r--r-- | tools/mpd-stats/cmd/mpd-scrobbler/main.go | 5 | ||||
-rw-r--r-- | tools/mpd-stats/internal/scrobbler/scrobbler.go | 31 |
2 files changed, 31 insertions, 5 deletions
diff --git a/tools/mpd-stats/cmd/mpd-scrobbler/main.go b/tools/mpd-stats/cmd/mpd-scrobbler/main.go index 3540807..fdd9e4d 100644 --- a/tools/mpd-stats/cmd/mpd-scrobbler/main.go +++ b/tools/mpd-stats/cmd/mpd-scrobbler/main.go @@ -1,7 +1,9 @@ package main import ( + "fmt" "log" + "os" "golang.fcuny.net/mpd-stats/internal/scrobbler" ) @@ -9,8 +11,9 @@ import ( func main() { net := "tcp" addr := "localhost:6600" + dbpath := fmt.Sprintf("%s/.config/scrobbler.sql", os.Getenv("HOME")) - s, err := scrobbler.NewScrobbler(net, addr) + s, err := scrobbler.NewScrobbler(net, addr, dbpath) if err != nil { log.Fatalf("failed to create a client: %v", err) } diff --git a/tools/mpd-stats/internal/scrobbler/scrobbler.go b/tools/mpd-stats/internal/scrobbler/scrobbler.go index 061b909..a31569e 100644 --- a/tools/mpd-stats/internal/scrobbler/scrobbler.go +++ b/tools/mpd-stats/internal/scrobbler/scrobbler.go @@ -1,6 +1,7 @@ package scrobbler import ( + "database/sql" "log" "golang.fcuny.net/mpd-stats/internal/mpd" @@ -8,17 +9,25 @@ import ( type Scrobbler struct { player *mpd.Player + db *sql.DB } -func NewScrobbler(net string, addr string) (*Scrobbler, error) { - var s Scrobbler - +func NewScrobbler(net string, addr string, dbpath string) (*Scrobbler, error) { p, err := mpd.NewPlayer(net, addr) if err != nil { return nil, err } - s.player = p + db, err := opendatabase(dbpath) + if err != nil { + return nil, err + } + + s := Scrobbler{ + player: p, + db: db, + } + return &s, nil } @@ -47,6 +56,7 @@ func (s *Scrobbler) Run() error { } log.Printf("we're playing %s/%s/%s [%s]\n", currentRecord.Artist, currentRecord.Album, currentRecord.Title, currentRecord.Duration) previousRecord = currentRecord + s.save(currentRecord) continue } @@ -60,7 +70,20 @@ func (s *Scrobbler) Run() error { if currentRecord.Id != previousRecord.Id { log.Printf("we're playing %s/%s/%s [%s]\n", currentRecord.Artist, currentRecord.Album, currentRecord.Title, currentRecord.Duration) previousRecord = currentRecord + s.save(currentRecord) } } } } + +func (s *Scrobbler) save(record *Record) error { + _, err := s.db.Exec("insert into records(id, title, artist, album, duration, time) values(?, ?, ?, ?, ?, ?)", + record.Id, + record.Title, + record.Artist, + record.Album, + int(record.Duration.Seconds()), + record.Timestamp, + ) + return err +} |