This repository has been archived by the owner on May 11, 2022. It is now read-only.
/
crawler.go
161 lines (145 loc) · 4.18 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
package availability
import (
"fmt"
"io"
"net/http"
"net/url"
"strings"
"github.com/gocolly/colly/v2"
"github.com/gocolly/colly/v2/debug"
"go.octolab.org/unsafe"
"github.com/kamilsk/check/errors"
)
const (
locationHeader = "Location"
clickOptHeader = "X-Click-Options"
)
var clickOptions = []string{"anonym", "nolog"}
// Crawler defines general behavior of website crawlers.
type Crawler interface {
// Visit starts to crawl a website starting with the passed URL.
// All implementations must communicate with report builder
// through the passed EventBus and close it when they're done.
Visit(url string, bus EventBus) error
}
// CrawlerConfig holds a website crawler's configuration.
type CrawlerConfig struct {
UserAgent string
Verbose bool
Output io.Writer
}
// CrawlerFunc adds possibility to use functions as a website crawler.
type CrawlerFunc func(string, EventBus) error
// Visit calls itself.
func (fn CrawlerFunc) Visit(url string, bus EventBus) error { return fn(url, bus) }
// CrawlerColly returns configured website crawler.
func CrawlerColly(config CrawlerConfig) Crawler {
return CrawlerFunc(func(entry string, bus EventBus) error {
defer close(bus)
base, err := url.Parse(entry)
if err != nil {
return errors.WithMessage(err, fmt.Sprintf("parse entry point URL %q", entry))
}
options := make([]colly.CollectorOption, 0, 9)
if config.UserAgent != "" {
options = append(options, colly.UserAgent(config.UserAgent))
}
if config.Verbose {
options = append(options, colly.Debugger(&debug.LogDebugger{Output: config.Output}))
}
options = append(options,
colly.IgnoreRobotsTxt(),
NoCookie(),
NoRedirect(),
OnRequest(),
OnError(bus),
OnResponse(bus),
OnHTML(base, bus),
)
return colly.NewCollector(options...).Visit(entry)
})
}
// NoRedirect disables redirects for `github.com/gocolly/colly.Collector`.
func NoRedirect() func(*colly.Collector) {
return func(c *colly.Collector) {
c.SetRedirectHandler(func(*http.Request, []*http.Request) error {
return http.ErrUseLastResponse
})
}
}
// NoCookie disables cookie for `github.com/gocolly/colly.Collector`.
func NoCookie() func(*colly.Collector) {
return func(c *colly.Collector) {
c.DisableCookies()
}
}
// OnRequest registers a callback by `github.com/gocolly/colly.Collector.OnRequest()`.
func OnRequest() func(*colly.Collector) {
options := strings.Join(clickOptions, ";")
return func(c *colly.Collector) {
c.OnRequest(func(req *colly.Request) {
req.Headers.Set(clickOptHeader, options)
})
}
}
// OnError registers a callback by `github.com/gocolly/colly.Collector.OnError()`.
func OnError(bus EventBus) func(*colly.Collector) {
return func(c *colly.Collector) {
c.OnError(func(resp *colly.Response, err error) {
location, redirect := resp.Request.URL.String(), ""
if resp.Headers != nil {
redirect = resp.Headers.Get(locationHeader)
}
bus <- ErrorEvent{
StatusCode: resp.StatusCode,
Location: location,
Redirect: redirect,
Error: err,
}
})
}
}
// OnResponse registers a callback by `github.com/gocolly/colly.Collector.OnResponse()`.
func OnResponse(bus EventBus) func(*colly.Collector) {
return func(c *colly.Collector) {
c.OnResponse(func(resp *colly.Response) {
location := resp.Request.URL.String()
bus <- ResponseEvent{
StatusCode: resp.StatusCode,
Location: location,
}
})
}
}
// OnHTML registers a callback by `github.com/gocolly/colly.Collector.OnHTML()`.
func OnHTML(base *url.URL, bus EventBus) func(*colly.Collector) {
isPage := func(current *url.URL) bool {
return current.Host == base.Host
}
return func(c *colly.Collector) {
c.OnHTML("a[href]", func(el *colly.HTMLElement) {
if isPage(el.Request.URL) {
attr := el.Attr("href")
if strings.HasPrefix(attr, "#") {
return
}
href := el.Request.AbsoluteURL(attr)
if href == "" {
bus <- ProblemEvent{Message: "bad url", Context: struct {
Page string
Href string
}{el.Request.URL.String(), attr}}
return
}
if !strings.HasPrefix(href, "http") {
return
}
bus <- WalkEvent{
Page: el.Request.URL.String(),
Href: href,
}
unsafe.Ignore(el.Request.Visit(href))
}
})
}
}