use the normal flow?

Signed-off-by: Xe <me@christine.website>
This commit is contained in:
Cadey Ratio 2022-03-08 20:56:25 -05:00
parent 4ec98c9698
commit 4b0416555b
1 changed files with 46 additions and 51 deletions

69
main.go
View File

@ -10,14 +10,15 @@ import (
"strings" "strings"
"time" "time"
"github.com/turnage/graw"
"github.com/turnage/graw/reddit" "github.com/turnage/graw/reddit"
"github.com/turnage/graw/streams"
) )
// UA is the user agent for Reddit // UA is the user agent for Reddit
const UA = `NixOS:tulpa.dev/cadey/snoo2nebby:v0.1.0 (by /u/shadowh511)` const UA = `NixOS:tulpa.dev/cadey/snoo2nebby:v0.1.0 (by /u/shadowh511)`
var ( var (
agentFile = flag.String("agent-file", "./var/agent.yml", "the path to the bot's reddit agent config")
webhookFile = flag.String("webhook-file", "./var/webhook.txt", "where the Discord webhook file is located") webhookFile = flag.String("webhook-file", "./var/webhook.txt", "where the Discord webhook file is located")
subreddit = flag.String("subreddit", "tulpas", "the subreddit to monitor") subreddit = flag.String("subreddit", "tulpas", "the subreddit to monitor")
pokeFreq = flag.Duration("poke-frequency", 5*time.Minute, "how often the bot should poke the feed") pokeFreq = flag.Duration("poke-frequency", 5*time.Minute, "how often the bot should poke the feed")
@ -36,41 +37,16 @@ func clampLen(data string) string {
return sb.String() return sb.String()
} }
func main() { type postReplicatingBot struct {
flag.Parse() whURL string
bot reddit.Bot
}
script, err := reddit.NewScript(UA, *pokeFreq) func (p *postReplicatingBot) Post(post *reddit.Post) error {
if err != nil {
log.Fatal(err)
}
whSlc, err := ioutil.ReadFile(*webhookFile)
if err != nil {
log.Fatal(err)
}
whURL := string(bytes.TrimSpace(whSlc))
kill := make(chan bool)
errs := make(chan error)
stream, err := streams.Subreddits(script, kill, errs, *subreddit)
if err != nil {
log.Fatal(err)
}
go func(errs chan error) {
for err := range errs {
log.Printf("%v", err)
}
}(errs)
log.Printf("listening for new posts on /r/%s", *subreddit)
for post := range stream {
log.Printf("got new post: by /u/%s: %q %s, NSFW: %v", post.Author, post.URL, post.Title, post.NSFW) log.Printf("got new post: by /u/%s: %q %s, NSFW: %v", post.Author, post.URL, post.Title, post.NSFW)
if post.NSFW && !*allowNSFW { if post.NSFW && !*allowNSFW {
continue return nil
} }
wh := Webhook{ wh := Webhook{
@ -86,17 +62,36 @@ func main() {
}, },
} }
req := Send(whURL, wh) req := Send(p.whURL, wh)
req.Header.Set("User-Agent", UA) req.Header.Set("User-Agent", UA)
resp, err := http.DefaultClient.Do(req) resp, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
errs <- fmt.Errorf("can't send webhook: %w", err) return fmt.Errorf("can't send webhook: %w", err)
continue
} }
err = Validate(resp) err = Validate(resp)
if err != nil { if err != nil {
errs <- fmt.Errorf("can't validate response: %w", err) return fmt.Errorf("can't validate response: %w", err)
continue
} }
return nil
}
func main() {
flag.Parse()
script, err := reddit.NewBotFromAgentFile(*agentFile, 5*time.Minute)
whSlc, err := ioutil.ReadFile(*webhookFile)
if err != nil {
log.Fatal(err)
}
whURL := string(bytes.TrimSpace(whSlc))
log.Printf("listening for new posts on /r/%s", *subreddit)
if _, wait, err := graw.Run(&postReplicatingBot{whURL, script}, script, graw.Config{Subreddits: []string{*subreddit}}); err != nil {
log.Fatalf("can't connect to reddit: %v", err)
} else {
log.Fatalf("graw run failed: %v", wait())
} }
} }