parent
68acb55eee
commit
569f560f16
|
@ -22,29 +22,29 @@ func (c *Config) loadReddit() {
|
|||
if sub.BodyLimit == 0 || sub.BodyLimit > 2045 {
|
||||
sub.BodyLimit = 2045
|
||||
}
|
||||
sub.FlairWhitelistRe = make([]*regexp.Regexp, len(sub.FlairWhitelist))
|
||||
for idx, f := range sub.FlairWhitelist {
|
||||
sub.FlairWhitelistRe[idx] = regexp.MustCompile(f)
|
||||
sub.FlairAllowlistRe = make([]*regexp.Regexp, len(sub.FlairAllowlist))
|
||||
for idx, f := range sub.FlairAllowlist {
|
||||
sub.FlairAllowlistRe[idx] = regexp.MustCompile(f)
|
||||
}
|
||||
sub.FlairBlacklistRe = make([]*regexp.Regexp, len(sub.FlairBlacklist))
|
||||
for idx, f := range sub.FlairBlacklist {
|
||||
sub.FlairBlacklistRe[idx] = regexp.MustCompile(f)
|
||||
sub.FlairBlocklistRe = make([]*regexp.Regexp, len(sub.FlairBlocklist))
|
||||
for idx, f := range sub.FlairBlocklist {
|
||||
sub.FlairBlocklistRe[idx] = regexp.MustCompile(f)
|
||||
}
|
||||
sub.TitleWhitelistRe = make([]*regexp.Regexp, len(sub.TitleWhitelist))
|
||||
for idx, t := range sub.TitleWhitelist {
|
||||
sub.TitleWhitelistRe[idx] = regexp.MustCompile(t)
|
||||
sub.TitleAllowlistRe = make([]*regexp.Regexp, len(sub.TitleAllowlist))
|
||||
for idx, t := range sub.TitleAllowlist {
|
||||
sub.TitleAllowlistRe[idx] = regexp.MustCompile(t)
|
||||
}
|
||||
sub.TitleBlacklistRe = make([]*regexp.Regexp, len(sub.TitleBlacklist))
|
||||
for idx, t := range sub.TitleBlacklist {
|
||||
sub.TitleBlacklistRe[idx] = regexp.MustCompile(t)
|
||||
sub.TitleBlocklistRe = make([]*regexp.Regexp, len(sub.TitleBlocklist))
|
||||
for idx, t := range sub.TitleBlocklist {
|
||||
sub.TitleBlocklistRe[idx] = regexp.MustCompile(t)
|
||||
}
|
||||
sub.BodyWhitelistRe = make([]*regexp.Regexp, len(sub.BodyWhitelist))
|
||||
for idx, b := range sub.BodyWhitelist {
|
||||
sub.BodyWhitelistRe[idx] = regexp.MustCompile(b)
|
||||
sub.BodyAllowlistRe = make([]*regexp.Regexp, len(sub.BodyAllowlist))
|
||||
for idx, b := range sub.BodyAllowlist {
|
||||
sub.BodyAllowlistRe[idx] = regexp.MustCompile(b)
|
||||
}
|
||||
sub.BodyBlacklistRe = make([]*regexp.Regexp, len(sub.BodyBlacklist))
|
||||
for idx, b := range sub.BodyBlacklist {
|
||||
sub.BodyBlacklistRe[idx] = regexp.MustCompile(b)
|
||||
sub.BodyBlocklistRe = make([]*regexp.Regexp, len(sub.BodyBlocklist))
|
||||
for idx, b := range sub.BodyBlocklist {
|
||||
sub.BodyBlocklistRe[idx] = regexp.MustCompile(b)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
2
go.mod
2
go.mod
|
@ -1,6 +1,6 @@
|
|||
module go.etztech.xyz/lurk
|
||||
|
||||
go 1.14
|
||||
go 1.16
|
||||
|
||||
require (
|
||||
github.com/dghubble/go-twitter v0.0.0-20200725221434-4bc8ad7ad1b4
|
||||
|
|
|
@ -46,7 +46,7 @@ func (r *Reddit) Post(p *reddit.Post) error {
|
|||
Title: title,
|
||||
URL: p.URL,
|
||||
Description: description,
|
||||
Color: 16312092, // Yellow
|
||||
Color: 0x007D96,
|
||||
Timestamp: disco.Now(),
|
||||
Author: &disco.Author{
|
||||
Name: "/u/" + p.Author,
|
||||
|
|
|
@ -1,15 +1,14 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"go.etztech.xyz/lurk/config"
|
||||
|
||||
"github.com/dghubble/go-twitter/twitter"
|
||||
"go.jolheiser.com/beaver"
|
||||
"go.jolheiser.com/disco"
|
||||
)
|
||||
|
||||
type Twitter struct {
|
||||
|
@ -20,58 +19,39 @@ type Twitter struct {
|
|||
func (t *Twitter) Run() {
|
||||
beaver.Debugf("setting up stream for %v", t.Filter)
|
||||
demux := twitter.NewSwitchDemux()
|
||||
demux.Tweet = func(tweet *twitter.Tweet) {
|
||||
beaver.Debugf("new tweet for %v", t.Filter)
|
||||
if t.Filter.FollowStrict {
|
||||
if tweet.InReplyToStatusIDStr != "" {
|
||||
beaver.Debug("tweet is a reply")
|
||||
return
|
||||
}
|
||||
var match bool
|
||||
for _, id := range t.Filter.Follows {
|
||||
if id == tweet.User.IDStr {
|
||||
match = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !match {
|
||||
beaver.Debug("tweet did not match any follow IDs")
|
||||
return
|
||||
}
|
||||
}
|
||||
if err := t.Discord(tweet); err != nil {
|
||||
beaver.Error(err)
|
||||
}
|
||||
}
|
||||
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) 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))
|
||||
func (t *Twitter) Tweet(tweet *twitter.Tweet) {
|
||||
beaver.Debugf("new tweet for %v", t.Filter)
|
||||
|
||||
if t.Filter.Webhook == "" {
|
||||
return fmt.Errorf("no webhook for %v", t.Filter)
|
||||
if t.Filter.FollowStrict {
|
||||
if tweet.InReplyToStatusIDStr != "" {
|
||||
beaver.Debug("tweet is a reply")
|
||||
return
|
||||
}
|
||||
var match bool
|
||||
for _, id := range t.Filter.Follows {
|
||||
if id == tweet.User.IDStr {
|
||||
match = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !match {
|
||||
beaver.Debug("tweet did not match any follow IDs")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
payload := bytes.NewBuffer(data)
|
||||
resp, err := httpClient.Post(t.Filter.Webhook, "application/json", payload)
|
||||
if err != nil {
|
||||
return err
|
||||
w := &disco.Webhook{
|
||||
Username: tweet.User.Name,
|
||||
Content: fmt.Sprintf("https://twitter.com/%d/status/%d", tweet.User.ID, tweet.ID),
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusNoContent {
|
||||
return err
|
||||
if _, err := w.Send(context.Background(), t.Filter.Webhook); err != nil {
|
||||
beaver.Error(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue