-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.go
149 lines (131 loc) · 2.87 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
package main
import (
"errors"
"fmt"
"golang.org/x/net/html"
"log"
"net/http"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
)
type crawler struct {
startPath string
host string
pages *Set
withPrefix string
stack *[]string
withLogs bool
timeout time.Duration
}
type CrawlerBuilder struct {
startPath string
host string
pages *Set
withPrefix string
withLogs bool
timeout time.Duration
}
func NewCrawlerBuilder() *CrawlerBuilder {
return &CrawlerBuilder{}
}
func (b *CrawlerBuilder) WithHost(host string) *CrawlerBuilder {
b.host = host
return b
}
func (b *CrawlerBuilder) WithStartPath(startPath string) *CrawlerBuilder {
b.startPath = startPath
return b
}
func (b *CrawlerBuilder) WithPrefix(prefix string) *CrawlerBuilder {
b.withPrefix = prefix
return b
}
func (b *CrawlerBuilder) WithLogs(withLogs bool) *CrawlerBuilder {
b.withLogs = withLogs
return b
}
func (b *CrawlerBuilder) WithTimeout(timeout time.Duration) *CrawlerBuilder {
b.timeout = timeout
return b
}
func (b *CrawlerBuilder) Build() *crawler {
return &crawler{
startPath: b.startPath,
host: b.host,
pages: NewSet(),
withPrefix: b.withPrefix,
stack: &[]string{},
withLogs: b.withLogs,
timeout: b.timeout,
}
}
func (c crawler) crawl() {
*c.stack = append(*c.stack, c.startPath)
for len(*c.stack) > 0 {
time.Sleep(c.timeout)
n := len(*c.stack) - 1
page := (*c.stack)[n]
*c.stack = (*c.stack)[:n]
err := c.processPage(&page)
if err != nil {
log.Println(err.Error())
continue
}
}
}
func (c crawler) processPage(page *string) error {
doc, err := c.getPage(page)
if err != nil {
return err
}
c.processDoc(doc)
return nil
}
func (c crawler) processDoc(doc *goquery.Document) {
for _, node := range doc.Find("a").Nodes {
c.processTag(node)
}
}
func (c crawler) processTag(node *html.Node) {
for _, attr := range node.Attr {
if attr.Key == "href" {
c.processAttribute(attr.Val)
}
}
}
func (c crawler) processAttribute(address string) {
if c.withPrefix != "" {
if strings.HasPrefix(address, c.withPrefix) {
c.addPage(address)
}
} else {
c.addPage(address)
}
}
func (c crawler) addPage(address string) {
if !c.pages.Has(address) {
c.pages.Add(address)
*c.stack = append(*c.stack, address)
if c.withLogs {
log.Printf("Found page %s", address)
}
}
}
func (c crawler) getPage(page *string) (*goquery.Document, error) {
res, err := http.Get(c.host + *page)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
log.Printf("status code error for page %s: %d %s\n", *page, res.StatusCode, res.Status)
return nil, errors.New(fmt.Sprintf("status code error for page %s: %d %s\n", *page, res.StatusCode, res.Status))
}
// Load the HTML document
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return nil, err
}
return doc, nil
}