2020-04-18 02:32:09 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2020-08-10 14:06:41 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2020-04-18 02:32:09 +00:00
|
|
|
|
2020-08-10 14:06:41 +00:00
|
|
|
"go.etztech.xyz/lurk/config"
|
|
|
|
"go.etztech.xyz/lurk/handler"
|
|
|
|
|
|
|
|
"github.com/dghubble/go-twitter/twitter"
|
|
|
|
"github.com/dghubble/oauth1"
|
2020-04-18 02:32:09 +00:00
|
|
|
"github.com/turnage/graw"
|
|
|
|
"github.com/turnage/graw/reddit"
|
|
|
|
"go.jolheiser.com/beaver"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
configPath string
|
|
|
|
debug bool
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.StringVar(&configPath, "config", "lurk.toml", "Path to lurk's config file")
|
|
|
|
flag.BoolVar(&debug, "debug", false, "Turn on debug mode")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
beaver.Console.Format = beaver.FormatOptions{
|
|
|
|
TimePrefix: true,
|
|
|
|
StackPrefix: true,
|
|
|
|
StackLimit: 15,
|
|
|
|
LevelPrefix: true,
|
|
|
|
LevelColor: true,
|
|
|
|
}
|
|
|
|
if debug {
|
|
|
|
beaver.Console.Level = beaver.DEBUG
|
|
|
|
}
|
|
|
|
|
2020-08-10 14:06:41 +00:00
|
|
|
cfg, err := config.Load(configPath)
|
2020-04-18 02:32:09 +00:00
|
|
|
if err != nil {
|
|
|
|
beaver.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-08-10 14:06:41 +00:00
|
|
|
// Reddit
|
|
|
|
go lurkReddit(cfg)
|
|
|
|
|
|
|
|
// Twitter
|
|
|
|
go lurkTwitter(cfg)
|
|
|
|
|
|
|
|
beaver.Info("Lurk is ready to start lurking!")
|
|
|
|
ch := make(chan os.Signal)
|
|
|
|
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
<-ch
|
|
|
|
}
|
|
|
|
|
|
|
|
func lurkReddit(cfg *config.Config) {
|
2020-04-18 02:32:09 +00:00
|
|
|
bot, err := reddit.NewBot(reddit.BotConfig{
|
2020-08-10 14:06:41 +00:00
|
|
|
Agent: cfg.Reddit.UserAgent(),
|
2020-04-18 02:32:09 +00:00
|
|
|
App: reddit.App{
|
2020-08-10 14:06:41 +00:00
|
|
|
ID: cfg.Reddit.ClientID,
|
|
|
|
Secret: cfg.Reddit.ClientSecret,
|
|
|
|
Username: cfg.Reddit.Username,
|
|
|
|
Password: cfg.Reddit.Password,
|
2020-04-18 02:32:09 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
beaver.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-08-10 14:06:41 +00:00
|
|
|
_, wait, err := graw.Run(&handler.Reddit{
|
|
|
|
Config: cfg,
|
|
|
|
}, bot, graw.Config{
|
|
|
|
Subreddits: cfg.Reddit.SubRedditNames(),
|
|
|
|
})
|
2020-04-18 02:32:09 +00:00
|
|
|
if err != nil {
|
2020-08-10 14:06:41 +00:00
|
|
|
beaver.Errorf("could not run reddit bot: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := wait(); err != nil {
|
2020-04-18 02:32:09 +00:00
|
|
|
beaver.Fatal(err)
|
|
|
|
}
|
2020-08-10 14:06:41 +00:00
|
|
|
}
|
2020-04-18 02:32:09 +00:00
|
|
|
|
2020-08-10 14:06:41 +00:00
|
|
|
func lurkTwitter(cfg *config.Config) {
|
|
|
|
twitterConfig := oauth1.NewConfig(cfg.Twitter.ConsumerKey, cfg.Twitter.ConsumerSecret)
|
|
|
|
token := oauth1.NewToken(cfg.Twitter.AccessToken, cfg.Twitter.AccessSecret)
|
|
|
|
|
|
|
|
httpClient := twitterConfig.Client(oauth1.NoContext, token)
|
|
|
|
client := twitter.NewClient(httpClient)
|
|
|
|
|
|
|
|
// Just to test if we have valid auth
|
|
|
|
_, _, err := client.Timelines.HomeTimeline(&twitter.HomeTimelineParams{
|
|
|
|
Count: 1,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-04-18 02:32:09 +00:00
|
|
|
beaver.Fatal(err)
|
|
|
|
}
|
2020-08-10 14:06:41 +00:00
|
|
|
|
|
|
|
for _, filter := range cfg.Twitter.Filters {
|
|
|
|
stream, err := client.Streams.Filter(&twitter.StreamFilterParams{
|
|
|
|
Follow: filter.Follows,
|
|
|
|
Locations: filter.Locations,
|
|
|
|
StallWarnings: twitter.Bool(false),
|
|
|
|
Track: filter.Tracks,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
beaver.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lurker := &handler.Twitter{
|
|
|
|
Filter: filter,
|
|
|
|
Stream: stream,
|
|
|
|
}
|
|
|
|
|
|
|
|
go lurker.Run()
|
|
|
|
}
|
2020-04-18 02:32:09 +00:00
|
|
|
}
|