forked from turnage/graw
/
scanner.go
63 lines (57 loc) · 1.97 KB
/
scanner.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
package reddit
// deletedAuthor is the author field of deleted posts on Reddit.
const deletedAuthor = "[deleted]"
// Scanner defines a low level interface for fetching reading Reddit listings.
type Scanner interface {
// Listing returns a harvest from a listing endpoint at Reddit.
//
// There are many things to consider when using this. A listing on
// Reddit is like an infinite, unstable list. It stretches functionally
// infinitely forward and backward in time, and elements from it vanish
// over time for many reasons (users delete their posts, posts get
// caught in a spam filter, mods remove them, etc).
//
// The "after" parameter is the name (in the form tx_xxxxxx, found by
// the Name field of any Reddit struct defined in this package) of an
// element known to be in the list. If an empty string the latest 100
// elements are returned.
//
// The way to subscribe to a listing is continually poll this, and keep
// track of your reference point, and replace it if it gets deleted or
// dropped from the listing for any reason, which is nontrivial. If your
// reference point becomes invalid, you will get no elements in the
// harvest and have no way to find your place unless you planned ahead.
//
// If you want a stream where all of this is handled for you, see graw
// or graw/streams.
Listing(path, after string) (Harvest, error)
ListingWithParams(path string, params map[string]string) (Harvest, error)
}
type scanner struct {
r reaper
}
func newScanner(r reaper) Scanner {
return &scanner{r: r}
}
func (s *scanner) Listing(path, after string) (Harvest, error) {
return s.r.reap(
path, map[string]string{
"raw_json": "1",
"limit": "100",
"before": after,
},
)
}
func (s *scanner) ListingWithParams(path string, params map[string]string) (
Harvest,
error,
) {
reaperParams := map[string]string{
"raw_json": "1",
"limit": "100",
}
for key, value := range params {
reaperParams[key] = value
}
return s.r.reap_post(path, reaperParams)
}