Created
January 23, 2013 03:21
-
-
Save dangra/4601612 to your computer and use it in GitHub Desktop.
My Solution to Go Tour #70 - Web Crawler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GistID: 4601612 | |
package main | |
import ( | |
"fmt" | |
) | |
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 Response struct { | |
*Request | |
body string | |
urls []string | |
err error | |
} | |
type Request struct { | |
url string | |
depth int | |
} | |
func fetcherPool(fetcher Fetcher, size int) (chan *Request, chan *Response) { | |
requests := make(chan *Request) | |
responses := make(chan *Response) | |
for i := 0; i < size; i++ { | |
go func() { | |
for { | |
req := <-requests | |
body, urls, err := fetcher.Fetch(req.url) | |
responses <- &Response{req, body, urls, err} | |
} | |
}() | |
} | |
return requests, responses | |
} | |
// Crawl uses fetcher to recursively crawl | |
// pages starting with url, to a maximum of depth. | |
func Crawl(url string, depth int, fetcher Fetcher) { | |
requests, responses := fetcherPool(fetcher, 10) | |
seen := map[string]bool{url: true} | |
schedule := func(d int, urls ...string) { | |
for _, u := range urls { | |
requests <- &Request{u, d} | |
} | |
} | |
unique := func(urls []string) []string { | |
output := make([]string, 0, len(urls)) | |
for _, u := range urls { | |
if !seen[u] { | |
seen[u] = true | |
output = append(output, u) | |
} | |
} | |
return output | |
} | |
schedule(depth, url) | |
for active := 1; active > 0; active-- { | |
res := <-responses | |
if res.err != nil { | |
fmt.Println(res.err) | |
continue | |
} | |
fmt.Printf("found: %s %q\n", res.url, res.body) | |
if d := res.depth - 1; d > 0 { | |
notseen := unique(res.urls) | |
if len(notseen) > 0 { | |
active += len(notseen) | |
go schedule(d, notseen...) | |
} | |
} | |
} | |
} | |
func main() { | |
Crawl("http://golang.org/", 4, fetcher) | |
} | |
// 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{ | |
"http://golang.org/": &fakeResult{ | |
"The Go Programming Language", | |
[]string{ | |
"http://golang.org/pkg/", | |
"http://golang.org/cmd/", | |
}, | |
}, | |
"http://golang.org/pkg/": &fakeResult{ | |
"Packages", | |
[]string{ | |
"http://golang.org/", | |
"http://golang.org/cmd/", | |
"http://golang.org/pkg/fmt/", | |
"http://golang.org/pkg/os/", | |
}, | |
}, | |
"http://golang.org/pkg/fmt/": &fakeResult{ | |
"Package fmt", | |
[]string{ | |
"http://golang.org/", | |
"http://golang.org/pkg/", | |
}, | |
}, | |
"http://golang.org/pkg/os/": &fakeResult{ | |
"Package os", | |
[]string{ | |
"http://golang.org/", | |
"http://golang.org/pkg/", | |
}, | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment