Skip to content

Commit 05a2a01

Browse files
committed
Added way for streaming redditor's comments
1 parent 8bbadfe commit 05a2a01

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

reddit.go

+28
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ func (c *Reddit) Comments(sort string, tdur string, limit int) ([]Comment, error
9595
return nil, err
9696
}
9797
return comments, nil
98+
case "redditor":
99+
return c.getRedditorComments(c.Chain.Name, sort, tdur, limit)
98100
default:
99101
return nil, fmt.Errorf("'%s' type does not have an option for comments", c.Chain.Type)
100102
}
@@ -252,6 +254,30 @@ func (c *Reddit) getSubredditComments(sr string, sort string, tdur string, limit
252254
return ret.GetChildren(), err
253255
}
254256

257+
func (c *Reddit) getRedditorComments(user string, sort string, tdur string, limit int) ([]Comment, error) {
258+
target := RedditOauth + "/u/" + user + "/comments.json"
259+
ans, err := c.MiraRequest("GET", target, map[string]string{
260+
"sort": sort,
261+
"limit": strconv.Itoa(limit),
262+
"t": tdur,
263+
})
264+
ret := &CommentListing{}
265+
json.Unmarshal(ans, ret)
266+
return ret.GetChildren(), err
267+
}
268+
269+
func (c *Reddit) getRedditorCommentsAfter(user string, sort string, last string, limit int) ([]Comment, error) {
270+
target := RedditOauth + "/u/" + user + "/comments.json"
271+
ans, err := c.MiraRequest("GET", target, map[string]string{
272+
"sort": sort,
273+
"limit": strconv.Itoa(limit),
274+
"before": last,
275+
})
276+
ret := &CommentListing{}
277+
json.Unmarshal(ans, ret)
278+
return ret.GetChildren(), err
279+
}
280+
255281
func (c *Reddit) getSubmissionComments(post_id string, sort string, tdur string, limit int) ([]Comment, []string, error) {
256282
if string(post_id[1]) != "3" {
257283
return nil, nil, errors.New("the passed ID36 is not a submission")
@@ -304,6 +330,8 @@ func (c *Reddit) CommentsAfter(sort string, last string, limit int) ([]Comment,
304330
switch c.Chain.Type {
305331
case "subreddit":
306332
return c.getSubredditCommentsAfter(c.Chain.Name, sort, last, limit)
333+
case "redditor":
334+
return c.getRedditorCommentsAfter(c.Chain.Name, sort, last, limit)
307335
default:
308336
return nil, fmt.Errorf("'%s' type does not have an option for comments", c.Chain.Type)
309337
}

streaming.go

+44-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ func (r *Reddit) StreamCommentReplies() (<-chan Comment, chan bool) {
3131
// // c is the channel with all comments
3232
// // stop is the channel to stop the stream. Do stop <- true to stop the loop
3333
func (r *Reddit) StreamComments() (<-chan Comment, chan bool, error) {
34+
err := r.checkType("subreddit", "redditor")
35+
if err != nil {
36+
return nil, nil, err
37+
}
38+
switch r.Chain.Type {
39+
case "subreddit":
40+
return r.streamSubredditComments()
41+
case "redditor":
42+
return r.streamRedditorComments()
43+
}
44+
return nil, nil, nil
45+
}
46+
47+
func (r *Reddit) streamSubredditComments() (<-chan Comment, chan bool, error) {
3448
c := make(chan Comment, 25)
3549
stop := make(chan bool, 1)
3650
anchor, err := r.Subreddit(r.Chain.Name).Comments("new", "hour", 1)
@@ -45,7 +59,36 @@ func (r *Reddit) StreamComments() (<-chan Comment, chan bool, error) {
4559
for {
4660
stop <- false
4761
un, _ := r.Subreddit(r.Chain.Name).CommentsAfter("new", last, 25)
48-
// un, _ := r.getSubredditCommentsAfter(sr, "new", last, 25)
62+
for _, v := range un {
63+
c <- v
64+
}
65+
if len(un) > 0 {
66+
last = un[0].GetId()
67+
}
68+
time.Sleep(r.Stream.CommentListInterval * time.Second)
69+
if <-stop {
70+
return
71+
}
72+
}
73+
}()
74+
return c, stop, nil
75+
}
76+
77+
func (r *Reddit) streamRedditorComments() (<-chan Comment, chan bool, error) {
78+
c := make(chan Comment, 25)
79+
stop := make(chan bool, 1)
80+
anchor, err := r.Redditor(r.Chain.Name).Comments("new", "hour", 1)
81+
if err != nil {
82+
return nil, nil, err
83+
}
84+
last := ""
85+
if len(anchor) > 0 {
86+
last = anchor[0].GetId()
87+
}
88+
go func() {
89+
for {
90+
stop <- false
91+
un, _ := r.Redditor(r.Chain.Name).CommentsAfter("new", last, 25)
4992
for _, v := range un {
5093
c <- v
5194
}

0 commit comments

Comments
 (0)