/
svc.go
205 lines (183 loc) · 6.43 KB
/
svc.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
193
194
195
196
197
198
199
200
201
202
203
204
205
package service
import (
"context"
"github.com/JohnnyTing/rabida/config"
"github.com/chromedp/chromedp"
)
//go:generate go-doudou name --file $GOFILE
type CssSelector struct {
Css string `json:"css"`
// Attr default is innerText
Attr string `json:"attr"`
// Scope supply a scope to each selector
// In jQuery, this would look something like this: $(scope).find(selector)
Scope string `json:"scope"`
// Attrs map each attribute to a css selector. when Attrs equals nil, stop recursively populating
Attrs map[string]CssSelector `json:"attrs"`
// Iframe if true, we will look for the element(s) within the first iframe in the page. if IframeSelector exist, will look for this.
Iframe bool `json:"iframe"`
// IframeSelector specify the iframe selector if have multiple iframe elements
IframeSelector *CssSelector `json:"iframeSelector"`
// XpathScope Note: only choose one between xpath and css selector
XpathScope string `json:"xpathScope"`
// Xpath xpath expression
// eg: //*[@id="zz"]/div[2]/ul/li[1]/text()
// eg: //div[@id="indexCarousel"]//div[@class="item"]//img/@src
Xpath string `json:"xpath"`
SetAttrs []SetAttribute `json:"setAttrs"`
// Before dosomething before retrieve value
Before []EventSelector `json:"before"`
Condition *Condition `json:"condition"`
}
type Job struct {
// Link the url you want to crawl
Link string `json:"link"`
// CssSelector root css selector
CssSelector CssSelector `json:"cssSelector"`
// PrePaginate do something before paginate
PrePaginate []EventSelector `json:"prePaginate"`
// Paginator css selector for next page
Paginator CssSelector `json:"paginator"`
PaginatorFunc func(currentPageNo int) CssSelector
// Limit limits how many pages should be crawled
Limit int `json:"limit"`
StartPageBtn CssSelector `json:"startPageBtn"`
StartPageUrl string `json:"startPageUrl"`
EnableCookies HttpCookies `json:"enableCookies"`
}
type EventSelector struct {
Type Event `json:"type"`
Condition Condition `json:"condition"`
Selector CssSelector `json:"selector"`
}
type HttpCookies struct {
RawCookies string `json:"rawCookies"`
Domain string `json:"domain"`
// Expires hour, default 1 year
Expires int `json:"expires"`
}
type SetAttribute struct {
AttributeName string `json:"attributeName"`
AttributeValue string `json:"attributeValue"`
}
type Condition struct {
Value string `json:"value"`
CheckFunc func(text, value string) bool
ExecSelector ExecSelector `json:"execSelector"`
}
type ExecSelector struct {
Type Event `json:"type"`
Selector CssSelector `json:"selector"`
}
type Event string
const (
ClickEvent Event = "click"
SetAttributesValueEvent Event = "setAttributesValue"
TextEvent Event = "getTextValue"
GetAttributeValueEvent Event = "getAttributeValue"
)
type Rabida interface {
Crawl(ctx context.Context, job Job,
// callback process result
// abort pagination if it returns true
callback func(ret []interface{}, nextPageUrl string, currentPageNo int) bool,
// actions before navigation
before []chromedp.Action,
// actions after navigation
after []chromedp.Action,
) error
CrawlWithConfig(ctx context.Context, job Job,
// callback process result
// abort pagination if it returns true
callback func(ret []interface{}, nextPageUrl string, currentPageNo int) bool,
// actions before navigation
before []chromedp.Action,
// actions after navigation
after []chromedp.Action,
conf config.RabiConfig,
options ...chromedp.ExecAllocatorOption,
) error
CrawlWithListeners(ctx context.Context, job Job,
// callback process result
// abort pagination if it returns true
callback func(ctx context.Context, ret []interface{}, nextPageUrl string, currentPageNo int) bool,
// actions before navigation
before []chromedp.Action,
// actions after navigation
after []chromedp.Action,
confPtr *config.RabiConfig,
options []chromedp.ExecAllocatorOption,
listeners ...func(ev interface{}),
) error
DownloadFile(ctx context.Context, job Job,
// callback process result
// abort pagination if it returns true
callback func(file string),
confPtr *config.RabiConfig,
options ...chromedp.ExecAllocatorOption,
) error
CrawlScroll(ctx context.Context, job Job,
// callback process result
// abort pagination if it returns true
callback func(ret []interface{}, cursor int, currentPageNo int) bool,
// actions before navigation
before []chromedp.Action,
// actions after navigation
after []chromedp.Action,
) error
CrawlScrollWithConfig(ctx context.Context, job Job,
// callback process result
// abort pagination if it returns true
callback func(ret []interface{}, cursor int, currentPageNo int) bool,
// actions before navigation
before []chromedp.Action,
// actions after navigation
after []chromedp.Action,
conf config.RabiConfig,
options ...chromedp.ExecAllocatorOption,
) error
CrawlScrollWithListeners(ctx context.Context, job Job,
// callback process result
// abort pagination if it returns true
callback func(ctx context.Context, ret []interface{}, cursor int, currentPageNo int) bool,
// actions before navigation
before []chromedp.Action,
// actions after navigation
after []chromedp.Action,
confPtr *config.RabiConfig,
options []chromedp.ExecAllocatorOption,
listeners ...func(ev interface{}),
) error
CrawlScrollSmooth(ctx context.Context, job Job,
// callback process result
// abort pagination if it returns true
callback func(ret []interface{}, currentPageNo int) bool,
// actions before navigation
before []chromedp.Action,
// actions after navigation
after []chromedp.Action,
) error
CrawlScrollSmoothWithConfig(ctx context.Context, job Job,
// callback process result
// abort pagination if it returns true
callback func(ret []interface{}, currentPageNo int) bool,
// actions before navigation
before []chromedp.Action,
// actions after navigation
after []chromedp.Action,
conf config.RabiConfig,
options ...chromedp.ExecAllocatorOption,
) error
CrawlScrollSmoothWithListeners(ctx context.Context, job Job,
// callback process result
// abort pagination if it returns true
callback func(ctx context.Context, ret []interface{}, currentPageNo int) bool,
// actions before navigation
before []chromedp.Action,
// actions after navigation
after []chromedp.Action,
confPtr *config.RabiConfig,
options []chromedp.ExecAllocatorOption,
listeners ...func(ev interface{}),
) error
}