-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_crawler.go
140 lines (117 loc) · 2.86 KB
/
web_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
/*
In this exercise you'll use Go's concurrency features to parallelize a web crawler.
Modify the Crawl function to fetch URLs in parallel without fetching the same URL twice.
Hint: you can keep a cache of the URLs that have been fetched on a map, but maps alone are not safe for concurrent use!
*/
package main
import (
"fmt"
"sync"
)
type Fetcher interface {
// Fetch returns the body of URL and
// a slice of URLs found on that page.
Fetch(url string) (body string, urls []string, err error)
}
type fetchResponse struct {
url string
body string
err error
}
type fetchedResults struct {
responses map[string]fetchResponse
mux sync.Mutex
wg sync.WaitGroup
}
// Crawl uses fetcher to recursively crawl
// pages starting with url, to a maximum of depth.
func Crawl(url string, depth int, fetcher Fetcher, results *fetchedResults) {
// TODO: Fetch URLs in parallel.
// TODO: Don't fetch the same URL twice.
// This implementation doesn't do either:
defer results.wg.Done()
if depth <= 0 {
return
}
results.mux.Lock()
if _, ok := results.responses[url]; ok {
results.mux.Unlock()
// fmt.Println("Already fetched: ", x.url)
return
}
results.mux.Unlock()
body, urls, err := fetcher.Fetch(url)
results.mux.Lock()
if err != nil {
results.responses[url] = fetchResponse{url, body, err}
results.mux.Unlock()
return
} else {
results.responses[url] = fetchResponse{url, body, nil}
}
results.mux.Unlock()
for _, u := range urls {
results.wg.Add(1)
go Crawl(u, depth-1, fetcher, results)
}
return
}
func main() {
var results fetchedResults
results.responses = make(map[string]fetchResponse)
results.wg.Add(1)
Crawl("https://golang.org/", 4, fetcher, &results)
results.wg.Wait()
for url, res := range results.responses {
if res.err != nil {
fmt.Println(res.err)
} else {
fmt.Printf("found: %s %q\n", url, res.body)
}
}
}
// fakeFetcher is Fetcher that returns canned results.
type fakeFetcher map[string]*fakeResult
type fakeResult struct {
body string
urls []string
}
func (f fakeFetcher) Fetch(url string) (string, []string, error) {
if res, ok := f[url]; ok {
return res.body, res.urls, nil
}
return "", nil, fmt.Errorf("not found: %s", url)
}
// fetcher is a populated fakeFetcher.
var fetcher = fakeFetcher{
"https://golang.org/": &fakeResult{
"The Go Programming Language",
[]string{
"https://golang.org/pkg/",
"https://golang.org/cmd/",
},
},
"https://golang.org/pkg/": &fakeResult{
"Packages",
[]string{
"https://golang.org/",
"https://golang.org/cmd/",
"https://golang.org/pkg/fmt/",
"https://golang.org/pkg/os/",
},
},
"https://golang.org/pkg/fmt/": &fakeResult{
"Package fmt",
[]string{
"https://golang.org/",
"https://golang.org/pkg/",
},
},
"https://golang.org/pkg/os/": &fakeResult{
"Package os",
[]string{
"https://golang.org/",
"https://golang.org/pkg/",
},
},
}