-
Notifications
You must be signed in to change notification settings - Fork 0
/
ksoft.go
383 lines (323 loc) · 8.91 KB
/
ksoft.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
package ksoftgo
import (
"encoding/json"
"fmt"
"log"
"net/http"
"runtime"
"strings"
"time"
)
// Following https://semver.org/
const VERSION string = "2.0.0"
// New creates a new KSoft instance.
func New(token string) (s *KSession, err error) {
s = &KSession{
Client: &http.Client{Timeout: 30 * time.Second},
UserAgent: "KSoftgo (https://github.com/KSoft-Si/KSoftgo, v" + VERSION + ")",
}
if token == "" {
err = fmt.Errorf("no token provided")
return
} else {
s.Token = token
}
return
}
// Get a random image
// Example:
// image, err := ksession.RandomImage(kosftgo.ParamRandomImage{Tag: "doge"})
func (s *KSession) RandomImage(tag ParamRandomImage) (i Image, err error) {
i = Image{}
res, err := s.request("GET", EndpointMemeRandomImage(tag), nil)
if err != nil {
return
}
err = json.Unmarshal(res, &i)
return
}
// Get a random meme
// Example:
// reddit, err := ksession.RandomMeme()
func (s *KSession) RandomMeme() (r Reddit, err error) {
r = Reddit{}
res, err := s.request("GET", EndpointMemeRandomMeme, nil)
if err != nil {
return
}
err = json.Unmarshal(res, &r)
return
}
// Get a random picture that makes you say awwwww
// Example:
// reddit, err := ksession.RandomAww()
func (s *KSession) RandomAww() (reddit Reddit, err error) {
reddit = Reddit{}
res, err := s.request("GET", EndpointMemeRandomAww, nil)
if err != nil {
return
}
err = json.Unmarshal(res, &reddit)
return
}
// Get a random reddit post
// Example:
// reddit, err := ksession.RandomReddit(ksoftgo.ParamRandomReddit{SubReddit: "memes", Options: ksoftgo.OptionalRandomReddit{Span: "month"}})
func (s *KSession) RandomReddit(param ParamRandomReddit) (reddit Reddit, err error) {
reddit = Reddit{}
res, err := s.request("GET", EndpointMemeRandomReddit(param), nil)
if err != nil {
return
}
err = json.Unmarshal(res, &reddit)
return
}
// Get a random NSFW post
// Example:
// reddit, err := ksession.RandomNSFW()
func (s *KSession) RandomNSFW() (reddit Reddit, err error) {
reddit = Reddit{}
res, err := s.request("GET", EndpointMemeRandomNSFW(ParamRandomNSFW{}), nil)
if err != nil {
return
}
err = json.Unmarshal(res, &reddit)
return
}
// Get a random NSFW post with options
// Example:
// reddit, err := ksession.RandomNSFW(ksoftgo.ParamRandomNSFW{GIFsOnly: true})
func (s *KSession) RandomNSFWOptions(options ParamRandomNSFW) (reddit Reddit, err error) {
reddit = Reddit{}
res, err := s.request("GET", EndpointMemeRandomNSFW(options), nil)
if err != nil {
return
}
err = json.Unmarshal(res, &reddit)
return
}
// Get a random WikiHow article
// Example:
// image, err := ksession.RandomWikiHow()
func (s *KSession) RandomWikiHow() (i WikiHowImage, err error) {
i = WikiHowImage{}
res, err := s.request("GET", EndpointMemeWikihow(ParamWikiHow{}), nil)
if err != nil {
return
}
err = json.Unmarshal(res, &i)
return
}
// Get a random WikiHow article with options
// Example:
// image, err := ksession.RandomWikiHow(ksoftgo.ParamWikiHow{NSFW: true})
func (s *KSession) RandomWikiHowOptions(options ParamWikiHow) (i WikiHowImage, err error) {
i = WikiHowImage{}
res, err := s.request("GET", EndpointMemeWikihow(options), nil)
if err != nil {
return
}
err = json.Unmarshal(res, &i)
return
}
// Get an image by it's snowflake
// Example:
// image, err := ksession.ImageBySnowflake("i-ix63ra_m-12")
func (s *KSession) ImageBySnowflake(snowflake string) (i Image, err error) {
i = Image{}
res, err := s.request("GET", EndpointMemeImage(snowflake), nil)
if err != nil {
return
}
err = json.Unmarshal(res, &i)
return
}
// Get tags
// Example:
// tags, err := ksession.GetTags()
func (s *KSession) GetTags() (tags Tags, err error) {
tags = Tags{}
res, err := s.request("GET", EndpointMemeTags, nil)
if err != nil {
return
}
err = json.Unmarshal(res, &tags)
return
}
// Add a ban to the ban list
// Example:
// err := ksession.AddBan(ksoftgo.ParamAddBan{ID: 0000000000000000, Reason: "bad guy", Proof: "imgur.com"})
func (s *KSession) AddBan(info ParamAddBan) (err error) {
jsonMap := make(map[string]interface{})
data, err := json.Marshal(info)
err = json.Unmarshal(data, &jsonMap)
if err != nil {
return
}
err = s.PostForm(EndpointBansAdd, dumpAsValues(jsonMap))
return
}
// Get ban info
// Example:
// baninfo, err := ksession.GetBanInfo(ksoftgo.ParamBans{UserID: "123456789123456789"})
func (s *KSession) GetBanInfo(param ParamBans) (info BanInfo, err error) {
info = BanInfo{}
res, err := s.request("GET", EndpointBansInfo(param), nil)
if err != nil {
return
}
err = json.Unmarshal(res, &info)
return
}
// Check user
// Example:
// isbanned, err := ksession.CheckBan(ksoftgo.ParamBans{UserID: "123456789123456789"})
func (s *KSession) CheckBan(param ParamBans) (c bool, err error) {
res, err := s.request("GET", EndpointBansCheck(param), nil)
if err != nil {
return
}
bc := BanCheck{}
err = json.Unmarshal(res, &bc)
return bc.Banned, err
}
// Delete ban
// Example:
// ksession.DeleteBan(ksoftgo.ParamDeleteBan{User: "123456789123456789", Force: false})
func (s *KSession) DeleteBan(delete ParamDeleteBan) {
_, err := s.request("DELETE", EndpointBansDelete(delete), nil)
if err != nil {
return
}
}
// List of bans
// Example:
// banlist, err := ksession.GetBans(ksoftgo.ParamListBans{Page: 1})
func (s *KSession) GetBans(param ParamListBans) (banlist BansList, err error) {
banlist = BansList{}
res, err := s.request("GET", EndpointBansList(param), nil)
if err != nil {
return
}
err = json.Unmarshal(res, &banlist)
return
}
// Search for locations and get maps
// Example:
// gis, err := ksession.GetGis(ksoftgo.ParamGIS{Location: "Montreal"})
func (s *KSession) GetGIS(params ParamGIS) (gis GIS, err error) {
gis = GIS{}
res, err := s.request("GET", EndpointKumoGis(params), nil)
if err != nil {
return
}
err = json.Unmarshal(res, &gis)
return
}
// Weather - easy
// Example:
// weather, err := ksession.GetWeather(ksoftgo.ParamWeather{Location: "Montreal", ReportType: "currently"})
func (s *KSession) GetWeather(params ParamWeather) (weather Weather, err error) {
weather = Weather{}
res, err := s.request("GET", EndpointKumoWeather(params), nil)
if err != nil {
return
}
err = json.Unmarshal(res, &weather)
return
}
// Weather - advanced
// Example:
// weather, err := ksession.GetAdvWeather(ksoftgo.ParamAdvWeather{Latitude: 0.0, Longitude: 0.0, ReportType: "currently"})
func (s *KSession) GetAdvWeather(params ParamAdvWeather) (weather Weather, err error) {
weather = Weather{}
res, err := s.request("GET", EndpointKumoWeatherAdv(params), nil)
if err != nil {
return
}
err = json.Unmarshal(res, &weather)
return
}
// GeoIP
// Example:
// geoip, err := ksession.GeoIP(ksoftgo.ParamIP{IP: "8.8.8.8"})
func (s *KSession) GeoIP(param ParamIP) (geoip GeoIP, err error) {
geoip = GeoIP{}
res, err := s.request("GET", EndpointKumoGeoIP(param), nil)
if err != nil {
return
}
err = json.Unmarshal(res, &geoip)
return
}
// Currency conversion
// Example:
// currency, err := ksession.CurrenyConversion(ksoftgo.ParamCurrency{FromCurrency: "USD", ToCurrency: "EUR", Value: 1.50})
func (s *KSession) CurrencyConversion(param ParamCurrency) (curr Currency, err error) {
curr = Currency{}
res, err := s.request("GET", EndpointKumoCurrency(param), nil)
if err != nil {
return
}
err = json.Unmarshal(res, &curr)
return
}
// Get lyrics
// Example:
// lyricssearch, err := ksession.SearchLyrics(ksoftgo.ParamSearchLyrics{Query: "Rick never gonna give you up"})
func (s *KSession) SearchLyrics(param ParamSearchLyrics) (results LyricsSearch, err error) {
results = LyricsSearch{}
res, err := s.request("GET", EndpointLyricsSearch(param), nil)
if err != nil {
return
}
err = json.Unmarshal(res, &results)
return
}
// Get artist by ID
// Example:
// artist, err := ksession.GetArtist(628942)
func (s *KSession) GetArtist(id int64) (results Artist, err error) {
results = Artist{}
res, err := s.request("GET", EndpointLyricsArtist(id), nil)
if err != nil {
return
}
err = json.Unmarshal(res, &results)
return
}
// Get album by ID
// Example:
// album, err := ksession.GetAlbum(88287)
func (s *KSession) GetAlbum(id int64) (results Album, err error) {
results = Album{}
res, err := s.request("GET", EndpointLyricsAlbum(id), nil)
if err != nil {
return
}
err = json.Unmarshal(res, &results)
return
}
// Get track by ID
// Example:
// track, err := ksession.GetTrack(680639)
func (s *KSession) GetTrack(id int64) (results Track, err error) {
results = Track{}
res, err := s.request("GET", EndpointLyricsTrack(id), nil)
if err != nil {
return
}
err = json.Unmarshal(res, &results)
return
}
func (s *KSession) log(caller int, format string, a ...interface{}) {
pc, file, line, ok := runtime.Caller(caller)
fmt.Println(ok)
files := strings.Split(file, "/")
file = files[len(files)-1]
name := runtime.FuncForPC(pc).Name()
fns := strings.Split(name, ".")
name = fns[len(fns)-1]
msg := fmt.Sprintf(format, a...)
log.Printf("[KSoft] %s:%d:%s() %s\n", file, line, name, msg)
}