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

Function streamSubredditPosts

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

Source from the content-addressed store, hash-verified

1356}
1357
1358func streamSubredditPosts(c *mira.Reddit, name string) (*mira.SubmissionStream, error) {
1359 sendC := make(chan models.Submission, 100)
1360 s := &mira.SubmissionStream{
1361 C: sendC,
1362 Close: make(chan struct{}),
1363 }
1364 _, err := c.Subreddit(name).Posts("new", "all", 1)
1365 if err != nil {
1366 return nil, err
1367 }
1368 var last models.RedditID
1369 go func() {
1370 sent := ring.New(100)
1371 for {
1372 select {
1373 case <-s.Close:
1374 close(sendC)
1375 return
1376 default:
1377 }
1378 posts, err := c.Subreddit(name).PostsAfter(last, 100)
1379 if err != nil {
1380 close(sendC)
1381 return
1382 }
1383 if len(posts) > 95 {
1384 //fmt.Printf("%d new posts | ", len(posts))
1385 }
1386 for i := len(posts) - 1; i >= 0; i-- {
1387 if ringContains(sent, posts[i].GetID()) {
1388 continue
1389 }
1390 sendC <- posts[i]
1391 sent.Value = posts[i].GetID()
1392 sent = sent.Next()
1393 }
1394 if len(posts) == 0 {
1395 last = ""
1396 } else if len(posts) > 2 {
1397 last = posts[1].GetID()
1398 }
1399 time.Sleep(13 * time.Second)
1400 }
1401 }()
1402 return s, nil
1403}
1404
1405func streamSubredditComments(c *mira.Reddit, name string) (*mira.SubmissionStream, error) {
1406 sendC := make(chan models.Submission, 100)

Callers 1

mainFunction · 0.85

Calls 1

ringContainsFunction · 0.85

Tested by

no test coverage detected