MCPcopy Create free account
hub / github.com/AudDMusic/RedditBot / streamSubredditComments

Function streamSubredditComments

main.go:1405–1452  ·  view source on GitHub ↗
(c *mira.Reddit, name string)

Source from the content-addressed store, hash-verified

1403}
1404
1405func streamSubredditComments(c *mira.Reddit, name string) (*mira.SubmissionStream, error) {
1406 sendC := make(chan models.Submission, 100)
1407 s := &mira.SubmissionStream{
1408 C: sendC,
1409 Close: make(chan struct{}),
1410 }
1411 _, err := c.Subreddit(name).Posts("new", "all", 1)
1412 if err != nil {
1413 return nil, err
1414 }
1415 var last models.RedditID
1416 go func() {
1417 sent := ring.New(100)
1418 T := time.NewTicker(time.Millisecond * 1500)
1419 for {
1420 select {
1421 case <-s.Close:
1422 close(sendC)
1423 return
1424 default:
1425 }
1426 comments, err := c.Subreddit(name).CommentsAfter("new", last, 100)
1427 if err != nil {
1428 close(sendC)
1429 return
1430 }
1431 if len(comments) > 95 {
1432 //fmt.Printf("%d new comments | ", len(comments))
1433 }
1434 for i := len(comments) - 1; i >= 0; i-- {
1435 if ringContains(sent, comments[i].GetID()) {
1436 continue
1437 }
1438 //fmt.Print("a")
1439 sendC <- comments[i]
1440 sent.Value = comments[i].GetID()
1441 sent = sent.Next()
1442 }
1443 if len(comments) == 0 {
1444 last = ""
1445 } else if len(comments) > 2 {
1446 last = comments[1].GetID()
1447 }
1448 <-T.C
1449 }
1450 }()
1451 return s, nil
1452}
1453
1454func ringContains(r *ring.Ring, n models.RedditID) bool {
1455 ret := false

Callers 1

mainFunction · 0.85

Calls 1

ringContainsFunction · 0.85

Tested by

no test coverage detected