-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider_lastfm.go
63 lines (53 loc) · 1.31 KB
/
provider_lastfm.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
package main
import (
"log"
"strings"
"time"
"github.com/shkh/lastfm-go/lastfm"
)
func (l *LastFmConfig) NowPlaying(n NowPlaying) {
if l == nil {
return
}
api := lastfm.New(l.Key, l.Secret)
err := api.Login(l.Username, l.Password)
if err != nil {
log.Printf("[lastfm] login error: %v", err)
return
}
// https://www.last.fm/api/show/track.updateNowPlaying
if _, err := api.Track.UpdateNowPlaying(lastfm.P{
"artist": strings.Join(n.Artists, ", "),
"track": n.Track,
"album": n.Album,
"duration": n.Duration,
"timestamp": time.Now().Unix(),
}); err != nil {
log.Printf("[lastfm] error updating now playing status: %v", err)
return
}
log.Println("[lastfm] updated now playing status ✅")
}
func (l *LastFmConfig) Scrobble(n NowPlaying) {
if l == nil {
return
}
api := lastfm.New(l.Key, l.Secret)
err := api.Login(l.Username, l.Password)
if err != nil {
log.Printf("[lastfm] login error: %v", err)
return
}
// https://www.last.fm/api/show/track.scrobble
if _, err := api.Track.Scrobble(lastfm.P{
"artist": strings.Join(n.Artists, ", "),
"track": n.Track,
"album": n.Album,
"duration": n.Duration,
"timestamp": time.Now().Unix(),
}); err != nil {
log.Printf("[lastfm] error scrobbling: %v", err)
return
}
log.Println("[lastfm] scrobbled ✅")
}