-
Notifications
You must be signed in to change notification settings - Fork 13
/
submissions.go
148 lines (138 loc) · 4.43 KB
/
submissions.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
package mira
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"regexp"
"strconv"
"github.com/thecsw/mira/v4/models"
)
// 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 subredditType:
return c.getSubredditPosts(name, sort, tdur, limit)
case redditorType:
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 subredditType:
return c.getSubredditPostsAfter(name, last, limit)
case redditorType:
return c.getRedditorPostsAfter(name, last, limit)
default:
return nil, fmt.Errorf("'%s' type does not have an option for submissions", ttype)
}
}
// 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(commentType)
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(commentType)
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(http.MethodGet, 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 "", fmt.Errorf("exceeded the maximum number of iterations: %v",
c.Values.GetSubmissionFromCommentTries)
}
}
return current, nil
}
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(http.MethodGet, 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(http.MethodGet, 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(http.MethodGet, target, map[string]string{
"limit": strconv.Itoa(limit),
"t": tdur,
})
ret := &models.PostListing{}
json.Unmarshal(ans, ret)
return ret.GetChildren(), err
}
func (c *Reddit) getSubredditPostsAfter(sr string, last string, limit int) ([]models.PostListingChild, error) {
target := RedditOauth + "/r/" + sr + "/new.json"
ans, err := c.MiraRequest(http.MethodGet, target, map[string]string{
"limit": strconv.Itoa(limit),
"before": last,
})
ret := &models.PostListing{}
json.Unmarshal(ans, ret)
return ret.GetChildren(), err
}