-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructs.go
439 lines (386 loc) · 11 KB
/
structs.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package ksoftgo
import (
"encoding/json"
"fmt"
"math"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
type KSession struct {
Token string
Debug bool
Client *http.Client
UserAgent string
MaxRestRetries int
RetryAfter time.Duration
}
// RESPONSES
type Album struct {
ID int `json:"id"`
Name string `json:"name"`
Year int `json:"year"`
Artist struct {
ID int `json:"id"`
Name string `json:"name"`
} `json:"artist"`
Tracks []struct {
ID int `json:"id"`
Name string `json:"name"`
} `json:"tracks"`
}
type Artist struct {
ID int `json:"id"`
Name string `json:"name"`
Albums []struct {
ID int `json:"id"`
Name string `json:"name"`
Year int `json:"year"`
} `json:"albums"`
Tracks []struct {
ID int `json:"id"`
Name string `json:"name"`
} `json:"tracks"`
}
type APIErrorMessage struct {
Code int `json:"code"`
Message string `json:"message"`
}
type Image struct {
URL string `json:"url"`
Snowflake string `json:"snowflake"`
NSFW bool `json:"nsfw"`
Tag string `json:"tag"`
}
type WikiHowImage struct {
URL string `json:"url"`
Title string `json:"title"`
NSFW bool `json:"nsfw"`
ArticleURL string `json:"article_url"`
}
type Tags struct {
Models []struct {
Name string `json:"name"`
Nsfw bool `json:"nsfw"`
} `json:"models"`
Tags []string `json:"tags"`
NsfwTags []string `json:"nsfw_tags"`
}
type BanCheck struct {
Banned bool `json:"is_banned"`
}
type BanInfo struct {
ID string `json:"id"`
Name string `json:"name"`
Discriminator string `json:"discriminator"`
ModeratorID string `json:"moderator_id"`
Reason string `json:"reason"`
Proof string `json:"proof"`
IsBanActive bool `json:"is_ban_active"`
CanBeAppealed bool `json:"can_be_appealed"`
Timestamp string `json:"timestamp"`
AppealReason string `json:"appeal_reason"`
AppealDate interface{} `json:"appeal_date"`
RequestedBy string `json:"requested_by"`
Exists bool `json:"exists"`
}
type BansList struct {
BanCount int `json:"ban_count"`
PageCount int `json:"page_count"`
PerPage int `json:"per_page"`
Page int `json:"page"`
OnPage int `json:"on_page"`
NextPage int `json:"next_page"`
PreviousPage interface{} `json:"previous_page"`
Data []struct {
ID string `json:"id"`
Name string `json:"name"`
Discriminator string `json:"discriminator"`
ModeratorID string `json:"moderator_id"`
Reason string `json:"reason"`
Proof string `json:"proof"`
IsBanActive bool `json:"is_ban_active"`
CanBeAppealed bool `json:"can_be_appealed"`
Timestamp string `json:"timestamp"`
AppealReason interface{} `json:"appeal_reason"`
AppealDate interface{} `json:"appeal_date"`
} `json:"data"`
}
type Currency struct {
Value float64 `json:"value"`
Pretty string `json:"pretty"`
}
type GeoIP struct {
Error bool `json:"error"`
Code int `json:"code"`
Data struct {
City string `json:"city"`
ContinentCode string `json:"continent_code"`
ContinentName string `json:"continent_name"`
CountryCode string `json:"country_code"`
CountryName string `json:"country_name"`
DmaCode interface{} `json:"dma_code"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
PostalCode string `json:"postal_code"`
Region string `json:"region"`
TimeZone string `json:"time_zone"`
Apis struct {
Weather string `json:"weather"`
Gis string `json:"gis"`
Openstreetmap string `json:"openstreetmap"`
Googlemaps string `json:"googlemaps"`
} `json:"apis"`
} `json:"data"`
}
type GIS struct {
Error bool `json:"error"`
Code int `json:"code"`
Data struct {
Address string `json:"address"`
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
BoundingBox []string `json:"bounding_box"`
Type []string `json:"type"`
Map string `json:"map"`
} `json:"data"`
}
type LyricsSearch struct {
Total int `json:"total"`
Took int `json:"took"`
Data []struct {
Artist string `json:"artist"`
ArtistID int `json:"artist_id"`
Album string `json:"album"`
AlbumIds string `json:"album_ids"`
AlbumYear string `json:"album_year"`
Name string `json:"name"`
Lyrics string `json:"lyrics"`
SearchStr string `json:"search_str"`
AlbumArt string `json:"album_art"`
Popularity int `json:"popularity"`
ID string `json:"id"`
SearchScore float64 `json:"search_score"`
URL string `json:"url"`
} `json:"data"`
}
type Reddit struct {
Title string `json:"title"`
ImageURL string `json:"image_url"`
Source string `json:"source"`
Subreddit string `json:"subreddit"`
Upvotes int `json:"upvotes"`
Downvotes int `json:"downvotes"`
Comments int `json:"comments"`
CreatedAt float64 `json:"created_at"`
NSFW bool `json:"nsfw"`
Author string `json:"author"`
}
type Track struct {
Name string `json:"name"`
Artist struct {
ID int `json:"id"`
Name string `json:"name"`
} `json:"artist"`
Albums []struct {
ID int `json:"id"`
Name string `json:"name"`
Year int `json:"year"`
} `json:"albums"`
Lyrics string `json:"lyrics"`
}
type Weather struct {
Error bool `json:"error"`
Status int `json:"status"`
Data struct {
Time string `json:"time"`
Summary string `json:"summary"`
Icon string `json:"icon"`
PrecipIntensity float64 `json:"precipIntensity"`
PrecipProbability float64 `json:"precipProbability"`
Temperature float64 `json:"temperature"`
ApparentTemperature float64 `json:"apparentTemperature"`
DewPoint float64 `json:"dewPoint"`
Humidity float64 `json:"humidity"`
Pressure float64 `json:"pressure"`
WindSpeed float64 `json:"windSpeed"`
WindGust float64 `json:"windGust"`
WindBearing int `json:"windBearing"`
CloudCover float64 `json:"cloudCover"`
UvIndex int `json:"uvIndex"`
Visibility float64 `json:"visibility"`
Ozone float64 `json:"ozone"`
SunriseTime interface{} `json:"sunriseTime"`
SunsetTime interface{} `json:"sunsetTime"`
IconURL string `json:"icon_url"`
Alerts []struct {
Title string `json:"title"`
Regions []string `json:"regions"`
Severity string `json:"severity"`
Time int `json:"time"`
Expires int `json:"expires"`
Description string `json:"description"`
URI string `json:"uri"`
} `json:"alerts"`
Units string `json:"units"`
Location struct {
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
Address string `json:"address,omitempty"`
} `json:"location"`
} `json:"data"`
}
// POST PARAMETERS
type ParamAddBan struct {
ID int64 `json:"user,omitempty"`
Reason string `json:"reason,omitempty"`
Proof string `json:"proof,omitempty"`
Name string `json:"user_name,omitempty"`
Discriminator int `json:"user_discriminator,omitempty"`
ModeratorID int64 `json:"mod,omitempty"`
CanBeAppealed bool `json:"appeal_possible,omitempty"`
}
// QUERY PARAMETERS
type ParamRandomNSFW struct {
GIFsOnly bool `url:"gifs,omitempty"`
}
type ParamWikiHow struct {
NSFW bool `url:"nsfw,omitempty"`
}
type ParamRandomReddit struct {
SubReddit string
Options OptionalRandomReddit
}
type OptionalRandomReddit struct {
RemoveNSFW bool `url:"remove_nsfw,omitempty"`
Span string `url:"span,omitempty"`
}
type ParamRandomImage struct {
Tag string `url:"tag"`
NSFW bool `url:"nsfw,omitempty"`
}
type ParamBans struct {
UserID string `url:"user"`
}
type ParamDeleteBan struct {
User int64 `url:"user"`
Force bool `url:"force,omitempty"`
}
type ParamListBans struct {
Page int64 `url:"page,omitempty"`
PerPage int `url:"per_page,omitempty"`
}
type ParamAdvWeather struct {
Latitude float64
Longitude float64
ReportType string
Options OptionalAdvWeather
}
type OptionalAdvWeather struct {
Units string `url:"units,omitempty"`
Lang string `url:"lang,omitempty"`
Icons string `url:"icons,omitempty"`
}
type ParamIP struct {
IP string `url:"ip"`
}
type ParamCurrency struct {
CurrencyFrom string `url:"from"`
CurrencyTo string `url:"to"`
Value float64 `url:"value"`
}
type ParamGIS struct {
Location string `url:"q"`
Fast bool `url:"fast,omitempty"`
More bool `url:"more,omitempty"`
MapZoom int `url:"map_zoom,omitempty"`
IncludeMap bool `url:"include_map,omitempty"`
}
type ParamSearchLyrics struct {
Query string `url:"q"`
TextOnly bool `url:"text_only,omitempty"`
Limit int `url:"limit,omitempty"`
}
// TODO: Separate report type and
type ParamWeather struct {
Location string
ReportType string
Units string
Lang string
Icons string
}
const (
ErrCodeMissingParameters = 123
ErrCodeInvalidValue = 124
ErrCodeAlreadyExists = 125
)
func dumpAsValues(m map[string]interface{}) (data url.Values) {
data = url.Values{}
for k, v := range m {
switch v.(type) {
case float64:
in, _ := ParseFloat(fmt.Sprintf("%v", v))
data.Set(k, strconv.FormatFloat(in, 'f', -1, 64))
break
default:
data.Set(k, fmt.Sprintf("%v", v))
}
}
return
}
/**
* Thanks bwmarrin
**/
type RESTError struct {
Request *http.Request
Response *http.Response
ResponseBody []byte
Message *APIErrorMessage
}
func newRestError(req *http.Request, resp *http.Response, body []byte) *RESTError {
restErr := &RESTError{
Request: req,
Response: resp,
ResponseBody: body,
}
var msg *APIErrorMessage
err := json.Unmarshal(body, &msg)
if err == nil {
restErr.Message = msg
}
return restErr
}
func (r RESTError) Error() string {
return "HTTP " + r.Response.Status + ", " + string(r.ResponseBody)
}
/*
* Thanks yyscamper (https://gist.github.com/yyscamper/5657c360fadd6701580f3c0bcca9f63a)
*/
func ParseFloat(str string) (float64, error) {
val, err := strconv.ParseFloat(str, 64)
if err == nil {
return val, nil
}
//Some number may be seperated by comma, for example, 23,120,123, so remove the comma firstly
str = strings.Replace(str, ",", "", -1)
//Some number is specifed in scientific notation
pos := strings.IndexAny(str, "eE")
if pos < 0 {
return strconv.ParseFloat(str, 64)
}
var baseVal float64
var expVal int64
baseStr := str[0:pos]
baseVal, err = strconv.ParseFloat(baseStr, 64)
if err != nil {
return 0, err
}
expStr := str[(pos + 1):]
expVal, err = strconv.ParseInt(expStr, 10, 64)
if err != nil {
return 0, err
}
return baseVal * math.Pow10(int(expVal)), nil
}