filter NSFW posts, clamp post length

Signed-off-by: Christine Dodrill <me@christine.website>
This commit is contained in:
Cadey Ratio 2021-09-29 20:13:57 -04:00
parent 094e23edf9
commit 26bed9bbd8
3 changed files with 27 additions and 2 deletions

1
.envrc Normal file
View File

@ -0,0 +1 @@
eval "$(lorri direnv)"

23
main.go
View File

@ -7,6 +7,7 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"strings"
"time" "time"
"github.com/turnage/graw/reddit" "github.com/turnage/graw/reddit"
@ -20,8 +21,21 @@ var (
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")
allowNSFW = flag.Bool("allow-nsfw", false, "proxy NSFW posts?")
) )
func clampLen(data string) string {
if len(data) < 1800 {
return data
}
var sb strings.Builder
sb.WriteString(data[0:1800])
sb.WriteString("\n\n[Post truncated for length]")
return sb.String()
}
func main() { func main() {
flag.Parse() flag.Parse()
@ -53,13 +67,18 @@ func main() {
log.Printf("listening for new posts on /r/%s", *subreddit) log.Printf("listening for new posts on /r/%s", *subreddit)
for post := range stream { for post := range stream {
log.Printf("got new post: by /u/%s: %q %s", post.Author, post.URL, post.Title) log.Printf("got new post: by /u/%s: %q %s, NSFW: %v", post.Author, post.URL, post.Title, post.NSFW)
if post.NSFW && !*allowNSFW {
continue
}
wh := Webhook{ wh := Webhook{
Embeds: []Embed{ Embeds: []Embed{
{ {
Title: post.Title, Title: post.Title,
URL: post.URL, URL: post.URL,
Description: post.SelfText, Description: clampLen(post.SelfText),
Footer: EmbedFooter{ Footer: EmbedFooter{
Text: "by /u/" + post.Author, Text: "by /u/" + post.Author,
}, },

5
shell.nix Normal file
View File

@ -0,0 +1,5 @@
{ pkgs ? import <nixpkgs> { } }:
pkgs.mkShell {
buildInputs = with pkgs; [ go gopls goimports bashInteractive ];
}