-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
types.go
192 lines (168 loc) · 6.51 KB
/
types.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package types
import (
"encoding/json"
"io/ioutil"
"path/filepath"
"strings"
"github.com/hbollon/igopher/internal/engine"
"github.com/hbollon/igopher/internal/modules/blacklist"
"github.com/hbollon/igopher/internal/modules/quotas"
"github.com/hbollon/igopher/internal/modules/scheduler"
"github.com/hbollon/igopher/internal/proxy"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
// SplitStringSlice is a custom string slice type used to define a custom json unmarshal rule
type SplitStringSlice []string
// UnmarshalJSON custom rule for unmarshal string array from string by splitting it by ';'
func (strSlice *SplitStringSlice) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
*strSlice = strings.Split(s, ";")
return nil
}
// IGopher struct store all bot and ig related configuration and modules instances.
// Settings are readed from Yaml config files.
type IGopher struct {
// SeleniumStruct contain all selenium stuff and config
SeleniumStruct engine.Selenium `yaml:"webdriver"`
// User credentials
UserAccount Account `yaml:"account"`
// Automatic messages sending module
DmModule AutoDM `yaml:"auto_dm"`
// Quotas
Quotas quotas.QuotaManager `yaml:"quotas"`
// Scrapper
ScrapperManager ScrapperConfig `yaml:"scrapper"`
// Scheduler
Scheduler scheduler.Manager `yaml:"schedule"`
// Interracted users blacklist
Blacklist blacklist.Manager `yaml:"blacklist"`
// Channels
InfoCh chan string `yaml:"-"`
ErrCh chan string `yaml:"-"`
CrashCh chan error `yaml:"-"`
ExitCh chan bool `yaml:"-"`
HotReloadCallback chan bool `yaml:"-"`
ReloadCallback chan bool `yaml:"-"`
// Running state
Running bool `yaml:"-"`
}
// HotReload update bot config without stopping it
// Some settings cannot be updated this way like account credentials
func (bot *IGopher) HotReload() error {
newConfig, err := ReadBotConfigYaml()
if err != nil {
return err
}
bot.DmModule = newConfig.DmModule
bot.Quotas = newConfig.Quotas
bot.ScrapperManager = newConfig.ScrapperManager
bot.Scheduler = newConfig.Scheduler
bot.Blacklist = newConfig.Blacklist
return nil
}
// ReadBotConfigYaml read config yml file and initialize it for use with bot
func ReadBotConfigYaml() (IGopher, error) {
var c IGopher
file, err := ioutil.ReadFile(filepath.FromSlash("./config/config.yaml"))
if err != nil {
logrus.Fatalf("Error opening config file: %s", err)
}
err = yaml.Unmarshal(file, &c)
if err != nil {
logrus.Fatalf("Error during unmarshal config file: %s\n", err)
}
c.Quotas.InitializeQuotaManager()
err = c.Scheduler.InitializeScheduler()
if err != nil {
logrus.Errorf("Failed to initialize scheduler: %v", err)
return c, err
}
err = c.Blacklist.InitializeBlacklist()
if err != nil {
logrus.Errorf("Failed to initialize blacklist: %v", err)
return c, err
}
return c, nil
}
// ScrapperConfig store scrapper configuration for user fetching
// It also store fetched usernames
type ScrapperConfig struct {
SrcAccounts []string `yaml:"src_accounts"`
FetchedAccounts []string
Quantity int `yaml:"fetch_quantity" validate:"numeric"`
}
// Account store personnal credentials
type Account struct {
Username string `json:"username" yaml:"username" validate:"required,min=1,max=30"`
Password string `json:"password" yaml:"password" validate:"required,min=1"`
}
// AutoDM store messaging module configuration
type AutoDM struct {
// List of all availlables message templates
DmTemplates []string `json:"dmTemplates" yaml:"dm_templates" validate:"required"`
// Greeting module add a customized DM header with recipient username
Greeting GreetingConfig `yaml:"greeting"`
Activated bool `json:"dmActivated" yaml:"activated"`
}
// GreetingConfig store greeting configuration for AutoDM module
type GreetingConfig struct {
// Add a string before the username
Template string `json:"greetingTemplate" yaml:"template" validate:"required"`
Activated bool `json:"greetingActivated" yaml:"activated"`
}
/* Yaml */
// BotConfigYaml is the raw representation of the yaml bot config file
type BotConfigYaml struct {
Account AccountYaml `json:"account" yaml:"account"`
SrcUsers ScrapperYaml `json:"scrapper" yaml:"scrapper"`
AutoDm AutoDmYaml `json:"auto_dm" yaml:"auto_dm"`
Quotas QuotasYaml `json:"quotas" yaml:"quotas"`
Schedule ScheduleYaml `json:"schedule" yaml:"schedule"`
Blacklist BlacklistYaml `json:"blacklist" yaml:"blacklist"`
Selenium SeleniumYaml `json:"webdriver" yaml:"webdriver"`
}
// AccountYaml is the yaml account configuration representation
type AccountYaml struct {
Username string `json:"username" yaml:"username" validate:"required,min=1,max=30"`
Password string `json:"password" yaml:"password" validate:"required"`
}
// ScrapperYaml is the yaml user scrapping configuration representation
type ScrapperYaml struct {
Accounts SplitStringSlice `json:"srcUsers" yaml:"src_accounts" validate:"required"`
Quantity int `json:"scrappingQuantity,string" yaml:"fetch_quantity" validate:"numeric,min=1"`
}
// AutoDmYaml is the yaml autodm module configuration representation
type AutoDmYaml struct {
DmTemplates SplitStringSlice `json:"dmTemplates" yaml:"dm_templates" validate:"required"`
Greeting GreetingYaml `json:"greeting" yaml:"greeting"`
Activated bool `json:"dmActivation,string" yaml:"activated"`
}
// GreetingYaml is the yaml dm greeting configuration representation
type GreetingYaml struct {
Template string `json:"greetingTemplate" yaml:"template"`
Activated bool `json:"greetingActivation,string" yaml:"activated"`
}
// QuotasYaml is the yaml quotas module configuration representation
type QuotasYaml struct {
DmDay int `json:"dmDay,string" yaml:"dm_per_day" validate:"numeric,min=1"`
DmHour int `json:"dmHour,string" yaml:"dm_per_hour" validate:"numeric,min=1"`
Activated bool `json:"quotasActivation,string" yaml:"activated"`
}
// ScheduleYaml is the yaml scheduler module configuration representation
type ScheduleYaml struct {
BeginAt string `json:"beginAt" yaml:"begin_at" validate:"contains=:"`
EndAt string `json:"endAt" yaml:"end_at" validate:"contains=:"`
Activated bool `json:"scheduleActivation,string" yaml:"activated"`
}
// BlacklistYaml is the yaml blacklist module configuration representation
type BlacklistYaml struct {
Activated bool `json:"blacklistActivation,string" yaml:"activated"`
}
// SeleniumYaml is the yaml selenium configuration representation
type SeleniumYaml struct {
Proxy proxy.Proxy `json:"proxy" yaml:"proxy"`
}