157 lines
3.7 KiB
Go
157 lines
3.7 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"runtime"
|
|
"strings"
|
|
|
|
"github.com/turnage/graw/reddit"
|
|
"github.com/turnage/graw/streams"
|
|
r "gopkg.in/rethinkdb/rethinkdb-go.v6"
|
|
"within.website/ln"
|
|
)
|
|
|
|
var (
|
|
// Reddit
|
|
redditAppID = flag.String("reddit-app-id", "", "Reddit app ID")
|
|
redditAppSecret = flag.String("reddit-app-secret", "", "Reddit app Secret")
|
|
redditUsername = flag.String("reddit-username", "", "Reddit username")
|
|
redditPassword = flag.String("reddit-password", "", "Reddit password")
|
|
)
|
|
|
|
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) PushReddit(ctx context.Context, p Post) error {
|
|
return mi.redditBot.PostLink("u_"+*redditUsername, p.Title, p.URL)
|
|
}
|
|
|
|
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 err := <-errs:
|
|
if err != nil {
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
}
|