-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler.go
168 lines (136 loc) · 3.37 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
package main
import (
"fmt"
"io"
"net/http"
"net/url"
"strings"
"sync"
"golang.org/x/net/html"
)
// Crawler crawls all the pages on a specified domain
type Crawler struct {
complete sync.WaitGroup
domain *url.URL
urlsToVisit chan *url.URL
pagesCrawled chan *Page
}
// NewCrawler creates a new crawler
func NewCrawler(domain *url.URL) *Crawler {
return &Crawler{
domain: domain,
urlsToVisit: make(chan *url.URL),
pagesCrawled: make(chan *Page),
}
}
// Crawl starts the crawing process and waits for it to complete
func (crawler *Crawler) Crawl() chan *Page {
results := make(chan *Page)
go crawler.visitPages()
go crawler.processPages(results)
crawler.queueURL(crawler.domain)
go func() {
crawler.complete.Wait()
close(results)
close(crawler.urlsToVisit)
close(crawler.pagesCrawled)
}()
return results
}
func (crawler *Crawler) queueURL(url *url.URL) {
crawler.complete.Add(1)
crawler.urlsToVisit <- url
}
func (crawler *Crawler) visitPages() {
visitedUrls := map[string]bool{}
for url := range crawler.urlsToVisit {
url = crawler.domain.ResolveReference(url)
if url.Hostname() != crawler.domain.Hostname() {
crawler.complete.Done()
continue
}
if _, visited := visitedUrls[url.String()]; visited {
crawler.complete.Done()
continue
}
visitedUrls[url.String()] = true
go crawler.visitPage(url)
}
}
func (crawler *Crawler) processPages(results chan<- *Page) {
for page := range crawler.pagesCrawled {
if page != nil {
for _, link := range page.links {
crawler.queueURL(link)
}
results <- page
}
crawler.complete.Done()
}
}
func (crawler *Crawler) visitPage(url *url.URL) {
resp, err := http.Get(url.String())
if err != nil {
crawler.pagesCrawled <- &Page{location: url, err: err}
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("non-successful response from '%v', status: '%s'", url, resp.Status)
crawler.pagesCrawled <- &Page{location: url, err: err}
return
}
if !isHTMLContent(resp) {
crawler.pagesCrawled <- nil
return
}
crawler.pagesCrawled <- processHTMLContent(url, resp.Body)
}
func isHTMLContent(resp *http.Response) bool {
contentType := resp.Header.Get("Content-Type")
return strings.Contains(contentType, "text/html")
}
func processHTMLContent(url *url.URL, content io.Reader) *Page {
page := &Page{location: url}
doc, err := html.Parse(content)
if err != nil {
page.err = err
return page
}
visitHTMLNode(doc, func(node *html.Node) {
page.extractLinksFromNode(node)
})
return page
}
func visitHTMLNode(node *html.Node, processHTMLElement func(node *html.Node)) {
if node.Type == html.ElementNode {
processHTMLElement(node)
}
for childNode := node.FirstChild; childNode != nil; childNode = childNode.NextSibling {
visitHTMLNode(childNode, processHTMLElement)
}
}
func (page *Page) extractLinksFromNode(node *html.Node) {
if node.Data == "a" {
if href := getAttributeValueAsURL(node, "href"); href != nil {
page.links = append(page.links, href)
}
}
}
func getAttributeValueAsURL(aTag *html.Node, attributeKey string) *url.URL {
for _, urlAttribute := range aTag.Attr {
if urlAttribute.Key != attributeKey {
continue
}
rawURL := strings.TrimSpace(urlAttribute.Val)
if rawURL == "" {
return nil
}
url, err := url.Parse(rawURL)
if err != nil {
return nil
}
return url
}
return nil
}