More updates

Signed-off-by: Etzelia <etzelia@hotmail.com>
main
Etzelia 2021-03-23 20:24:00 -05:00
parent 68acb55eee
commit 569f560f16
No known key found for this signature in database
GPG Key ID: 708511AE7ABC5314
4 changed files with 46 additions and 66 deletions

View File

@ -22,29 +22,29 @@ func (c *Config) loadReddit() {
if sub.BodyLimit == 0 || sub.BodyLimit > 2045 { if sub.BodyLimit == 0 || sub.BodyLimit > 2045 {
sub.BodyLimit = 2045 sub.BodyLimit = 2045
} }
sub.FlairWhitelistRe = make([]*regexp.Regexp, len(sub.FlairWhitelist)) sub.FlairAllowlistRe = make([]*regexp.Regexp, len(sub.FlairAllowlist))
for idx, f := range sub.FlairWhitelist { for idx, f := range sub.FlairAllowlist {
sub.FlairWhitelistRe[idx] = regexp.MustCompile(f) sub.FlairAllowlistRe[idx] = regexp.MustCompile(f)
} }
sub.FlairBlacklistRe = make([]*regexp.Regexp, len(sub.FlairBlacklist)) sub.FlairBlocklistRe = make([]*regexp.Regexp, len(sub.FlairBlocklist))
for idx, f := range sub.FlairBlacklist { for idx, f := range sub.FlairBlocklist {
sub.FlairBlacklistRe[idx] = regexp.MustCompile(f) sub.FlairBlocklistRe[idx] = regexp.MustCompile(f)
} }
sub.TitleWhitelistRe = make([]*regexp.Regexp, len(sub.TitleWhitelist)) sub.TitleAllowlistRe = make([]*regexp.Regexp, len(sub.TitleAllowlist))
for idx, t := range sub.TitleWhitelist { for idx, t := range sub.TitleAllowlist {
sub.TitleWhitelistRe[idx] = regexp.MustCompile(t) sub.TitleAllowlistRe[idx] = regexp.MustCompile(t)
} }
sub.TitleBlacklistRe = make([]*regexp.Regexp, len(sub.TitleBlacklist)) sub.TitleBlocklistRe = make([]*regexp.Regexp, len(sub.TitleBlocklist))
for idx, t := range sub.TitleBlacklist { for idx, t := range sub.TitleBlocklist {
sub.TitleBlacklistRe[idx] = regexp.MustCompile(t) sub.TitleBlocklistRe[idx] = regexp.MustCompile(t)
} }
sub.BodyWhitelistRe = make([]*regexp.Regexp, len(sub.BodyWhitelist)) sub.BodyAllowlistRe = make([]*regexp.Regexp, len(sub.BodyAllowlist))
for idx, b := range sub.BodyWhitelist { for idx, b := range sub.BodyAllowlist {
sub.BodyWhitelistRe[idx] = regexp.MustCompile(b) sub.BodyAllowlistRe[idx] = regexp.MustCompile(b)
} }
sub.BodyBlacklistRe = make([]*regexp.Regexp, len(sub.BodyBlacklist)) sub.BodyBlocklistRe = make([]*regexp.Regexp, len(sub.BodyBlocklist))
for idx, b := range sub.BodyBlacklist { for idx, b := range sub.BodyBlocklist {
sub.BodyBlacklistRe[idx] = regexp.MustCompile(b) sub.BodyBlocklistRe[idx] = regexp.MustCompile(b)
} }
} }
} }

2
go.mod
View File

@ -1,6 +1,6 @@
module go.etztech.xyz/lurk module go.etztech.xyz/lurk
go 1.14 go 1.16
require ( require (
github.com/dghubble/go-twitter v0.0.0-20200725221434-4bc8ad7ad1b4 github.com/dghubble/go-twitter v0.0.0-20200725221434-4bc8ad7ad1b4

View File

@ -46,7 +46,7 @@ func (r *Reddit) Post(p *reddit.Post) error {
Title: title, Title: title,
URL: p.URL, URL: p.URL,
Description: description, Description: description,
Color: 16312092, // Yellow Color: 0x007D96,
Timestamp: disco.Now(), Timestamp: disco.Now(),
Author: &disco.Author{ Author: &disco.Author{
Name: "/u/" + p.Author, Name: "/u/" + p.Author,

View File

@ -1,15 +1,14 @@
package handler package handler
import ( import (
"bytes" "context"
"encoding/json"
"fmt" "fmt"
"net/http"
"go.etztech.xyz/lurk/config" "go.etztech.xyz/lurk/config"
"github.com/dghubble/go-twitter/twitter" "github.com/dghubble/go-twitter/twitter"
"go.jolheiser.com/beaver" "go.jolheiser.com/beaver"
"go.jolheiser.com/disco"
) )
type Twitter struct { type Twitter struct {
@ -20,8 +19,16 @@ type Twitter struct {
func (t *Twitter) Run() { func (t *Twitter) Run() {
beaver.Debugf("setting up stream for %v", t.Filter) beaver.Debugf("setting up stream for %v", t.Filter)
demux := twitter.NewSwitchDemux() demux := twitter.NewSwitchDemux()
demux.Tweet = func(tweet *twitter.Tweet) { demux.Tweet = t.Tweet
beaver.Debugf("streaming %v", t.Filter)
demux.HandleChan(t.Stream.Messages)
beaver.Debugf("disconnected from stream: %v", t.Filter)
}
func (t *Twitter) Tweet(tweet *twitter.Tweet) {
beaver.Debugf("new tweet for %v", t.Filter) beaver.Debugf("new tweet for %v", t.Filter)
if t.Filter.FollowStrict { if t.Filter.FollowStrict {
if tweet.InReplyToStatusIDStr != "" { if tweet.InReplyToStatusIDStr != "" {
beaver.Debug("tweet is a reply") beaver.Debug("tweet is a reply")
@ -39,39 +46,12 @@ func (t *Twitter) Run() {
return return
} }
} }
if err := t.Discord(tweet); err != nil {
w := &disco.Webhook{
Username: tweet.User.Name,
Content: fmt.Sprintf("https://twitter.com/%d/status/%d", tweet.User.ID, tweet.ID),
}
if _, err := w.Send(context.Background(), t.Filter.Webhook); err != nil {
beaver.Error(err) beaver.Error(err)
} }
} }
beaver.Debugf("streaming %v", t.Filter)
demux.HandleChan(t.Stream.Messages)
beaver.Debugf("disconnected from stream: %v", t.Filter)
}
func (t *Twitter) Discord(tweet *twitter.Tweet) error {
e := map[string]string{
"content": fmt.Sprintf("https://twitter.com/%d/status/%d", tweet.User.ID, tweet.ID),
}
data, err := json.Marshal(e)
if err != nil {
return err
}
beaver.Debug(string(data))
if t.Filter.Webhook == "" {
return fmt.Errorf("no webhook for %v", t.Filter)
}
payload := bytes.NewBuffer(data)
resp, err := httpClient.Post(t.Filter.Webhook, "application/json", payload)
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
return err
}
return nil
}