forked from thecsw/mira
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreaming.go
191 lines (181 loc) · 4.55 KB
/
streaming.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
package mira
import (
"time"
"github.com/Moonlight-io/mira/models"
)
// StreamCommentReplies streams comment replies
// c is the channel with all unread messages
func (r *Reddit) StreamCommentReplies() <-chan models.Comment {
c := make(chan models.Comment, 100)
go func() {
for {
un, _ := r.Me().ListUnreadMessages()
for _, v := range un {
if v.IsCommentReply() {
// Only process comment replies and
// mark them as read.
c <- v
// You can read the message with
r.Me().ReadMessage(v.GetId())
}
}
time.Sleep(r.Stream.CommentListInterval * time.Second)
}
}()
return c
}
// StreamMentions streams recent mentions
// c is the channel with all unread messages
func (r *Reddit) StreamMentions() <-chan models.Comment {
c := make(chan models.Comment, 100)
go func() {
for {
un, _ := r.Me().ListUnreadMessages()
for _, v := range un {
if v.IsMention() {
// Only process comment replies and
// mark them as read.
c <- v
// You can read the message with
r.Me().ReadMessage(v.GetId())
}
}
time.Sleep(r.Stream.CommentListInterval * time.Second)
}
}()
return c
}
// StreamComments streams comments from a redditor or a subreddit
// c is the channel with all comments
func (r *Reddit) StreamComments() (<-chan models.Comment, error) {
name, ttype, err := r.checkType("subreddit", "redditor")
if err != nil {
return nil, err
}
switch ttype {
case "subreddit":
return r.streamSubredditComments(name)
case "redditor":
return r.streamRedditorComments(name)
}
return nil, nil
}
/// StreamSubmissions streams submissions from a redditor or a subreddit
// c is the channel with all submissions
func (r *Reddit) StreamSubmissions() (<-chan models.PostListingChild, error) {
name, ttype, err := r.checkType("subreddit", "redditor")
if err != nil {
return nil, err
}
switch ttype {
case "subreddit":
return r.streamSubredditSubmissions(name)
case "redditor":
return r.streamRedditorSubmissions(name)
}
return nil, nil
}
func (r *Reddit) streamSubredditComments(subreddit string) (<-chan models.Comment, error) {
c := make(chan models.Comment, 100)
anchor, err := r.Subreddit(subreddit).Comments("new", "hour", 1)
if err != nil {
return nil, err
}
last := ""
if len(anchor) > 0 {
last = anchor[0].GetId()
}
go func() {
for {
un, _ := r.Subreddit(subreddit).CommentsAfter("new", last, 100)
if len(un) < 1 {
time.Sleep(r.Stream.CommentListInterval * time.Second)
continue
}
last = un[0].GetId()
for _, v := range un {
c <- v
}
time.Sleep(r.Stream.CommentListInterval * time.Second)
}
}()
return c, nil
}
func (r *Reddit) streamRedditorComments(redditor string) (<-chan models.Comment, error) {
c := make(chan models.Comment, 100)
anchor, err := r.Redditor(redditor).Comments("new", "hour", 1)
if err != nil {
return nil, err
}
last := ""
if len(anchor) > 0 {
last = anchor[0].GetId()
}
go func() {
for {
un, _ := r.Redditor(redditor).CommentsAfter("new", last, 100)
if len(un) < 1 {
time.Sleep(r.Stream.CommentListInterval * time.Second)
continue
}
last = un[0].GetId()
for _, v := range un {
c <- v
}
time.Sleep(r.Stream.CommentListInterval * time.Second)
}
}()
return c, nil
}
func (r *Reddit) streamSubredditSubmissions(subreddit string) (<-chan models.PostListingChild, error) {
c := make(chan models.PostListingChild, 100)
anchor, err := r.Subreddit(subreddit).Submissions("new", "hour", 1)
if err != nil {
return nil, err
}
last := ""
if len(anchor) > 0 {
last = anchor[0].GetId()
}
go func() {
for {
new, _ := r.Subreddit(subreddit).SubmissionsAfter(last, r.Stream.PostListSlice)
if len(new) < 1 {
time.Sleep(r.Stream.PostListInterval * time.Second)
continue
}
last = new[0].GetId()
for i := range new {
c <- new[len(new)-i-1]
}
time.Sleep(r.Stream.PostListInterval * time.Second)
}
}()
return c, nil
}
func (r *Reddit) streamRedditorSubmissions(redditor string) (<-chan models.PostListingChild, error) {
c := make(chan models.PostListingChild, 100)
anchor, err := r.Redditor(redditor).Submissions("new", "hour", 1)
if err != nil {
return nil, err
}
last := ""
if len(anchor) > 0 {
last = anchor[0].GetId()
}
go func() {
for {
new, _ := r.Redditor(redditor).SubmissionsAfter(last, r.Stream.PostListSlice)
if len(new) < 1 {
time.Sleep(r.Stream.PostListInterval * time.Second)
continue
}
last = new[0].GetId()
for i := range new {
c <- new[len(new)-i-1]
}
time.Sleep(r.Stream.PostListInterval * time.Second)
}
}()
return c, nil
}