-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
137 lines (116 loc) · 2.88 KB
/
main.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"fmt"
"net/url"
"strings"
"time"
_ "embed"
"github.com/Code-Hex/go-generics-cache/policy/fifo"
"github.com/altfoxie/drpc"
"github.com/getlantern/systray"
)
const AppID = "991335093878673448"
//go:embed assets/tray.png
var icon []byte
var firstSecond = time.Unix(1, 0)
func main() {
systray.Run(onReady, nil)
}
func onReady() {
client, err := drpc.New(AppID)
if err != nil {
panic(err)
}
systray.SetIcon(icon)
state := systray.AddMenuItem("No state", "")
state.Disable()
systray.AddSeparator()
restart := systray.AddMenuItem("Restart Discord RPC", "")
quit := systray.AddMenuItem("Quit", "")
go func() {
cache := fifo.NewCache[string, string](fifo.WithCapacity(100))
ticker := time.NewTicker(5 * time.Second)
// hacky trick to force first tick
for ; true; <-ticker.C {
result, err := executeScript()
if err != nil {
state.SetTitle("Script error")
client.Close()
continue
}
if result.state == "" {
state.SetTitle("Apple Music is not running")
client.Close()
continue
}
if result.name == "" && result.artist == "" {
state.SetTitle("No song")
client.Close()
continue
}
if err = client.Connect(); err != nil {
state.SetTitle("Discord RPC error")
continue
}
song := result.artist + " – " + result.name
switch {
case result.artist == "":
song = result.name
case result.name == "":
song = result.artist
}
state.SetTitle(fmt.Sprintf("%s (%d:%02d / %d:%02d)",
song, int(result.position/60), int(result.position)%60,
int(result.duration/60), int(result.duration)%60))
activity := drpc.Activity{
Details: result.name,
State: result.artist,
Timestamps: &drpc.Timestamps{
Start: time.Now().Add(-time.Duration(result.position) * time.Second),
End: firstSecond,
},
Assets: &drpc.Assets{
LargeImage: "music",
SmallImage: "pause",
},
Buttons: []drpc.Button{
{
Label: "Search on YouTube",
URL: "https://www.youtube.com/results?" + url.Values{
"search_query": {song},
}.Encode(),
},
},
}
if result.state == StatePlaying {
activity.Assets.SmallImage = "play"
activity.Timestamps = &drpc.Timestamps{
Start: time.Now().Add(-time.Duration(result.position) * time.Second),
End: time.Now().
Add(time.Duration((result.duration - result.position) * float64(time.Second))),
}
}
artwork, _ := cache.Get(song)
query := strings.TrimSpace(result.artist + " " + result.name)
if artwork == "" {
artwork = artworkSearchITunes(query)
}
if artwork == "" {
artwork = artworkSearchMusixmatch(query)
}
if artwork != "" {
cache.Set(song, artwork)
activity.Assets.LargeImage = artwork
}
client.SetActivity(activity)
}
}()
for {
select {
case <-restart.ClickedCh:
client.Close()
case <-quit.ClickedCh:
systray.Quit()
}
}
}