filter NSFW posts, clamp post length
Signed-off-by: Christine Dodrill <me@christine.website>
This commit is contained in:
parent
094e23edf9
commit
26bed9bbd8
23
main.go
23
main.go
|
@ -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,
|
||||||
},
|
},
|
||||||
|
|
Reference in New Issue