-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (46 loc) · 1.03 KB
/
main.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
package main
import (
"fmt"
"log"
"net/url"
"os"
"time"
)
func main() {
if len(os.Args) != 2 {
log.Fatalf("Missing Argument: website (e.g. https://bbc.co.uk/)")
}
domain, err := url.Parse(os.Args[1])
if err != nil {
log.Fatal(err)
}
start := time.Now()
crawler := NewCrawler(domain)
results := crawler.Crawl()
pagesCrawled, errorCount := processResults(results)
elasped := time.Since(start)
rate := float64(pagesCrawled) / (float64(elasped) / float64(time.Second))
fmt.Printf("Crawled %d pages in %s (%.2f req/sec) with %d errors \n", pagesCrawled, elasped, rate, errorCount)
}
func processResults(results chan *Page) (pagesCrawled, errorCount int) {
for page := range results {
printPage(page)
if page.err != nil {
errorCount++
}
pagesCrawled++
}
return
}
func printPage(page *Page) {
fmt.Printf("URL:\t%v\n", page.location)
if page.err != nil {
fmt.Printf("Error: %v\n", page.err)
return
}
fmt.Println("Links:")
for _, link := range page.links {
fmt.Printf("\t%v\n", link)
}
fmt.Println()
}