forked from turnage/graw
/
reaper.go
185 lines (159 loc) · 3.75 KB
/
reaper.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
package reddit
import (
"net/http"
"net/url"
"strings"
"sync"
"time"
)
var (
// scheme is a map of TLS=[true|false] to the scheme for that setting.
scheme = map[bool]string{
true: "https",
false: "http",
}
formEncoding = map[string][]string{
"content-type": {"application/x-www-form-urlencoded"},
}
)
type reaperConfig struct {
client client
parser parser
hostname string
reapSuffix string
tls bool
rate time.Duration
}
// reaper is a high level api for Reddit HTTP requests.
type reaper interface {
// reap executes a GET request to Reddit and returns the elements from
// the endpoint.
reap(path string, values map[string]string) (Harvest, error)
// executes a POST request but evaluates as a GET
reap_post(path string, values map[string]string) (Harvest, error)
// sow executes a POST request to Reddit.
sow(path string, values map[string]string) error
// get_sow executes a POST request to Reddit
// and returns the response, usually the posted item
get_sow(path string, values map[string]string) (Submission, error)
}
type reaperImpl struct {
cli client
parser parser
hostname string
reapSuffix string
scheme string
rate time.Duration
last time.Time
mu *sync.Mutex
}
func newReaper(c reaperConfig) reaper {
return &reaperImpl{
cli: c.client,
parser: c.parser,
hostname: c.hostname,
reapSuffix: c.reapSuffix,
scheme: scheme[c.tls],
rate: c.rate,
mu: &sync.Mutex{},
}
}
func (r *reaperImpl) reap(path string, values map[string]string) (Harvest, error) {
r.rateBlock()
resp, err := r.cli.Do(
&http.Request{
Method: "GET",
URL: r.url(r.path(path, r.reapSuffix), values),
Host: r.hostname,
},
)
if err != nil {
return Harvest{}, err
}
comments, posts, messages, mores, err := r.parser.parse(resp)
return Harvest{
Comments: comments,
Posts: posts,
Messages: messages,
Mores: mores,
}, err
}
func (r *reaperImpl) reap_post(path string, values map[string]string) (Harvest, error) {
r.rateBlock()
values["api_type"] = "json"
resp, err := r.cli.Do(
&http.Request{
Method: "POST",
Host: r.hostname,
Header: formEncoding,
URL: r.url(path, values),
},
)
if err != nil {
return Harvest{}, err
}
comments, posts, messages, mores, err := r.parser.parse(resp)
return Harvest{
Comments: comments,
Posts: posts,
Messages: messages,
Mores: mores,
}, nil
}
func (r *reaperImpl) sow(path string, values map[string]string) error {
r.rateBlock()
_, err := r.cli.Do(
&http.Request{
Method: "POST",
Header: formEncoding,
Host: r.hostname,
URL: r.url(path, values),
},
)
return err
}
func (r *reaperImpl) get_sow(path string, values map[string]string) (Submission, error) {
r.rateBlock()
values["api_type"] = "json"
resp, err := r.cli.Do(
&http.Request{
Method: "POST",
Header: formEncoding,
Host: r.hostname,
URL: r.url(path, values),
},
)
if err != nil {
return Submission{}, err
}
return r.parser.parse_submitted(resp)
}
func (r *reaperImpl) rateBlock() {
r.mu.Lock()
defer r.mu.Unlock()
if time.Since(r.last) < r.rate {
<-time.After(r.last.Add(r.rate).Sub(time.Now()))
}
r.last = time.Now()
}
func (r *reaperImpl) url(path string, values map[string]string) *url.URL {
return &url.URL{
Scheme: r.scheme,
Host: r.hostname,
Path: path,
RawQuery: r.formatValues(values).Encode(),
}
}
func (r *reaperImpl) path(p string, suff string) string {
if strings.HasSuffix(p, suff) {
return p
}
return p + suff
}
func (r *reaperImpl) formatValues(values map[string]string) url.Values {
formattedValues := url.Values{}
for key, value := range values {
formattedValues[key] = []string{value}
}
return formattedValues
}