-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
162 lines (140 loc) · 3.78 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"runtime"
"time"
s "github.com/connordennison/soundcloud-rpc/structs"
"github.com/getlantern/systray"
"github.com/hugolgst/rich-go/client"
)
var (
connectedToDiscordRPC = false
)
func getCurrentTrack(creds s.SoundCloudApiCreds) s.PlayHistoryTrack {
// first we make the request to the soundcloud api (https://api-v2.soundcloud.com/me/play-history/tracks?limit=1) with the client id and auth token as query params
resp, err := http.Get("https://api-v2.soundcloud.com/me/play-history/tracks?limit=1&client_id=" + creds.ClientId + "&auth_token=" + creds.AuthToken)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// then we decode the response into a PlayHistory struct
var playHistory s.PlayHistory
err = json.NewDecoder(resp.Body).Decode(&playHistory)
if err != nil {
panic(err)
}
// then we return the first track in the PlayHistory struct
return playHistory.Collection[0].PlayHistoryTrack
}
func getUserInfo(creds s.SoundCloudApiCreds) s.User {
resp, err := http.Get("https://api-v2.soundcloud.com/me?client_id=" + creds.ClientId + "&auth_token=" + creds.AuthToken)
if err != nil {
panic(err)
}
defer resp.Body.Close()
var user s.User
err = json.NewDecoder(resp.Body).Decode(&user)
if err != nil {
panic(err)
}
return user
}
func getCredentials() s.SoundCloudApiCreds {
file, err := os.Open("config.json")
if err != nil {
panic(err)
}
defer file.Close()
var creds s.SoundCloudApiCreds
err = json.NewDecoder(file).Decode(&creds)
if err != nil {
panic(err)
}
return creds
}
func connectRPC() {
err := client.Login("1121969757122478090")
if err != nil {
panic(err)
}
connectedToDiscordRPC = true
log.Println("Connected to Discord RPC")
}
func updatePresence(playHistTrack s.PlayHistoryTrack, user s.User) {
startTime := time.Unix(0, playHistTrack.PlayedAt*int64(time.Millisecond))
duration := time.Duration(playHistTrack.Track.FullDuration) * time.Millisecond
endTime := startTime.Add(duration)
currentTime := time.Now()
if currentTime.After(endTime) {
client.Logout()
connectedToDiscordRPC = false
return
}
if !connectedToDiscordRPC {
connectRPC()
}
err := client.SetActivity(client.Activity{
State: playHistTrack.Track.User.Username,
Details: playHistTrack.Track.Title,
LargeImage: playHistTrack.Track.ArtworkUrl,
LargeText: playHistTrack.Track.Title,
SmallImage: user.AvatarUrl,
SmallText: fmt.Sprintf("Logged in as %s", user.Username),
Buttons: []*client.Button{
{
Label: "Listen on SoundCloud",
Url: playHistTrack.Track.PermalinkUrl,
},
},
Timestamps: &client.Timestamps{
Start: &startTime,
End: &endTime,
},
})
if err != nil {
// in this scenario we don't want to panic as it'd be useless to halt the whole program over one failed rpc update
// instead we just print the error to the console
log.Println(err)
}
}
func getIcon(s string) []byte {
b, err := os.ReadFile(s)
if err != nil {
panic(err)
}
return b
}
func main() {
systray.Run(onReady, onExit)
}
func onReady() {
if runtime.GOOS == "windows" {
systray.SetIcon(getIcon("assets/icon.ico"))
} else {
systray.SetIcon(getIcon("assets/icon.png"))
}
systray.SetTitle("SoundCloud RPC")
systray.SetTooltip("SoundCloud RPC")
systray.AddMenuItem("SoundCloud RPC by cnnd", "SoundCloud RPC by cnnd").Disable()
systray.AddSeparator()
bQuit := systray.AddMenuItem("Quit", "Quit the whole app")
go func() {
<-bQuit.ClickedCh
systray.Quit()
}()
creds := getCredentials()
user := getUserInfo(creds) // we only need to run this once as it's rare for the userinfo to change within one session
connectRPC()
for {
playHistTrack := getCurrentTrack(creds)
updatePresence(playHistTrack, user)
time.Sleep(time.Second * 8)
}
}
func onExit() {
client.Logout()
}