-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathradio.go
298 lines (259 loc) · 8.23 KB
/
radio.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package main
import (
"encoding/json"
"errors"
"log"
"net/http"
"net/url"
"slices"
"strconv"
"strings"
"time"
"github.com/bwmarrin/discordgo"
"mvdan.cc/xurls/v2"
)
// Current song API response
type Current struct {
Main struct {
Np string `json:"np"`
Listeners int `json:"listeners"`
Bitrate int `json:"bitrate"`
Isafkstream bool `json:"isafkstream"`
Isstreamdesk bool `json:"isstreamdesk"`
Current int `json:"current"`
StartTime int `json:"start_time"`
EndTime int `json:"end_time"`
Lastset string `json:"lastset"`
Trackid int `json:"trackid"`
Thread string `json:"thread"`
Requesting bool `json:"requesting"`
Djname string `json:"djname"`
Dj struct {
ID int `json:"id"`
Djname string `json:"djname"`
Djtext string `json:"djtext"`
Djimage string `json:"djimage"`
Djcolor string `json:"djcolor"`
Visible bool `json:"visible"`
Priority int `json:"priority"`
CSS string `json:"css"`
ThemeID int `json:"theme_id"`
Role string `json:"role"`
} `json:"dj"`
Queue []struct {
Meta string `json:"meta"`
Time string `json:"time"`
Type int `json:"type"`
Timestamp int64 `json:"timestamp"`
} `json:"queue"`
Lp []struct {
Meta string `json:"meta"`
Time string `json:"time"`
Type int `json:"type"`
Timestamp int `json:"timestamp"`
} `json:"lp"`
} `json:"main"`
}
const api = `https://r-a-d.io/api`
const frontpage = `https://r-a-d.io`
const radioRed = 0xDF4C3A
func radioState() (Current, error) {
//Get the current state structure from the r/a/dio API
resp, err := http.Get(api)
if err != nil {
return Current{}, err
}
if resp.StatusCode != http.StatusOK {
return Current{}, errors.New("Error: " + http.StatusText(resp.StatusCode))
}
defer resp.Body.Close()
// Fill the record with the data from the JSON
var record Current
// Use json.Decode for reading streams of JSON data
if err := json.NewDecoder(resp.Body).Decode(&record); err != nil {
return Current{}, err
}
return record, nil
}
func radioHelp(s *discordgo.Session, m *discordgo.MessageCreate) {
s.ChannelMessageSend(m.ChannelID, "Usage: !radio [queue, dj]")
}
func radioQueue(s *discordgo.Session, m *discordgo.MessageCreate) {
record, err := radioState()
if err != nil {
log.Println(err)
return
}
if !record.Main.Isafkstream {
s.ChannelMessageSend(m.ChannelID, "Sadly Hanyuu is not in a position to play the current queue, a DJ is playing!")
return
}
queue := make([]*discordgo.MessageEmbedField, len(record.Main.Queue))
for i := range queue {
timeLeft := (time.Duration(time.Unix(record.Main.Queue[i].Timestamp, 0).Sub(time.Now().In(time.UTC)).Seconds()) * time.Second).String()
for i := 0; i < len(timeLeft)-10; i++ {
timeLeft = " " + timeLeft
}
queue[i] = new(discordgo.MessageEmbedField)
queue[i].Name = timeLeft + " from now"
queue[i].Value = record.Main.Queue[i].Meta
queue[i].Inline = false
}
embed := &discordgo.MessageEmbed{URL: frontpage,
Title: "Playback queue",
Color: radioRed,
Footer: &discordgo.MessageEmbedFooter{Text: "Now playing: " + record.Main.Np},
Author: &discordgo.MessageEmbedAuthor{Name: record.Main.Djname, IconURL: api + "/dj-image/" + record.Main.Dj.Djimage},
Thumbnail: &discordgo.MessageEmbedThumbnail{URL: api + "/dj-image/" + record.Main.Dj.Djimage},
Fields: queue,
}
s.ChannelMessageSendEmbed(m.ChannelID, embed)
}
func radioCurrent(s *discordgo.Session, m *discordgo.MessageCreate) {
record, err := radioState()
if err != nil {
log.Println(err)
return
}
progress := (time.Duration(record.Main.Current-record.Main.StartTime) * time.Second).String() + " / " + (time.Duration(record.Main.EndTime-record.Main.StartTime) * time.Second).String()
fields := make([]*discordgo.MessageEmbedField, 2)
fields[0] = new(discordgo.MessageEmbedField)
fields[1] = new(discordgo.MessageEmbedField)
fields[1].Name = record.Main.Dj.Djname
fields[1].Inline = false
fields[1].Value = "Listeners: " + strconv.Itoa(record.Main.Listeners)
fields[0].Name = record.Main.Np
fields[0].Value = progress
fields[0].Inline = false
if !record.Main.Isafkstream && !IsUrl(record.Main.Thread) && HasImage(record.Main.Thread) {
embed := &discordgo.MessageEmbed{URL: frontpage,
Title: "Now playing",
Color: radioRed,
Image: GetImage(record.Main.Thread),
Thumbnail: &discordgo.MessageEmbedThumbnail{URL: api + "/dj-image/" + record.Main.Dj.Djimage},
Fields: fields,
}
if _, err := s.ChannelMessageSendEmbed(m.ChannelID, embed); err != nil {
log.Println(err)
}
} else {
footer := new(discordgo.MessageEmbedFooter)
if !record.Main.Isafkstream {
footer.Text = "Current thread: " + record.Main.Thread
} else {
footer.Text = "Upcoming: " + record.Main.Queue[0].Meta
}
embed := &discordgo.MessageEmbed{URL: frontpage,
Title: "Now playing",
Color: radioRed,
Footer: footer,
Thumbnail: &discordgo.MessageEmbedThumbnail{URL: api + "/dj-image/" + record.Main.Dj.Djimage},
Fields: fields,
}
if _, err := s.ChannelMessageSendEmbed(m.ChannelID, embed); err != nil {
log.Println(err)
}
}
}
func GetImage(s string) *discordgo.MessageEmbedImage {
rxRelaxed := xurls.Relaxed()
res := rxRelaxed.FindString(s)
return &discordgo.MessageEmbedImage{
URL: res,
}
}
func HasImage(s string) bool {
rxRelaxed := xurls.Relaxed()
res := rxRelaxed.FindString(s)
if res == "" {
return false
}
ext, err := GetFileExtensionFromUrl(res)
if err != nil {
return false
}
things := []string{"gif", "jpg", "png", "webp", "jpeg"}
return slices.Contains(things, ext)
}
func GetFileExtensionFromUrl(rawUrl string) (string, error) {
u, err := url.Parse(rawUrl)
if err != nil {
return "", err
}
pos := strings.LastIndex(u.Path, ".")
if pos == -1 {
return "", errors.New("couldn't find a period to indicate a file extension")
}
return u.Path[pos+1 : len(u.Path)], nil
}
func IsUrl(s string) bool {
_, err := url.Parse(s)
return err != nil
}
// Songs is the API response of the current music queue
type Songs []struct {
Artist string `json:"artist"`
Title string `json:"title"`
ID int `json:"id"`
Lastplayed int `json:"lastplayed"`
Lastrequested int `json:"lastrequested"`
Requestable bool `json:"requestable"`
}
// Search is the API response for a music index search query
type Search struct {
Total int `json:"total"`
PerPage int `json:"per_page"`
CurrentPage int `json:"current_page"`
LastPage int `json:"last_page"`
From int `json:"from"`
To int `json:"to"`
Data Songs `json:"data"`
}
func radioSearchResults(query string) Songs {
const searchAPI = api + "/search/"
//Get the current state structure from the r/a/dio API
resp, err := http.Get(searchAPI + query)
if err != nil {
log.Println(err)
}
if resp.StatusCode != http.StatusOK {
log.Println("Error: " + http.StatusText(resp.StatusCode))
}
defer resp.Body.Close()
// Fill the record with the data from the JSON
var record Search
// Use json.Decode for reading streams of JSON data
if err := json.NewDecoder(resp.Body).Decode(&record); err != nil {
log.Println(err)
}
var songs Songs
songs = append(songs, record.Data...)
for i := 2; i <= record.LastPage; i++ {
resp, err := http.Get(searchAPI + query + "?page=" + strconv.Itoa(i))
if err != nil {
log.Println(err)
}
if resp.StatusCode != http.StatusOK {
log.Println("Error: " + http.StatusText(resp.StatusCode))
}
// Fill the record with the data from the JSON
var record Search
// Use json.Decode for reading streams of JSON data
if err := json.NewDecoder(resp.Body).Decode(&record); err != nil {
log.Println(err)
}
songs = append(songs, record.Data...)
resp.Body.Close()
}
return songs
}
func radioSearch(s *discordgo.Session, m *discordgo.MessageCreate, name string) {
songs := radioSearchResults(name)
lines := make([]string, len(songs))
for i := range lines {
lines[i] = songs[i].Artist + " - " + "[" + songs[i].Title + "]" + "(" + `https://r-a-d.io/request/` + strconv.Itoa(songs[i].ID) + ")"
}
if _, err := s.ChannelMessageSend(m.ChannelID, strings.Join(lines, "\n")); err != nil {
log.Println(err)
}
}