-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
113 lines (94 loc) · 2.12 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
package main
import (
"awesome-piracy-bot/pkg/discord"
"awesome-piracy-bot/pkg/telegram"
"fmt"
"github.com/spf13/viper"
"log"
"os"
"reflect"
)
var goroutineDelta = make(chan int)
type Config struct {
Watchers struct {
Telegram struct {
Enabled bool
APIToken string
}
Discord struct {
Enabled bool
APIToken string
}
Reddit struct {
Enabled bool
}
IRC struct {
Enabled bool
}
}
}
type WatcherConfig struct {
Type string
APIToken string
}
type WatcherConfigs []WatcherConfig
func main() {
// get configuration
var name = "awesome-piracy-bot"
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(fmt.Sprintf("/etc/%s/", name))
viper.AddConfigPath(fmt.Sprintf("$HOME/.config/%s/", name))
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
log.Panicf("Fatal error in config file: %s \n", err)
}
config := new(Config)
if err := viper.Unmarshal(config); err != nil {
log.Panicf("Error parsing config file, %v \n", err)
}
v := reflect.ValueOf(config.Watchers).Type()
var watcherConfigs WatcherConfigs
// generate a list of WatcherConfigs
for i := 0; i < v.NumField(); i++ {
c := reflect.ValueOf(config.Watchers).Field(i)
enabled := c.FieldByName("Enabled")
token := c.FieldByName("APIToken")
// create a WatcherConfig with this iteration's watcher type
if enabled.Bool() == true {
watcherConfig := WatcherConfig{
Type: v.Field(i).Name,
APIToken: token.String(),
}
watcherConfigs = append(watcherConfigs, watcherConfig)
}
}
numGoroutines := 0
//urls := make(chan string)
// start watchers
for _, w := range watcherConfigs {
go w.startWatcher()
}
// start listening to watcher channels for URLs
// run metadata tasks for incoming URLs
// upload URL with metadata to ElasticSearch
for diff := range goroutineDelta {
numGoroutines += diff
if numGoroutines == 0 {
os.Exit(0)
}
}
}
func (c WatcherConfig) startWatcher() {
if c.Type == "Telegram" {
telegram.Run(c.APIToken)
}
if c.Type == "Discord" {
discord.Run(c.APIToken)
}
goroutineDelta <- +1
go f()
}
func f() {
goroutineDelta <- -1
}