2021-03-24 01:28:06 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-12-17 04:40:07 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-03-24 01:28:06 +00:00
|
|
|
|
|
|
|
"go.jolheiser.com/lurk/config"
|
|
|
|
|
|
|
|
"github.com/dghubble/go-twitter/twitter"
|
|
|
|
"go.jolheiser.com/disco"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Twitter struct {
|
|
|
|
Filter config.Filter
|
|
|
|
Stream *twitter.Stream
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Twitter) Run() {
|
2021-12-17 04:40:07 +00:00
|
|
|
log.Debug().Msgf("setting up stream for %v", t.Filter)
|
2021-03-24 01:28:06 +00:00
|
|
|
demux := twitter.NewSwitchDemux()
|
|
|
|
demux.Tweet = t.Tweet
|
|
|
|
|
2021-12-17 04:40:07 +00:00
|
|
|
log.Debug().Msgf("streaming %v", t.Filter)
|
2021-03-24 01:28:06 +00:00
|
|
|
demux.HandleChan(t.Stream.Messages)
|
2021-12-17 04:40:07 +00:00
|
|
|
log.Debug().Msgf("disconnected from stream: %v", t.Filter)
|
2021-03-24 01:28:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Twitter) Tweet(tweet *twitter.Tweet) {
|
2021-12-17 04:40:07 +00:00
|
|
|
log.Debug().Msgf("new tweet for %v", t.Filter)
|
2021-03-24 01:28:06 +00:00
|
|
|
|
|
|
|
if t.Filter.FollowStrict {
|
|
|
|
if tweet.InReplyToStatusIDStr != "" {
|
2021-12-17 04:40:07 +00:00
|
|
|
log.Debug().Msg("tweet is a reply")
|
2021-03-24 01:28:06 +00:00
|
|
|
return
|
|
|
|
}
|
2021-03-25 03:17:34 +00:00
|
|
|
if tweet.RetweetedStatus != nil {
|
2021-12-17 04:40:07 +00:00
|
|
|
log.Debug().Msg("tweet is a retweet")
|
2021-03-25 03:17:34 +00:00
|
|
|
return
|
|
|
|
}
|
2021-03-24 01:28:06 +00:00
|
|
|
var match bool
|
|
|
|
for _, id := range t.Filter.Follows {
|
|
|
|
if id == tweet.User.IDStr {
|
|
|
|
match = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !match {
|
2021-12-17 04:40:07 +00:00
|
|
|
log.Debug().Msg("tweet did not match any follow IDs")
|
2021-03-24 01:28:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w := &disco.Webhook{
|
2021-03-28 04:00:17 +00:00
|
|
|
Username: "@" + tweet.User.ScreenName,
|
|
|
|
AvatarURL: tweet.User.ProfileImageURLHttps,
|
|
|
|
Content: fmt.Sprintf("https://twitter.com/%d/status/%d", tweet.User.ID, tweet.ID),
|
2021-03-24 01:28:06 +00:00
|
|
|
}
|
|
|
|
if _, err := w.Send(context.Background(), t.Filter.Webhook); err != nil {
|
2021-12-17 04:40:07 +00:00
|
|
|
log.Err(err).Msg("")
|
2021-03-24 01:28:06 +00:00
|
|
|
}
|
|
|
|
}
|