-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
121 lines (110 loc) · 3.5 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"fmt"
"github.com/charmbracelet/log"
"os"
"github.com/alecthomas/kingpin/v2"
"github.com/jaxxstorm/grass/bot"
"github.com/jaxxstorm/grass/search"
"github.com/jaxxstorm/grass/storage"
"github.com/joho/godotenv"
)
var (
Version = "dev"
dbType = kingpin.Flag("db", "Specify the database type to use: dynamodb or sqlite").Default("sqlite").Enum("dynamodb", "sqlite")
keywords = kingpin.Flag("keyword", "Specify keywords to search for").Strings()
botTypes = kingpin.Flag("bot", "Specify bot types to use: print, discord").Strings()
searchers = kingpin.Flag("searchers", "Specify searchers to use: hackernews, reddit, bluesky").Strings()
tableName = kingpin.Flag("table-name", "Specify the table name to use for SQLite storage").Envar("SOCIAL_SEARCH_TABLE_NAME").Default("grass").String()
showVersion = kingpin.Flag("version", "Show the version and exit").Bool()
)
func init() {
// Load the .env file
err := godotenv.Load()
if err != nil {
log.Debug("No .env file found or error reading it; make sure environment variables are set.")
}
}
func main() {
kingpin.Parse()
if *showVersion {
fmt.Println("Version:", Version)
os.Exit(0)
}
// Initialize searchers
var searchersList []search.Searcher
for _, searcher := range *searchers {
switch searcher {
case "hackernews":
searchersList = append(searchersList, search.NewHackerNewsSearcher())
case "reddit":
redditSearcher, err := search.NewRedditSearcher()
if err != nil {
log.Fatalf("Failed to initialize Reddit searcher: %v", err)
}
searchersList = append(searchersList, redditSearcher)
case "bluesky":
blueskySearcher, err := search.NewBlueskySearcher()
if err != nil {
log.Fatalf("Failed to initialize Bluesky searcher: %v", err)
}
searchersList = append(searchersList, blueskySearcher)
case "fediverse":
fediverseSearcher, err := search.NewFediverseSearcher()
if err != nil {
log.Fatalf("Failed to initialize Fediverse searcher: %v", err)
}
searchersList = append(searchersList, fediverseSearcher)
case "youtube":
youtubeSearcher, err := search.NewYouTubeSearcher()
if err != nil {
log.Fatalf("Failed to initialize YouTube searcher: %v", err)
}
searchersList = append(searchersList, youtubeSearcher)
default:
log.Fatalf("Unknown searcher specified: %s", searcher)
}
}
// Initialize the storage backend
var storer storage.Storer
var err error
switch *dbType {
case "dynamodb":
storer, err = storage.NewDynamoDBStorer(*tableName)
if err != nil {
log.Fatalf("Failed to initialize DynamoDB storage: %v", err)
}
case "sqlite":
storer, err = storage.NewSQLiteStorer(*tableName)
if err != nil {
log.Fatalf("Failed to initialize SQLite storage: %v", err)
}
defer func() {
if err := storer.(*storage.SQLiteStorer).Close(); err != nil {
log.Printf("Failed to close SQLite storage: %v", err)
}
}()
default:
log.Fatalf("Unknown database type: %s", *dbType)
}
// Initialize notifiers
var notifiers []bot.Notifier
for _, botType := range *botTypes {
switch botType {
case "print":
notifiers = append(notifiers, bot.NewPrintNotifier())
case "discord":
notifiers = append(notifiers, bot.NewDiscordNotifier())
case "slack":
notifiers = append(notifiers, bot.NewSlackNotifier())
default:
log.Fatalf("Unknown bot type: %s", botType)
}
}
// Run the bot
b := bot.NewBot(searchersList, storer, notifiers)
for _, keyword := range *keywords {
log.Printf("Running search for keyword: %s", keyword)
b.Run(keyword)
}
}