forked from hbollon/IGopher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.go
More file actions
304 lines (272 loc) · 8.37 KB
/
bot.go
File metadata and controls
304 lines (272 loc) · 8.37 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package igopher
import (
"context"
"errors"
"flag"
"fmt"
"math"
"math/rand"
"time"
"github.com/hbollon/igopher/internal/process"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
)
// BotStruct is the main struct instance used by this bot
var BotStruct IGopher
// Flags declarations
var Flags = struct {
// LogLevelFlag set loglevel threshold
// If undefined or wrong set it to INFO level
LogLevelFlag *string
// ForceDlFlag force re-download of all dependencies
ForceDlFlag *bool
// DebugFlag set selenium debug mode and display its logging to stderr
DebugFlag *bool
// DevToolsFlag launch Electron gui with devtools openned
DevToolsFlag *bool
// IgnoreDependenciesFlag disable dependencies manager on startup
IgnoreDependenciesFlag *bool
// BackgroundFlag IGopher as background task with actual configuration and ignore TUI
BackgroundFlag *bool
// HeadlessFlag execute Selenium webdriver in headless mode
HeadlessFlag *bool
// PortFlag specifie custom communication port for Selenium and web drivers
PortFlag *int
}{
LogLevelFlag: flag.String("loglevel", "info", "Log level threshold"),
ForceDlFlag: flag.Bool("force-download", false, "Force redownload of all dependencies even if exists"),
DebugFlag: flag.Bool("debug", false, "Display debug and selenium output"),
DevToolsFlag: flag.Bool("dev-tools", false, "Launch Electron gui with dev tools openned"),
IgnoreDependenciesFlag: flag.Bool("ignore-dependencies", false, "Skip dependencies management"),
HeadlessFlag: flag.Bool("headless", false, "Run WebDriver with frame buffer"),
PortFlag: flag.Int("port", 8080, "Specify custom communication port"),
}
// errStopBot is used to trigger bot stopping from some function
var errStopBot = errors.New("Bot stop process triggered")
func initClientConfig() *ClientConfig {
clientConfig := CreateClientConfig()
clientConfig.LogLevel, _ = log.ParseLevel(*Flags.LogLevelFlag)
clientConfig.ForceDependenciesDl = *Flags.ForceDlFlag
clientConfig.Debug = *Flags.DebugFlag
clientConfig.DevTools = *Flags.DevToolsFlag
clientConfig.IgnoreDependencies = *Flags.IgnoreDependenciesFlag
clientConfig.Headless = *Flags.HeadlessFlag
if *Flags.PortFlag > math.MaxUint16 || *Flags.PortFlag < 8080 {
log.Warnf("Invalid port argument '%d'. Use default 8080.", *Flags.PortFlag)
} else {
clientConfig.Port = uint16(*Flags.PortFlag)
}
return clientConfig
}
// LaunchBotTui start dm bot on main goroutine
func LaunchBotTui() {
// Initialize client configuration
var err error
clientConfig := initClientConfig()
BotStruct, err = ReadBotConfigYaml()
if err != nil {
logrus.Warn(err)
}
// Download dependencies
if !clientConfig.IgnoreDependencies {
DownloadDependencies(true, true, clientConfig.ForceDependenciesDl)
}
// Initialize Selenium and WebDriver and defer their closing
BotStruct.SeleniumStruct.InitializeSelenium(clientConfig)
BotStruct.SeleniumStruct.InitChromeWebDriver()
defer BotStruct.SeleniumStruct.CloseSelenium()
process.DumpProcessPidToFile()
rand.Seed(time.Now().Unix())
if err = BotStruct.Scheduler.CheckTime(); err == nil {
BotStruct.ConnectToInstagram()
for {
var users []string
users, err = BotStruct.FetchUsersFromUserFollowers()
if err != nil {
BotStruct.SeleniumStruct.Fatal("Failed users fetching: ", err)
}
for _, username := range users {
var res bool
res, err = BotStruct.SendMessage(username, BotStruct.DmModule.DmTemplates[rand.Intn(len(BotStruct.DmModule.DmTemplates))])
if !res || err != nil {
log.Errorf("Error during message sending: %v", err)
}
}
}
} else {
BotStruct.SeleniumStruct.Fatal("Error on bot launch: ", err)
}
}
func checkBotChannels() bool {
select {
case <-BotStruct.hotReloadCallback:
if err := BotStruct.HotReload(); err != nil {
logrus.Errorf("Bot hot reload failed: %v", err)
BotStruct.hotReloadCallback <- false
} else {
logrus.Info("Bot hot reload successfully.")
BotStruct.hotReloadCallback <- true
}
break
case <-BotStruct.reloadCallback:
logrus.Info("Bot reload successfully.")
break
case <-BotStruct.exitCh:
logrus.Info("Bot process successfully stopped.")
return true
default:
break
}
return false
}
// Initialize client and bot configs, download dependencies,
// launch Selenium instance and finally run dm bot routine
func launchBot(ctx context.Context) {
// Initialize client configuration
var err error
clientConfig := initClientConfig()
BotStruct, err = ReadBotConfigYaml()
if err != nil {
logrus.Warn(err)
}
BotStruct.running = true
// Download dependencies
if !clientConfig.IgnoreDependencies {
DownloadDependencies(true, true, clientConfig.ForceDependenciesDl)
}
// Initialize Selenium and WebDriver and defer their closing
BotStruct.SeleniumStruct.InitializeSelenium(clientConfig)
BotStruct.SeleniumStruct.InitChromeWebDriver()
defer BotStruct.SeleniumStruct.CloseSelenium()
defer BotStruct.SeleniumStruct.Proxy.StopForwarderProxy()
// Creation of needed communication channels and deferring their closing
exitedCh = make(chan bool)
defer close(exitedCh)
hotReloadCh = make(chan bool)
defer close(hotReloadCh)
reloadCh = make(chan bool)
defer close(reloadCh)
BotStruct.infoCh = make(chan string)
defer close(BotStruct.infoCh)
BotStruct.errCh = make(chan string)
defer close(BotStruct.errCh)
BotStruct.crashCh = make(chan error)
defer close(BotStruct.crashCh)
BotStruct.exitCh = make(chan bool)
defer close(BotStruct.exitCh)
BotStruct.reloadCallback = make(chan bool)
defer close(BotStruct.reloadCallback)
BotStruct.hotReloadCallback = make(chan bool)
defer close(BotStruct.hotReloadCallback)
process.DumpProcessPidToFile()
// Start bot routine
go func() {
defer func() {
if r := recover(); r != nil {
log.Errorf("Unknown error: %v", r)
SendMessageToElectron(
MessageOut{
Status: ERROR,
Msg: "bot crash",
Payload: fmt.Errorf("Unknown error: %v", r),
},
)
BotStruct.running = false
}
}()
rand.Seed(time.Now().Unix())
if err = BotStruct.Scheduler.CheckTime(); err == nil {
if exit := checkBotChannels(); exit {
return
}
BotStruct.ConnectToInstagram()
for {
var users []string
if exit := checkBotChannels(); exit {
return
}
users, err = BotStruct.FetchUsersFromUserFollowers()
if err != nil {
BotStruct.crashCh <- fmt.Errorf("Failed users fetching: %v. Check logs tab for more details", err)
return
}
for _, username := range users {
if exit := checkBotChannels(); exit {
return
}
var res bool
res, err = BotStruct.SendMessage(username, BotStruct.DmModule.DmTemplates[rand.Intn(len(BotStruct.DmModule.DmTemplates))])
if !res || err != nil {
BotStruct.errCh <- fmt.Sprintf("Error during message sending: %v", err)
log.Errorf("Error during message sending: %v", err)
}
}
}
} else {
if err == errStopBot {
return
}
BotStruct.crashCh <- err
BotStruct.SeleniumStruct.Fatal("Error on bot launch: ", err)
}
}()
var msg string
for {
select {
case msg = <-BotStruct.infoCh:
log.Infof("infoCh: %s", msg)
break
case msg = <-BotStruct.errCh:
log.Errorf("errCh: %s", msg)
break
case err := <-BotStruct.crashCh:
log.Errorf("crashCh: %v", err)
SendMessageToElectron(
MessageOut{
Status: ERROR,
Msg: "bot crash",
Payload: err.Error(),
},
)
BotStruct.running = false
return
case <-hotReloadCh:
BotStruct.hotReloadCallback <- true
if <-BotStruct.hotReloadCallback {
hotReloadCh <- true
} else {
hotReloadCh <- false
}
break
case <-reloadCh:
BotStruct.reloadCallback <- true
return
case <-ctx.Done():
BotStruct.exitCh <- true
exitedCh <- true
BotStruct.running = false
return
default:
break
}
if ws, err := BotStruct.SeleniumStruct.WebDriver.WindowHandles(); len(ws) == 0 || err != nil {
BotStruct.SeleniumStruct.CleanUp()
return
}
time.Sleep(10 * time.Millisecond)
}
}
// 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
}