-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrawler.go
226 lines (195 loc) · 4.51 KB
/
crawler.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package chuper
import (
"net/http"
"os"
"time"
"github.com/PuerkitoBio/fetchbot"
"github.com/PuerkitoBio/goquery"
"github.com/Sirupsen/logrus"
)
const (
DefaultCrawlDelay = 5 * time.Second
DefaultCrawlPoliteness = false
DefaultLogFormat = "text"
DefaultLogLevel = "info"
DefaultUserAgent = fetchbot.DefaultUserAgent
)
var (
DefaultHTTPClient = http.DefaultClient
DefaultCache = NewMemoryCache()
)
type Crawler struct {
CrawlDelay time.Duration
CrawlDuration time.Duration
CrawlPoliteness bool
LogFormat string
LogLevel string
Logger *logrus.Logger
UserAgent string
HTTPClient fetchbot.Doer
Cache Cache
mux *fetchbot.Mux
f *fetchbot.Fetcher
q *fetchbot.Queue
}
// New returns an initialized Crawler.
func New() *Crawler {
return &Crawler{
CrawlDelay: DefaultCrawlDelay,
CrawlPoliteness: DefaultCrawlPoliteness,
LogFormat: DefaultLogFormat,
LogLevel: DefaultLogLevel,
UserAgent: DefaultUserAgent,
HTTPClient: DefaultHTTPClient,
Cache: DefaultCache,
mux: fetchbot.NewMux(),
}
}
func (c *Crawler) Start() Enqueuer {
if c.Logger == nil {
c.Logger = newLogger(c.LogFormat, c.LogLevel)
}
c.mux.HandleErrors(c.newErrorHandler())
h := c.newRequestHandler()
f := fetchbot.New(h)
f.CrawlDelay = c.CrawlDelay
f.DisablePoliteness = !c.CrawlPoliteness
f.HttpClient = c.HTTPClient
f.UserAgent = c.UserAgent
c.f = f
c.q = c.f.Start()
if c.CrawlDuration > 0 {
go func() {
t := time.After(c.CrawlDuration)
<-t
c.q.Close()
}()
}
return &Queue{c.q}
}
func (c *Crawler) Block() {
c.q.Block()
}
func (c *Crawler) Finish() {
c.q.Close()
}
type ResponseCriteria struct {
Method string
ContentType string
Status int
MinStatus int
MaxStatus int
Path string
Host string
}
func (c *Crawler) Match(r *ResponseCriteria) *fetchbot.ResponseMatcher {
m := c.mux.Response()
if r.Method != "" {
m.Method(r.Method)
}
if r.ContentType != "" {
m.ContentType(r.ContentType)
}
if r.Status != 0 {
m.Status(r.Status)
} else {
if r.MinStatus != 0 && r.MaxStatus != 0 {
m.StatusRange(r.MinStatus, r.MaxStatus)
} else {
if r.MinStatus != 0 {
m.Status(r.MinStatus)
}
if r.MaxStatus != 0 {
m.Status(r.MaxStatus)
}
}
}
if r.Path != "" {
m.Path(r.Path)
}
if r.Host != "" {
m.Host(r.Host)
}
return m
}
func (c *Crawler) Register(rc *ResponseCriteria, procs ...Processor) {
m := c.Match(rc)
h := c.newHTMLHandler(procs...)
m.Handler(h)
}
func newLogger(format, level string) *logrus.Logger {
log := logrus.New()
log.Out = os.Stdout
log.Formatter = newFormatter(format)
log.Level = parseLogLevel(level)
return log
}
func newFormatter(format string) logrus.Formatter {
switch format {
case "text", "":
return &logrus.TextFormatter{}
case "json":
return &logrus.JSONFormatter{}
default:
return &logrus.TextFormatter{}
}
}
func parseLogLevel(level string) logrus.Level {
switch level {
case "panic":
return logrus.PanicLevel
case "fatal":
return logrus.FatalLevel
case "error":
return logrus.ErrorLevel
case "warn", "warning":
return logrus.WarnLevel
case "info":
return logrus.InfoLevel
case "debug":
return logrus.DebugLevel
default:
return logrus.InfoLevel
}
}
func (c *Crawler) newErrorHandler() fetchbot.Handler {
return fetchbot.HandlerFunc(func(ctx *fetchbot.Context, res *http.Response, err error) {
c.Logger.WithFields(logrus.Fields{
"url": ctx.Cmd.URL(),
"method": ctx.Cmd.Method(),
}).Error(err)
})
}
func (c *Crawler) newRequestHandler() fetchbot.Handler {
return fetchbot.HandlerFunc(func(ctx *fetchbot.Context, res *http.Response, err error) {
if res != nil {
context := &Ctx{ctx, c.Cache, c.Logger}
c.Logger.WithFields(logrus.Fields{
"method": context.Method(),
"status": res.StatusCode,
"content_type": res.Header.Get("Content-Type"),
"depth": context.Depth(),
}).Info(context.URL())
}
c.mux.Handle(ctx, res, err)
})
}
func (c *Crawler) newHTMLHandler(procs ...Processor) fetchbot.Handler {
return fetchbot.HandlerFunc(func(ctx *fetchbot.Context, res *http.Response, err error) {
context := &Ctx{ctx, c.Cache, c.Logger}
doc, err := goquery.NewDocumentFromResponse(res)
if err != nil {
c.Logger.WithFields(logrus.Fields{
"url": context.URL(),
"method": context.Method(),
}).Error(err)
return
}
for _, p := range procs {
ok := p.Process(context, doc)
if !ok {
return
}
}
})
}