-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.go
More file actions
453 lines (370 loc) · 9.42 KB
/
bot.go
File metadata and controls
453 lines (370 loc) · 9.42 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package wee
import (
"log"
"time"
"github.com/coghost/xpretty"
"github.com/coghost/zlog"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/launcher"
"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/strutil"
"github.com/samber/lo"
"go.uber.org/zap"
)
const (
_uniqueIDLen = 16
_browserChrome = "Google Chrome"
)
type Bot struct {
// UniqueID is the identifier of a bot.
UniqueID string
logger *zap.Logger
// trackTime tracks the time spend on operation.
trackTime bool
// init behaviours
launcher *launcher.Launcher
browser *rod.Browser
page *rod.Page
prevPage *rod.Page
root *rod.Element
left int
windowMaximize bool
// bounds of browser
bounds *BrowserBounds
isLaunched bool
withPageCreation bool
humanized bool
stealthMode bool
// launcher/browser options
userMode bool
headless bool
devtools bool
userAgent string
acceptLanguage string
userDataDir string
// when in userMode, by default will skip cleanup, we can set forceCleanup to do the cleanup.
forceCleanup bool
// clearCookies used when we want to clear all cookies in user-mode
clearCookies bool
// cookies
withCookies bool
cookieFolder string
cookieFile string
// copyAsCURLCookies reads `copy as cURL` directly from clipboard
copyAsCURLCookies []byte
// scrollHeight is the last valid height, use as fallback value.
scrollHeight float64
// highlightTimes highlight element times before next operation.
highlightTimes int
// popovers are anything that popup and block normal actions,
// if popovers is set, will try to interact with it every time before doing input/click operations.
popovers []string
panicBy PanicByType
longTimeout time.Duration
mediumTimeout time.Duration
shortTimeout time.Duration
napTimeout time.Duration
pt10s time.Duration
pt1s time.Duration
browserOptions []BrowserOptionFunc
// unused options
// innerHeight int
// presetOptions []BotOption
// windowSize []int
// viewSize []int
// incognito bool
}
func NewBot(options ...BotOption) *Bot {
bot := &Bot{withPageCreation: true}
bot.initialize()
bindBotOptions(bot, options...)
resetIfHeadless(bot)
bot.CustomizePage()
return bot
}
// NewBotWithOptionsOnly creates a new Bot instance with options only, without creating a page.
func NewBotWithOptionsOnly(options ...BotOption) *Bot {
options = append(options, WithPage(false))
return NewBot(options...)
}
// BindBotLanucher launches the browser and page for the bot.
// This is used when we create a bot first and launch the browser elsewhere.
func BindBotLanucher(bot *Bot, options ...BotOption) {
if bot.isLaunched {
return
}
var (
lnchr *launcher.Launcher
brw *rod.Browser
)
if bot.userMode {
if bot.forceCleanup {
err := ForceQuitBrowser(_browserChrome, 5) //nolint: mnd
if err != nil {
log.Fatalf("cannot close chrome: %v", err)
}
}
// lnchr, brw = NewUserMode(LaunchLeakless(bot.forceCleanup), BrowserUserDataDir(bot.userDataDir))
lnchr, brw = NewUserMode(bot.browserOptions...)
} else {
lnchr, brw = NewBrowser(bot.browserOptions...)
}
options = append(options, Launcher(lnchr), Browser(brw), WithPage(true))
bindBotOptions(bot, options...)
resetIfHeadless(bot)
bot.CustomizePage()
}
func resetIfHeadless(bot *Bot) {
if bot.headless {
l, brw := NewBrowser(BrowserHeadless(bot.headless))
bot.launcher = l
bot.browser = brw
}
}
// NewBotDefault creates a bot with a default launcher/browser.
// The launcher/browser passed in will be ignored.
func NewBotDefault(options ...BotOption) *Bot {
l, brw := NewBrowser()
options = append(options, Launcher(l), Browser(brw))
return NewBot(options...)
}
// NewBotHeadless sets headless with NewBotDefault
func NewBotHeadless(options ...BotOption) *Bot {
options = append(options, Headless(true))
return NewBotDefault(options...)
}
// NewBotForDebug creates a bot which launches browser with option: `--show-paint-rects`,
//
// refer: https://peter.sh/experiments/chromium-command-line-switches/#show-paint-rects
func NewBotForDebug(options ...BotOption) *Bot {
l, brw := NewBrowser(BrowserPaintRects(true))
options = append(options, Launcher(l), Browser(brw))
return NewBot(options...)
}
// NewBotUserMode creates a bot that connects to the system Chrome browser.r
func NewBotUserMode(options ...BotOption) *Bot {
l, brw := NewUserMode()
options = append(options, Launcher(l), Browser(brw), UserMode(true))
return NewBot(options...)
}
// initialize inits all attributes not exposed with options
func (b *Bot) initialize() {
b.logger = zlog.MustNewZapLogger()
b.highlightTimes = 1
b.SetTimeout()
b.UniqueID = strutil.RandomCharsV3(_uniqueIDLen)
}
// BlockInCleanUp performs cleanup and blocks execution.
func (b *Bot) BlockInCleanUp() {
defer b.Cleanup()
defer Blocked()
}
// Cleanup closes the opened page and quits the browser in non-userMode.
// In userMode, by default it will skip cleanup.
func (b *Bot) Cleanup() {
if !b.isLaunched {
return
}
// non user mode, close and clean.
if !b.userMode {
b.browser.MustClose()
b.launcher.Cleanup()
return
}
// by default is not force mode, just return.
if !b.forceCleanup {
b.logger.Info("runs in user mode, skip cleanup browser, you should call `bot.Page().Close()` manually.")
return
}
// in case cookie matters the crawler result.
if b.clearCookies {
b.ClearStorageCookies()
_ = RandSleepNap()
}
b.browser.MustClose()
b.launcher.Cleanup()
}
// ClearStorageCookies calls StorageClearCookies, clears all history cookies.
func (b *Bot) ClearStorageCookies() {
err := b.browser.SetCookies(nil)
if err != nil {
b.logger.Error("cannot clear cookies", zap.Error(err))
} else {
b.logger.Info("cleared cookies before quit.")
}
}
func (b *Bot) SetTimeout() {
b.longTimeout = LongToSec * time.Second
b.mediumTimeout = MediumToSec * time.Second
b.shortTimeout = ShortToSec * time.Second
b.napTimeout = NapToSec * time.Second
b.pt10s = PT10Sec * time.Second
b.pt1s = 1 * time.Second
}
func (b *Bot) SetPanicWith(panicWith PanicByType) {
b.panicBy = panicWith
}
func (b *Bot) pie(err error) {
if err == nil {
return
}
switch b.panicBy {
case PanicByDump:
dump.P(err)
xpretty.DumpCallerStack()
case PanicByLogError:
b.logger.Error("error happens", zap.Error(err))
default:
dump.P(err)
b.logger.Panic("error happens", zap.Error(err))
}
}
func (b *Bot) Page() *rod.Page {
return b.page
}
func (b *Bot) Browser() *rod.Browser {
return b.browser
}
func (b *Bot) CurrentURL() string {
return b.page.MustInfo().URL
}
func (b *Bot) CookieFile() string {
return b.cookieFile
}
/** bot init options **/
type BotOption func(*Bot)
func bindBotOptions(b *Bot, opts ...BotOption) {
for _, f := range opts {
f(b)
}
}
func Launcher(l *launcher.Launcher) BotOption {
return func(o *Bot) {
o.launcher = l
}
}
func Browser(brw *rod.Browser) BotOption {
return func(o *Bot) {
o.browser = brw
}
}
func UserAgent(s string) BotOption {
return func(o *Bot) {
o.userAgent = s
}
}
func AcceptLanguage(s string) BotOption {
return func(o *Bot) {
o.acceptLanguage = s
}
}
func UserDataDir(s string) BotOption {
return func(o *Bot) {
o.userDataDir = s
o.browserOptions = append(o.browserOptions, BrowserUserDataDir(s))
}
}
func WithBrowserOptions(browserOptions []BrowserOptionFunc) BotOption {
return func(o *Bot) {
o.browserOptions = append(o.browserOptions, browserOptions...)
}
}
func WithHighlightTimes(i int) BotOption {
return func(o *Bot) {
o.highlightTimes = i
}
}
func WithPage(b bool) BotOption {
return func(o *Bot) {
o.withPageCreation = b
}
}
// WithCookies will automatically load cookies from current dir for ".cookies/xxx".
func WithCookies(b bool) BotOption {
return func(o *Bot) {
o.withCookies = b
}
}
// WithCookieFolder will load cookies from folder passed in.
func WithCookieFolder(s string) BotOption {
return func(o *Bot) {
o.cookieFolder = s
}
}
// WithCookieFile uses the cookie file specified,
// cookie file should be json array.
func WithCookieFile(s string) BotOption {
return func(o *Bot) {
o.cookieFile = s
}
}
// CopyAsCURLCookies reads the raw content `Copy as cURL` from clipboard
func CopyAsCURLCookies(b []byte) BotOption {
return func(o *Bot) {
o.copyAsCURLCookies = b
}
}
func UserMode(b bool) BotOption {
return func(o *Bot) {
o.userMode = b
}
}
func Headless(b bool) BotOption {
return func(o *Bot) {
o.headless = b
}
}
func Devtools(b bool) BotOption {
return func(o *Bot) {
o.devtools = b
}
}
func Humanized(b bool) BotOption {
return func(o *Bot) {
o.humanized = b
}
}
func StealthMode(b bool) BotOption {
return func(o *Bot) {
o.stealthMode = b
}
}
func ForceCleanup(b bool) BotOption {
return func(o *Bot) {
o.forceCleanup = b
}
}
func ClearCookies(b bool) BotOption {
return func(o *Bot) {
o.clearCookies = b
}
}
// TrackTime tracktime shows logs with level `debug`.
func TrackTime(b bool) BotOption {
return func(o *Bot) {
o.trackTime = b
}
}
func Logger(l *zap.Logger) BotOption {
return func(o *Bot) {
o.logger = l
}
}
func WithPanicBy(i PanicByType) BotOption {
return func(o *Bot) {
o.panicBy = i
}
}
// WithPopovers is useful when popovers appear randomly.
func WithPopovers(popovers ...string) BotOption {
return func(o *Bot) {
o.popovers = append(o.popovers, popovers...)
o.popovers = lo.Uniq(o.popovers)
}
}
// WithLeftPosition sets the left position of the browser window.
func WithLeftPosition(i int) BotOption {
return func(o *Bot) {
o.left = i
}
}