mi-v1/cmd/mi/reddit.go

142 lines
3.2 KiB
Go

package main
import (
"context"
"runtime"
"strings"
"github.com/turnage/graw/reddit"
"github.com/turnage/graw/streams"
r "gopkg.in/rethinkdb/rethinkdb-go.v6"
"within.website/ln"
)
func makeReddit() (reddit.Bot, error) {
cfg := reddit.BotConfig{
Agent: "mi:life_logging_bot:0.0.1 by /u/shadowh511",
App: reddit.App{
ID: *redditAppID,
Secret: *redditAppSecret,
Username: *redditUsername,
Password: *redditPassword,
},
}
bot, err := reddit.NewBot(cfg)
if err != nil {
return nil, err
}
return bot, nil
}
func (mi *Mi) StreamReddit(ctx context.Context) {
kill := make(chan bool)
errs := make(chan error, 5)
posts, comments, err := streams.User(mi.redditBot, kill, errs, *redditUsername)
if err != nil {
ln.FatalErr(ctx, err)
}
postReplies, err := streams.PostReplies(mi.redditBot, kill, errs)
if err != nil {
ln.FatalErr(ctx, err)
}
commentReplies, err := streams.CommentReplies(mi.redditBot, kill, errs)
if err != nil {
ln.FatalErr(ctx, err)
}
ln.Log(ctx, ln.Info("streaming reddit posts"))
for {
select {
case <-ctx.Done():
kill <- true
kill <- true
kill <- true
runtime.Goexit()
case <-errs:
ln.Error(ctx, err)
case p := <-posts:
ln.Log(ctx, ln.Info("got reddit post"), ln.F{
"from": p.Author,
"link": p.Permalink,
})
data := map[string]interface{}{
"id": p.Name,
"permalink": p.Permalink,
"created_at": p.CreatedUTC,
"author": p.Author,
"body": p.SelfText,
"url": p.URL,
}
err := r.Table("reddit_posts").Insert(data).Exec(mi.session)
if err != nil {
ln.Error(ctx, err)
}
case c := <-comments:
ln.Log(ctx, ln.Info("got reddit comment"), ln.F{
"id": c.ID,
"author": c.Author,
"thread": strings.Join(strings.Split(c.Permalink, "/")[:6], "/") + "/",
})
data := map[string]interface{}{
"id": c.ID,
"author": c.Author,
"parent_id": c.ParentID,
"body": c.Body,
"thread": strings.Join(strings.Split(c.Permalink, "/")[:6], "/") + "/",
}
err := r.Table("reddit_comments").Insert(data).Exec(mi.session)
if err != nil {
ln.Error(ctx, err)
}
case pr := <-postReplies:
ln.Log(ctx, ln.Info("got reddit post reply"), ln.F{
"id": pr.ID,
"author": pr.Author,
"thread": strings.Join(strings.Split(pr.Context, "/")[:6], "/") + "/",
})
data := map[string]interface{}{
"id": pr.ID,
"author": pr.Author,
"parent_id": pr.ParentID,
"body": pr.Body,
"thread": strings.Join(strings.Split(pr.Context, "/")[:6], "/") + "/",
}
err := r.Table("reddit_comments").Insert(data).Exec(mi.session)
if err != nil {
ln.Error(ctx, err)
}
case cr := <-commentReplies:
ln.Log(ctx, ln.Info("got reddit comment reply"), ln.F{
"id": cr.ID,
"author": cr.Author,
"thread": strings.Join(strings.Split(cr.Context, "/")[:6], "/") + "/",
})
data := map[string]interface{}{
"id": cr.ID,
"author": cr.Author,
"parent_id": cr.ParentID,
"body": cr.Body,
"thread": strings.Join(strings.Split(cr.Context, "/")[:6], "/") + "/",
}
err := r.Table("reddit_comments").Insert(data).Exec(mi.session)
if err != nil {
ln.Error(ctx, err)
}
}
}
}