forked from hawx/mpd-scrobbler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
scrobbler.go
114 lines (93 loc) · 2.27 KB
/
scrobbler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"time"
"github.com/BurntSushi/toml"
"github.com/chrisf1337/mpd-scrobbler/client"
"github.com/chrisf1337/mpd-scrobbler/scrobble"
)
const (
// only submit tracks longer then minTrackLen
minTrackLen = 30
// polling interval
sleepTime = 5 * time.Second
)
const helpMessage = `Usage: mpd-scrobbler [options]
Scrobbles tracks from mpd.
--config <path> # Path to config file (default: './config.toml')
--db <path> # Path to database for caching (default: './scrobble.db')
--port <port> # Port mpd running on (default: '6600')
--help # Display this message
`
var (
config = flag.String("config", "./config.toml", "")
dbPath = flag.String("db", "./scrobble.db", "")
port = flag.String("port", "6600", "")
help = flag.Bool("help", false, "")
)
func catchInterrupt() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
s := <-c
log.Printf("caught %s: shutting down", s)
}
func init() {
log.SetFlags(log.Lshortfile)
}
func main() {
flag.Parse()
if *help {
fmt.Println(helpMessage)
os.Exit(0)
}
c, err := client.Dial("tcp", ":"+*port)
if err != nil {
log.Fatal(err)
}
defer c.Close()
db, err := scrobble.Open(*dbPath)
if err != nil {
log.Fatal(err)
}
defer db.Close()
var conf map[string]map[string]string
if _, err := toml.DecodeFile(*config, &conf); err != nil {
log.Fatal(err)
}
apis := []scrobble.Scrobbler{}
for k, v := range conf {
api, err := scrobble.New(db, k, v["key"], v["secret"], v["username"], v["password"], v["uri"])
if err != nil {
log.Fatal(k, " ", err)
}
apis = append(apis, api)
}
toSubmit := make(chan client.Song)
nowPlaying := make(chan client.Song)
go c.Watch(sleepTime, toSubmit, nowPlaying)
go func() {
for {
select {
case s := <-nowPlaying:
for _, api := range apis {
err := api.NowPlaying(s.Artist, s.Album, s.AlbumArtist, s.Title)
if err != nil {
log.Printf("[%s] err(NowPlaying): %s\n", api.Name(), err)
}
}
case s := <-toSubmit:
for _, api := range apis {
err := api.Scrobble(s.Artist, s.Album, s.AlbumArtist, s.Title, s.Start)
if err != nil {
log.Printf("[%s] err(Scrobble): %s\n", api.Name(), err)
}
}
}
}
}()
catchInterrupt()
}