parent
68acb55eee
commit
569f560f16
|
@ -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
2
go.mod
|
@ -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
|
||||||
|
|
|
@ -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,
|
||||||
|
|
|
@ -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,58 +19,39 @@ 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("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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
beaver.Debugf("streaming %v", t.Filter)
|
beaver.Debugf("streaming %v", t.Filter)
|
||||||
demux.HandleChan(t.Stream.Messages)
|
demux.HandleChan(t.Stream.Messages)
|
||||||
beaver.Debugf("disconnected from stream: %v", t.Filter)
|
beaver.Debugf("disconnected from stream: %v", t.Filter)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Twitter) Discord(tweet *twitter.Tweet) error {
|
func (t *Twitter) Tweet(tweet *twitter.Tweet) {
|
||||||
e := map[string]string{
|
beaver.Debugf("new tweet for %v", t.Filter)
|
||||||
"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 == "" {
|
if t.Filter.FollowStrict {
|
||||||
return fmt.Errorf("no webhook for %v", t.Filter)
|
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)
|
w := &disco.Webhook{
|
||||||
resp, err := httpClient.Post(t.Filter.Webhook, "application/json", payload)
|
Username: tweet.User.Name,
|
||||||
if err != nil {
|
Content: fmt.Sprintf("https://twitter.com/%d/status/%d", tweet.User.ID, tweet.ID),
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
if _, err := w.Send(context.Background(), t.Filter.Webhook); err != nil {
|
||||||
if resp.StatusCode != http.StatusNoContent {
|
beaver.Error(err)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue