Created
May 26, 2017 19:24
-
-
Save edupo/838a502fa21f12d9edacc491ca9eba4a to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"fmt" | |
"sync" | |
) | |
type Fetcher interface { | |
Fetch(url string) (body string, urls []string, err error) | |
} | |
// A WebMap is a safe map of url and body. | |
type WebMap struct { | |
m map[string]string | |
mux sync.Mutex | |
} | |
func (m *WebMap) Inc(url string) (ret bool) { | |
m.mux.Lock() | |
defer m.mux.Unlock() | |
_, ret = m.m[url] | |
return | |
} | |
func (m *WebMap) Add(url, body string) { | |
if !m.Inc(url) { | |
m.mux.Lock() | |
defer m.mux.Unlock() | |
m.m[url] = body | |
} | |
} | |
// crawler is the class which actually recurses the the web. | |
type crawler struct { | |
wm WebMap | |
wg sync.WaitGroup | |
f Fetcher | |
} | |
func (c *crawler) crawl(url string, d int) { | |
// Defering done to the wait group | |
defer c.wg.Done() | |
// Fetching all urls | |
body, urls, err := c.f.Fetch(url) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
c.wm.Add(url, body) // Adds the url to the WebMap | |
fmt.Printf("found: %s %q\n", url, body) // Report | |
// Put here your processing code for the url. | |
// Spawn new goroutines to continue the job. | |
for _, u := range urls { | |
if ! c.wm.Inc(u) && d > 0 { | |
c.wg.Add(1) | |
go c.crawl(u, d-1) | |
} | |
} | |
return | |
} | |
// Crawl uses fetcher to recursively crawl | |
// pages starting with url, to a maximum of depth. | |
func Crawl(url string, depth int, fetcher Fetcher) { | |
wm := WebMap{ | |
m: make(map[string]string), | |
} | |
c := crawler{ | |
wm: wm, | |
f: fetcher, | |
} | |
c.wg.Add(1) | |
go c.crawl(url, depth) | |
c.wg.Wait() | |
return | |
} | |
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