/
crawler.go
199 lines (174 loc) · 3.94 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
package crawler
import (
"bytes"
"fmt"
"github.com/PuerkitoBio/fetchbot"
"github.com/PuerkitoBio/goquery"
"github.com/fatih/color"
"net/http"
"net/http/httputil"
"net/url"
"sync"
"time"
)
type Crawler struct {
store Store
fetchbot *fetchbot.Fetcher
client fetchbot.Doer
allowedSites map[string]struct{}
dupMu sync.Mutex
dup map[string]struct{}
q *fetchbot.Queue
}
func New(cnf Config) (*Crawler, error) {
store, e := Open(cnf.Dbdriver, cnf.Dbcnf)
if e != nil {
return nil, e
}
cr := &Crawler{
store: store,
client: &Doer{},
dup: map[string]struct{}{},
allowedSites: map[string]struct{}{},
}
mux := fetchbot.NewMux()
mux.HandleErrors(fetchbot.HandlerFunc(func(ctx *fetchbot.Context, res *http.Response, err error) {
color.Red("[ERR] %s %s - %s\n", ctx.Cmd.Method(), ctx.Cmd.URL(), err)
}))
mux.Response().Status(200).ContentType("text/html").Handler(fetchbot.HandlerFunc(cr.Handler))
cr.fetchbot = fetchbot.New(mux)
cr.fetchbot.HttpClient = cr.client
cr.fetchbot.DisablePoliteness = true
cr.fetchbot.CrawlDelay = time.Second * 1
cr.fetchbot.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"
return cr, nil
}
func (cr *Crawler) Run(links ...string) {
color.Green("Run crawler")
vi, _ := cr.store.Visited()
for _, v := range vi {
cr.dup[v.URL().String()] = struct{}{}
}
color.Green("Current link count: %d(visited: %d)", cr.store.CountLink(), len(vi))
for _, v := range links {
u, e := url.Parse(v)
if e != nil {
continue
}
site, has := cr.store.GetSite(u.Host)
if !has {
site = NewSite(u.Host, true)
} else {
site.Allowed = true
}
e = cr.store.SaveSite(site)
if e != nil {
color.Red(e.Error())
}
}
as, _ := cr.store.AllowedSites()
color.Green("Current site count: %d, allowed %d", cr.store.CountSite(), len(as))
for _, v := range as {
if cr.allowedSites == nil {
cr.allowedSites = map[string]struct{}{}
}
cr.allowedSites[v.Host] = struct{}{}
}
q := cr.fetchbot.Start()
for _, v := range links {
cr.dup[v] = struct{}{}
}
_, e := q.SendStringGet(links...)
if e != nil {
color.Red(e.Error())
}
cr.q = q
lnks, e := cr.store.NotVisited()
if e != nil {
color.Red(e.Error())
}
for _, v := range lnks {
l := v.URL()
clearUrl(l)
if cr.NeedVisit(l) {
q.SendStringGet(l.String())
}
}
}
func (cr *Crawler) Stop() {
cr.q.Cancel()
}
func (cr *Crawler) Block() {
if cr.q != nil {
cr.q.Block()
}
}
func ExtractLinks(url *url.URL, in []byte) (res []*url.URL) {
doc, e := goquery.NewDocumentFromReader(bytes.NewReader(in))
if e != nil {
return
}
dup := map[string]struct{}{}
doc.Find("a[href]").Each(func(i int, s *goquery.Selection) {
val, _ := s.Attr("href")
// Resolve address
u, err := url.Parse(val)
if err != nil {
return
}
clearUrl(u)
if err != nil {
fmt.Printf("error: resolve URL %s - %s\n", val, err)
return
}
if _, ok := dup[u.String()]; !ok {
res = append(res, u)
} else {
dup[u.String()] = struct{}{}
}
})
return
}
func clearUrl(u *url.URL) {
//u.RawQuery = ""
u.Fragment = ""
}
func (cr *Crawler) Get(rawurl string, forceFetch bool) (link *Link, blob []byte, e error) {
var (
u *url.URL
has bool
req *http.Request
resp *http.Response
bts []byte
)
u, e = url.Parse(rawurl)
if e != nil {
return
}
link, has = cr.store.GetLink(u.RequestURI())
if has && !forceFetch && !link.Visited.IsZero() {
blob, e = link.GetBlob(cr.store)
color.Cyan("From db")
return
}
req, e = http.NewRequest("GET", rawurl, nil)
if e != nil {
return
}
resp, e = cr.fetchbot.HttpClient.Do(req)
if e != nil {
return
}
defer resp.Body.Close()
blob, e = httputil.DumpResponse(resp, true)
if e != nil {
return
}
color.Yellow("%d", len(bts))
link = NewLink(u)
cr.store.SaveBlob(link.URL().String(), blob)
link.Visited = time.Now()
e = cr.store.SaveLink(link)
return
}
var ErrNotFound = fmt.Errorf("not found")