forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postfetcher.go
193 lines (152 loc) · 4 KB
/
postfetcher.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
package reddit
import (
"strconv"
"sync"
"time"
greddit "github.com/jonas747/go-reddit"
"github.com/jonas747/retryableredis"
"github.com/jonas747/yagpdb/common"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
var KeyLastScannedPostIDFast = "reddit_last_post_id"
var KeyLastScannedPostIDSlow = "reddit_slow_last_post_id"
// PostFetcher is responsible from fetching posts from reddit at a given interval and delay
// delay bieng it will make sure not to call the handler on posts newer than the given delay
type PostFetcher struct {
Name string
LastScannedPostIDKey string
LastID int64
StopChan chan *sync.WaitGroup
started time.Time
hasCaughtUp bool
delay time.Duration
redditClient *greddit.Client
handler PostHandler
log *logrus.Entry
}
type PostHandler interface {
HandleRedditPosts(links []*greddit.Link)
}
func NewPostFetcher(redditClient *greddit.Client, slow bool, handler PostHandler) *PostFetcher {
idKey := KeyLastScannedPostIDFast
name := "fast"
delay := time.Minute
if slow {
name = "slow"
idKey = KeyLastScannedPostIDSlow
delay = time.Minute * 15
}
return &PostFetcher{
Name: name,
redditClient: redditClient,
LastScannedPostIDKey: idKey,
delay: delay,
handler: handler,
log: logger.WithField("rfeed_type", name),
StopChan: make(chan *sync.WaitGroup),
}
}
func (p *PostFetcher) Run() {
lastLogged := time.Now()
numPosts := 0
ticker := time.NewTicker(time.Second * 5)
for {
select {
case wg := <-p.StopChan:
wg.Done()
return
case <-ticker.C:
}
links, err := p.GetNewPosts()
if err != nil {
p.log.WithError(err).Error("error fetchind new links")
continue
}
if len(links) < 1 {
continue
}
// basic stats
numPosts += len(links)
if time.Since(lastLogged) >= time.Minute {
p.log.Info("Num posts last minute: ", numPosts)
lastLogged = time.Now()
numPosts = 0
}
p.handler.HandleRedditPosts(links)
}
}
func (p *PostFetcher) initCursor() (int64, error) {
var storedID int64
common.RedisPool.Do(retryableredis.Cmd(&storedID, "GET", p.LastScannedPostIDKey))
if storedID != 0 {
p.log.Info("reddit feed continuing from ", storedID)
return storedID, nil
}
p.log.Warn("reddit plugin failed resuming, starting from most recent post")
// Start from new
newPosts, err := p.redditClient.GetNewLinks("all", "", "")
if err != nil {
return 0, err
}
if len(newPosts) < 1 {
return 0, errors.New("No posts")
}
stringID := newPosts[0].ID
parsed, err := strconv.ParseInt(stringID, 36, 64)
return parsed, err
}
func (p *PostFetcher) GetNewPosts() ([]*greddit.Link, error) {
if p.started.IsZero() {
p.started = time.Now()
}
if p.LastID == 0 {
lID, err := p.initCursor()
if err != nil {
return nil, errors.WithMessage(err, "Failed initialising cursor")
}
p.LastID = lID
logrus.Info("Initialized reddit post cursor at ", lID)
}
toFetch := make([]string, 100)
for i := int64(0); i < 100; i++ {
toFetch[i] = "t3_" + strconv.FormatInt(p.LastID+i+1, 36)
}
resp, err := p.redditClient.LinksInfo(toFetch)
if err != nil {
return nil, err
}
end := 0
highestID := int64(-1)
for i, v := range resp {
unixSeconds := int64(v.CreatedUtc)
age := time.Since(time.Unix(unixSeconds, 0))
// logrus.Info(age.String())
// stay 1 minute behind
if age < p.delay {
break
}
end = i + 1
parsedId, err := strconv.ParseInt(v.ID, 36, 64)
if err != nil {
logrus.WithError(err).WithField("id", v.ID).Error("Failed parsing reddit post id")
continue
}
if highestID < parsedId {
highestID = parsedId
}
}
resp = resp[:end]
if highestID != -1 {
p.LastID = highestID
common.RedisPool.Do(retryableredis.FlatCmd(nil, "SET", p.LastScannedPostIDKey, highestID))
}
if !p.hasCaughtUp {
logrus.Info("Redditfeed processed ", len(resp), " links")
}
if len(resp) < 50 && !p.hasCaughtUp {
logrus.Info("Reddit feed caught up in ", time.Since(p.started).String())
p.hasCaughtUp = true
}
return resp, nil
}