forked from turnage/graw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.go
164 lines (148 loc) · 2.98 KB
/
scan.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
package graw
import (
"fmt"
"github.com/mix/graw/botfaces"
"github.com/mix/graw/reddit"
"github.com/mix/graw/streams"
)
var (
postHandlerErr = fmt.Errorf(
"You must implement PostHandler to handle subreddit feeds.",
)
commentHandlerErr = fmt.Errorf(
"You must implement CommentHandler to handle subreddit " +
"comment feeds.",
)
userHandlerErr = fmt.Errorf(
"You must implement UserHandler to handle user feeds.",
)
loggedOutErr = fmt.Errorf(
"You must be running as a logged in bot to get inbox feeds.",
)
)
// Scan connects any requested logged-out event sources to the given handler,
// making requests with the given script handle. It launches a goroutine for the
// scan. It returns two functions: a stop() function to stop the scan at any
// time, and a wait() function to block until the scan fails.
func Scan(handler interface{}, script reddit.Script, cfg Config) (
func(),
func() error,
error,
) {
kill := make(chan bool)
errs := make(chan error)
if cfg.PostReplies || cfg.CommentReplies || cfg.Mentions || cfg.Messages {
return nil, nil, loggedOutErr
}
if err := connectScanStreams(
handler,
script,
cfg,
kill,
errs,
); err != nil {
return nil, nil, err
}
return launch(handler, kill, errs, logger(cfg.Logger))
}
// connectScanStreams connects the streams a scanner can subscribe to to the
// handler.
func connectScanStreams(
handler interface{},
sc reddit.Scanner,
c Config,
kill <-chan bool,
errs chan<- error,
) error {
if len(c.Subreddits) > 0 {
ph, ok := handler.(botfaces.PostHandler)
if !ok {
return postHandlerErr
}
if posts, err := streams.Subreddits(
sc,
kill,
errs,
c.Subreddits...,
); err != nil {
return err
} else {
go func() {
for p := range posts {
errs <- ph.Post(p)
}
}()
}
}
if len(c.CustomFeeds) > 0 {
ph, ok := handler.(botfaces.PostHandler)
if !ok {
return postHandlerErr
}
for user, feeds := range c.CustomFeeds {
if posts, err := streams.CustomFeeds(
sc,
kill,
errs,
user,
feeds...,
); err != nil {
return err
} else {
go func() {
for p := range posts {
errs <- ph.Post(p)
}
}()
}
}
}
if len(c.SubredditComments) > 0 {
ch, ok := handler.(botfaces.CommentHandler)
if !ok {
return commentHandlerErr
}
if comments, err := streams.SubredditComments(
sc,
kill,
errs,
c.SubredditComments...,
); err != nil {
return err
} else {
go func() {
for c := range comments {
errs <- ch.Comment(c)
}
}()
}
}
if len(c.Users) > 0 {
uh, ok := handler.(botfaces.UserHandler)
if !ok {
return userHandlerErr
}
for _, user := range c.Users {
if posts, comments, err := streams.User(
sc,
kill,
errs,
user,
); err != nil {
return err
} else {
go func() {
for p := range posts {
errs <- uh.UserPost(p)
}
}()
go func() {
for c := range comments {
errs <- uh.UserComment(c)
}
}()
}
}
}
return nil
}