forked from thecsw/mira
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reddit.go
755 lines (697 loc) · 20.8 KB
/
reddit.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
package mira
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"github.com/Moonlight-io/mira/models"
)
// Surely, Reddit API is always developing and I can't implement all endpoints.
// It will be a bit of a bloat. Instead, you have accessto *Reddit.MiraRequest
// method that will let you to do any custom reddit api calls!
//
// Here is the signature:
//
// func (c *Reddit) MiraRequest(method string, target string, payload map[string]string) ([]byte, error) {...}
//
// It is pretty straight-forward. The return is a slice of bytes. Parse it yourself.
func (c *Reddit) MiraRequest(method string, target string, payload map[string]string) ([]byte, error) {
values := "?"
for i, v := range payload {
v = url.QueryEscape(v)
values += fmt.Sprintf("%s=%s&", i, v)
}
values = values[:len(values)-1]
r, err := http.NewRequest(method, target+values, nil)
if err != nil {
return nil, err
}
r.Header.Set("User-Agent", c.Creds.UserAgent)
r.Header.Set("Authorization", "Bearer "+c.Token)
response, err := c.Client.Do(r)
if err != nil {
return nil, err
}
defer response.Body.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(response.Body)
data := buf.Bytes()
if err := findRedditError(data); err != nil {
return nil, err
}
return data, nil
}
// Me pushes a new Redditor value
func (c *Reddit) Me() *Reddit {
return c.addQueue(c.Creds.Username, "me")
}
// Subreddit pushes a new subreddit value to the internal queue
func (c *Reddit) Subreddit(name ...string) *Reddit {
return c.addQueue(strings.Join(name, "+"), "subreddit")
}
// Submission pushes a new submission value to the internal queue
func (c *Reddit) Submission(name string) *Reddit {
return c.addQueue(name, "submission")
}
// Comment pushes a new comment value to the internal queue
func (c *Reddit) Comment(name string) *Reddit {
return c.addQueue(name, "comment")
}
// Redditor pushes a new redditor value to the internal queue
func (c *Reddit) Redditor(name string) *Reddit {
return c.addQueue(name, "redditor")
}
// Submissions returns submissions from a subreddit up to a specified limit sorted by the given parameters
//
// Sorting options: "hot", "new", "top", "rising", "controversial", "random"
//
// Duration options: "hour", "day", "week", "year", "all"
//
// Limit is any numerical value, so 0 <= limit <= 100
func (c *Reddit) Submissions(sort string, tdur string, limit int) ([]models.PostListingChild, error) {
name, ttype := c.getQueue()
switch ttype {
case "subreddit":
return c.getSubredditPosts(name, sort, tdur, limit)
case "redditor":
return c.getRedditorPosts(name, sort, tdur, limit)
default:
return nil, fmt.Errorf("'%s' type does not have an option for submissions", ttype)
}
}
// SubmissionsAfter returns new submissions from a subreddit
//
// Last is the anchor of a submission id
//
// Limit is any numerical value, so 0 <= limit <= 100
func (c *Reddit) SubmissionsAfter(last string, limit int) ([]models.PostListingChild, error) {
name, ttype := c.getQueue()
switch ttype {
case "subreddit":
return c.getSubredditPostsAfter(name, last, limit)
case "redditor":
return c.getRedditorPostsAfter(name, last, limit)
default:
return nil, fmt.Errorf("'%s' type does not have an option for submissions", ttype)
}
}
// Comments returns comments from a subreddit up to a specified limit sorted by the given parameters
//
// Sorting options: "hot", "new", "top", "rising", "controversial", "random"
//
// Duration options: "hour", "day", "week", "year", "all"
//
// Limit is any numerical value, so 0 <= limit <= 100
func (c *Reddit) Comments(sort string, tdur string, limit int) ([]models.Comment, error) {
name, ttype := c.getQueue()
switch ttype {
case "subreddit":
return c.getSubredditComments(name, sort, tdur, limit)
case "submission":
comments, _, err := c.getSubmissionComments(name, sort, tdur, limit)
if err != nil {
return nil, err
}
return comments, nil
case "redditor":
return c.getRedditorComments(name, sort, tdur, limit)
default:
return nil, fmt.Errorf("'%s' type does not have an option for comments", ttype)
}
}
// CommentsAfter returns new comments from a subreddit
//
// Last is the anchor of a comment id
//
// Limit is any numerical value, so 0 <= limit <= 100
func (c *Reddit) CommentsAfter(sort string, last string, limit int) ([]models.Comment, error) {
name, ttype := c.getQueue()
switch ttype {
case "subreddit":
return c.getSubredditCommentsAfter(name, sort, last, limit)
case "redditor":
return c.getRedditorCommentsAfter(name, sort, last, limit)
default:
return nil, fmt.Errorf("'%s' type does not have an option for comments", ttype)
}
}
// Info returns MiraInterface of last pushed object
func (c *Reddit) Info() (MiraInterface, error) {
name, ttype := c.getQueue()
switch ttype {
case "me":
return c.getMe()
case "submission":
return c.getSubmission(name)
case "comment":
return c.getComment(name)
case "subreddit":
return c.getSubreddit(name)
case "redditor":
return c.getUser(name)
default:
return nil, fmt.Errorf("returning type is not defined")
}
}
func (c *Reddit) getMe() (models.Me, error) {
target := RedditOauth + "/api/v1/me"
ret := &models.Me{}
ans, err := c.MiraRequest("GET", target, nil)
if err != nil {
return *ret, err
}
json.Unmarshal(ans, ret)
return *ret, nil
}
func (c *Reddit) getSubmission(id string) (models.PostListingChild, error) {
target := RedditOauth + "/api/info.json"
ans, err := c.MiraRequest("GET", target, map[string]string{
"id": id,
})
ret := &models.PostListing{}
json.Unmarshal(ans, ret)
if len(ret.GetChildren()) < 1 {
return models.PostListingChild{}, fmt.Errorf("id not found")
}
return ret.GetChildren()[0], err
}
func (c *Reddit) getComment(id string) (models.Comment, error) {
target := RedditOauth + "/api/info.json"
ans, err := c.MiraRequest("GET", target, map[string]string{
"id": id,
})
ret := &models.CommentListing{}
json.Unmarshal(ans, ret)
if len(ret.GetChildren()) < 1 {
return models.Comment{}, fmt.Errorf("id not found")
}
return ret.GetChildren()[0], err
}
// ExtractSubmission extracts submission id from last pushed object
// does not make an api call like .Root(), use this instead
func (c *Reddit) ExtractSubmission() (string, error) {
name, _, err := c.checkType("comment")
if err != nil {
return "", err
}
info, err := c.Comment(name).Info()
if err != nil {
return "", err
}
link := info.GetUrl()
reg := regexp.MustCompile(`comments/([^/]+)/`)
res := reg.FindStringSubmatch(link)
if len(res) < 1 {
return "", errors.New("couldn't extract submission id")
}
return "t3_" + res[1], nil
}
// Root will return the submission id of a comment
// Very expensive on API calls. Please use .ExtractSubmission() instead
func (c *Reddit) Root() (string, error) {
name, _, err := c.checkType("comment")
if err != nil {
return "", err
}
current := name
// Not a comment passed
if string(current[1]) != "1" {
return "", errors.New("the passed ID is not a comment")
}
target := RedditOauth + "/api/info.json"
temp := models.CommentListing{}
tries := 0
for string(current[1]) != "3" {
ans, err := c.MiraRequest("GET", target, map[string]string{
"id": current,
})
if err != nil {
return "", err
}
json.Unmarshal(ans, &temp)
if len(temp.Data.Children) < 1 {
return "", errors.New("could not find the requested comment")
}
current = temp.Data.Children[0].GetParentId()
tries++
if tries > c.Values.GetSubmissionFromCommentTries {
return "", errors.New(fmt.Sprintf("Exceeded the maximum number of iterations: %v", c.Values.GetSubmissionFromCommentTries))
}
}
return current, nil
}
func (c *Reddit) getUser(name string) (models.Redditor, error) {
target := RedditOauth + "/user/" + name + "/about"
ans, err := c.MiraRequest("GET", target, nil)
ret := &models.Redditor{}
json.Unmarshal(ans, ret)
return *ret, err
}
func (c *Reddit) getSubreddit(name string) (models.Subreddit, error) {
target := RedditOauth + "/r/" + name + "/about"
ans, err := c.MiraRequest("GET", target, nil)
ret := &models.Subreddit{}
json.Unmarshal(ans, ret)
return *ret, err
}
func (c *Reddit) getRedditorPosts(user string, sort string, tdur string, limit int) ([]models.PostListingChild, error) {
target := RedditOauth + "/u/" + user + "/submitted/" + sort + ".json"
ans, err := c.MiraRequest("GET", target, map[string]string{
"limit": strconv.Itoa(limit),
"t": tdur,
})
ret := &models.PostListing{}
json.Unmarshal(ans, ret)
return ret.GetChildren(), err
}
func (c *Reddit) getRedditorPostsAfter(user string, last string, limit int) ([]models.PostListingChild, error) {
target := RedditOauth + "/u/" + user + "/submitted/new.json"
ans, err := c.MiraRequest("GET", target, map[string]string{
"limit": strconv.Itoa(limit),
"before": last,
})
ret := &models.PostListing{}
json.Unmarshal(ans, ret)
return ret.GetChildren(), err
}
func (c *Reddit) getSubredditPosts(sr string, sort string, tdur string, limit int) ([]models.PostListingChild, error) {
target := RedditOauth + "/r/" + sr + "/" + sort + ".json"
ans, err := c.MiraRequest("GET", target, map[string]string{
"limit": strconv.Itoa(limit),
"t": tdur,
})
ret := &models.PostListing{}
json.Unmarshal(ans, ret)
return ret.GetChildren(), err
}
func (c *Reddit) getSubredditComments(sr string, sort string, tdur string, limit int) ([]models.Comment, error) {
target := RedditOauth + "/r/" + sr + "/comments.json"
ans, err := c.MiraRequest("GET", target, map[string]string{
"sort": sort,
"limit": strconv.Itoa(limit),
"t": tdur,
})
ret := &models.CommentListing{}
json.Unmarshal(ans, ret)
return ret.GetChildren(), err
}
func (c *Reddit) getRedditorComments(user string, sort string, tdur string, limit int) ([]models.Comment, error) {
target := RedditOauth + "/u/" + user + "/comments.json"
ans, err := c.MiraRequest("GET", target, map[string]string{
"sort": sort,
"limit": strconv.Itoa(limit),
"t": tdur,
})
ret := &models.CommentListing{}
json.Unmarshal(ans, ret)
return ret.GetChildren(), err
}
func (c *Reddit) getRedditorCommentsAfter(user string, sort string, last string, limit int) ([]models.Comment, error) {
target := RedditOauth + "/u/" + user + "/comments.json"
ans, err := c.MiraRequest("GET", target, map[string]string{
"sort": sort,
"limit": strconv.Itoa(limit),
"before": last,
})
ret := &models.CommentListing{}
json.Unmarshal(ans, ret)
return ret.GetChildren(), err
}
func (c *Reddit) getSubmissionComments(post_id string, sort string, tdur string, limit int) ([]models.Comment, []string, error) {
if string(post_id[1]) != "3" {
return nil, nil, errors.New("the passed ID36 is not a submission")
}
target := RedditOauth + "/comments/" + post_id[3:]
ans, err := c.MiraRequest("GET", target, map[string]string{
"sort": sort,
"limit": strconv.Itoa(limit),
"showmore": strconv.FormatBool(true),
"t": tdur,
})
if err != nil {
return nil, nil, err
}
temp := make([]models.CommentListing, 0, 8)
json.Unmarshal(ans, &temp)
ret := make([]models.Comment, 0, 8)
for _, v := range temp {
comments := v.GetChildren()
for _, v2 := range comments {
ret = append(ret, v2)
}
}
// Cut off the "more" kind
children := ret[len(ret)-1].Data.Children
ret = ret[:len(ret)-1]
return ret, children, nil
}
func (c *Reddit) getSubredditPostsAfter(sr string, last string, limit int) ([]models.PostListingChild, error) {
target := RedditOauth + "/r/" + sr + "/new.json"
ans, err := c.MiraRequest("GET", target, map[string]string{
"limit": strconv.Itoa(limit),
"before": last,
})
ret := &models.PostListing{}
json.Unmarshal(ans, ret)
return ret.GetChildren(), err
}
func (c *Reddit) getSubredditCommentsAfter(sr string, sort string, last string, limit int) ([]models.Comment, error) {
target := RedditOauth + "/r/" + sr + "/comments.json"
ans, err := c.MiraRequest("GET", target, map[string]string{
"sort": sort,
"limit": strconv.Itoa(limit),
"before": last,
})
ret := &models.CommentListing{}
json.Unmarshal(ans, ret)
return ret.GetChildren(), err
}
// Submit submits a submission to a subreddit
func (c *Reddit) Submit(title string, text string) (models.Submission, error) {
ret := &models.Submission{}
name, _, err := c.checkType("subreddit")
if err != nil {
return *ret, err
}
target := RedditOauth + "/api/submit"
ans, err := c.MiraRequest("POST", target, map[string]string{
"title": title,
"sr": name,
"text": text,
"kind": "self",
"resubmit": "true",
"api_type": "json",
})
json.Unmarshal(ans, ret)
return *ret, err
}
// Reply replies to a comment with text
func (c *Reddit) Reply(text string) (models.CommentWrap, error) {
ret := &models.CommentWrap{}
name, _, err := c.checkType("comment")
if err != nil {
return *ret, err
}
target := RedditOauth + "/api/comment"
ans, err := c.MiraRequest("POST", target, map[string]string{
"text": text,
"thing_id": name,
"api_type": "json",
})
json.Unmarshal(ans, ret)
return *ret, err
}
// ReplyWithID is the same as Reply but with explicit passing comment id
func (c *Reddit) ReplyWithID(name, text string) (models.CommentWrap, error) {
ret := &models.CommentWrap{}
target := RedditOauth + "/api/comment"
ans, err := c.MiraRequest("POST", target, map[string]string{
"text": text,
"thing_id": name,
"api_type": "json",
})
json.Unmarshal(ans, ret)
return *ret, err
}
// Save posts a comment to a submission
func (c *Reddit) Save(text string) (models.CommentWrap, error) {
ret := &models.CommentWrap{}
name, _, err := c.checkType("submission")
if err != nil {
return *ret, err
}
target := RedditOauth + "/api/comment"
ans, err := c.MiraRequest("POST", target, map[string]string{
"text": text,
"thing_id": name,
"api_type": "json",
})
json.Unmarshal(ans, ret)
return *ret, err
}
// SaveWithID is the same as Save but with explicitely passing
func (c *Reddit) SaveWithID(name, text string) (models.CommentWrap, error) {
ret := &models.CommentWrap{}
target := RedditOauth + "/api/comment"
ans, err := c.MiraRequest("POST", target, map[string]string{
"text": text,
"thing_id": name,
"api_type": "json",
})
json.Unmarshal(ans, ret)
return *ret, err
}
// Delete deletes whatever is next in the queue
func (c *Reddit) Delete() error {
name, _, err := c.checkType("comment", "submission")
if err != nil {
return err
}
target := RedditOauth + "/api/del"
_, err = c.MiraRequest("POST", target, map[string]string{
"id": name,
"api_type": "json",
})
return err
}
// Approve is a mod tool to approve a comment or a submission
// Will fail if not a mod
func (c *Reddit) Approve() error {
name, _, err := c.checkType("comment")
if err != nil {
return err
}
target := RedditOauth + "/api/approve"
_, err = c.MiraRequest("POST", target, map[string]string{
"id": name,
"api_type": "json",
})
return err
}
// Distinguish is a mod tool to distinguish a comment or a submission
// Will fail if not a mod
func (c *Reddit) Distinguish(how string, sticky bool) error {
name, _, err := c.checkType("comment")
if err != nil {
return err
}
target := RedditOauth + "/api/distinguish"
_, err = c.MiraRequest("POST", target, map[string]string{
"id": name,
"how": how,
"sticky": strconv.FormatBool(sticky),
"api_type": "json",
})
return err
}
// Edit will edit the next queued comment
func (c *Reddit) Edit(text string) (models.CommentWrap, error) {
ret := &models.CommentWrap{}
name, _, err := c.checkType("comment", "submission")
if err != nil {
return *ret, err
}
target := RedditOauth + "/api/editusertext"
ans, err := c.MiraRequest("POST", target, map[string]string{
"text": text,
"thing_id": name,
"api_type": "json",
})
json.Unmarshal(ans, ret)
return *ret, err
}
// Compose will send a message to next redditor
func (c *Reddit) Compose(subject, text string) error {
name, _, err := c.checkType("redditor")
if err != nil {
return err
}
target := RedditOauth + "/api/compose"
_, err = c.MiraRequest("POST", target, map[string]string{
"subject": subject,
"text": text,
"to": name,
"api_type": "json",
})
return err
}
// ReadMessage marks the next comment/message as read
func (c *Reddit) ReadMessage(message_id string) error {
_, _, err := c.checkType("me")
if err != nil {
return err
}
target := RedditOauth + "/api/read_message"
_, err = c.MiraRequest("POST", target, map[string]string{
"id": message_id,
})
return err
}
// ReadAllMessages uses ReadMessage on all unread messages
func (c *Reddit) ReadAllMessages() error {
_, _, err := c.checkType("me")
if err != nil {
return err
}
target := RedditOauth + "/api/read_all_messages"
_, err = c.MiraRequest("POST", target, nil)
return err
}
// ListUnreadMessages returns a list of all unread messages
func (c *Reddit) ListUnreadMessages() ([]models.Comment, error) {
_, _, err := c.checkType("me")
if err != nil {
return nil, err
}
target := RedditOauth + "/message/unread"
ans, err := c.MiraRequest("GET", target, map[string]string{
"mark": "false",
})
ret := &models.CommentListing{}
json.Unmarshal(ans, ret)
return ret.GetChildren(), err
}
// UpdateSidebar updates subreddit's sidebar. Needs mod privileges
func (c *Reddit) UpdateSidebar(text string) error {
name, _, err := c.checkType("subreddit")
if err != nil {
return err
}
target := RedditOauth + "/api/site_admin"
_, err = c.MiraRequest("POST", target, map[string]string{
"sr": name,
"name": "None",
"description": text,
"title": name,
"wikimode": "anyone",
"link_type": "any",
"type": "public",
"api_type": "json",
})
return err
}
// UserFlair updates user's flair in a sub. Needs mod permissions
func (c *Reddit) UserFlair(user, text string) error {
name, _, err := c.checkType("subreddit")
if err != nil {
return err
}
target := RedditOauth + "/r/" + name + "/api/flair"
_, err = c.MiraRequest("POST", target, map[string]string{
"name": user,
"text": text,
"api_type": "json",
})
return err
}
// UserFlairv2 gets the list of flair from a subreddit
func (c *Reddit) UserFlairV2() ([]models.SubredditUserFlair, error) {
res := make([]models.SubredditUserFlair, 0)
name, _, err := c.checkType("subreddit")
if err != nil {
return res, err
}
target := RedditOauth + "/r/" + name + "/api/user_flair_v2"
if ans, err := c.MiraRequest("GET", target, nil); err != nil {
return res, err
} else {
err = json.Unmarshal(ans, &res)
return res, err
}
}
// UserFlairWithID is the same as UserFlair but explicit redditor name
func (c *Reddit) UserFlairWithID(name, user, text string) error {
target := RedditOauth + "/r/" + name + "/api/flair"
_, err := c.MiraRequest("POST", target, map[string]string{
"name": user,
"text": text,
"api_type": "json",
})
return err
}
// SelectFlair sets a submission flair
func (c *Reddit) SelectFlair(text string) error {
name, _, err := c.checkType("submission")
if err != nil {
return err
}
target := RedditOauth + "/api/selectflair"
_, err = c.MiraRequest("POST", target, map[string]string{
"link": name,
"text": text,
"api_type": "json",
})
return err
}
// SelectUserFlairTemplate sets a users flair to a template id
func (c *Reddit) SelectUserFlairTemplate(user, id, text string) error {
name, _, err := c.checkType("subreddit")
if err != nil {
return err
}
target := RedditOauth + "/r/" + name + "/api/selectflair"
_, err = c.MiraRequest("POST", target, map[string]string{
"api_type": "json",
"flair_template_id": id,
"name": user,
"text": text,
})
return err
}
// SelectFlairWithID sets submission flair with explicit ID
func (c *Reddit) SelectFlairWithID(name, text string) error {
target := RedditOauth + "/api/selectflair"
_, err := c.MiraRequest("POST", target, map[string]string{
"link": name,
"text": text,
"api_type": "json",
})
return err
}
func (c *Reddit) checkType(rtype ...string) (string, string, error) {
name, ttype := c.getQueue()
if name == "" {
return "", "", fmt.Errorf("identifier is empty")
}
if !findElem(ttype, rtype) {
return "", "", fmt.Errorf("the passed type is not a valid type for this call | expected: %s", strings.Join(rtype, ", "))
}
return name, ttype, nil
}
func (c *Reddit) addQueue(name string, ttype string) *Reddit {
c.Chain <- &ChainVals{Name: name, Type: ttype}
return c
}
func (c *Reddit) getQueue() (string, string) {
if len(c.Chain) < 1 {
return "", ""
}
temp := <-c.Chain
return temp.Name, temp.Type
}
func findElem(elem string, arr []string) bool {
for _, v := range arr {
if elem == v {
return true
}
}
return false
}
// RedditErr is a struct to store reddit error messages
type RedditErr struct {
Message string `json:"message"`
Error string `json:"error"`
}
func findRedditError(data []byte) error {
object := &RedditErr{}
json.Unmarshal(data, object)
if object.Message != "" || object.Error != "" {
return fmt.Errorf("%s | error code: %s", object.Message, object.Error)
}
return nil
}