-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloop.go
123 lines (101 loc) · 2.91 KB
/
loop.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
115
116
117
118
119
120
121
122
123
package main
import (
"fmt"
"log"
"regexp"
"strings"
"time"
"github.com/godbus/dbus/v5"
)
func RunMainLoop(conn *dbus.Conn, config *Config) {
previouslyPlaying := map[string]NowPlaying{}
scrobbledPrevious := map[string]bool{}
var blacklist []*regexp.Regexp
for _, expression := range config.Blacklist {
compiled, err := regexp.Compile(expression)
if err != nil {
log.Printf("error compiling regex blacklist entry: %v", err)
continue
}
blacklist = append(blacklist, compiled)
}
if len(blacklist) > 0 {
log.Printf("ignoring scrobbles from %d players", len(blacklist))
}
for {
nowPlaying, err := GetNowPlaying(conn, blacklist)
if err != nil {
log.Printf("failed to get current playback status: %v", err)
continue
}
for player := range nowPlaying {
if _, ok := previouslyPlaying[player]; !ok {
log.Printf("new player found: %s", player)
previouslyPlaying[player] = NowPlaying{}
scrobbledPrevious[player] = false
}
}
for player := range previouslyPlaying {
if _, ok := nowPlaying[player]; !ok {
log.Printf("player disappeared: %s", player)
delete(previouslyPlaying, player)
delete(scrobbledPrevious, player)
}
}
for player, status := range nowPlaying {
minPlayTime, err := minPlayTime(status, config)
if err != nil {
log.Printf(
"[%s] cannot calculate minimum playback time for %s by %s: %v",
player,
status.Track,
strings.Join(status.Artists, ", "),
err,
)
continue
}
if !NowPlayingEquals(status, previouslyPlaying[player]) {
log.Printf("[%s] started playing %s by %s", player, status.Track, strings.Join(status.Artists, ", "))
status.Position = 0
previouslyPlaying[player] = status
scrobbledPrevious[player] = false
for _, provider := range config.Providers() {
provider.NowPlaying(status)
}
continue
}
if status.Position < minPlayTime || status.PlaybackStatus != PlaybackPlaying || scrobbledPrevious[player] {
continue
}
log.Printf(
"[%s] scrobbling track %s by %s, played %s/%s",
player,
status.Track,
strings.Join(status.Artists, ", "),
formatDuration(minPlayTime),
formatDuration(status.Duration),
)
scrobbledPrevious[player] = true
for _, provider := range config.Providers() {
provider.NowPlaying(status)
provider.Scrobble(status)
}
}
time.Sleep(time.Millisecond * time.Duration(config.PollRate))
}
}
func minPlayTime(nowPlaying NowPlaying, config *Config) (int64, error) {
if nowPlaying.Duration < 0 {
return 0, fmt.Errorf("invalid track length: %d", nowPlaying.Duration)
}
half := int64((float64(nowPlaying.Duration) / 100) * float64(config.MinPlaybackPercent))
return min(half, config.MinPlaybackDuration), nil
}
func formatDuration(duration int64) string {
if duration < 0 {
return "invalid duration"
}
minutes := duration / 60
seconds := duration % 60
return fmt.Sprintf("%02d:%02d", minutes, seconds)
}