-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
198 lines (166 loc) · 3.73 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
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
package main
import (
"fmt"
"log"
"math"
"net/http"
"net/url"
"os"
"os/signal"
"strconv"
"sync"
"time"
"github.com/PuerkitoBio/goquery"
)
type Opts struct {
maxCrawls uint64
rootUrl string
}
type Crawler struct {
crawledPages map[string]bool
maxCrawls uint64
maxRoutines int
workChan chan []string
unseenLinksChan chan string
rootUrl string
wg sync.WaitGroup
}
func NewCrawler(opts *Opts) *Crawler {
c := &Crawler{
crawledPages: make(map[string]bool),
maxCrawls: uint64(math.Inf(1)),
workChan: make(chan []string),
unseenLinksChan: make(chan string),
rootUrl: opts.rootUrl,
maxRoutines: 20,
wg: sync.WaitGroup{},
}
if opts.maxCrawls != 0 {
c.maxCrawls = opts.maxCrawls
}
return c
}
// isValidUrl tests a string to determine if it is a well-structured url or not.
// https://golangcode.com/how-to-check-if-a-string-is-a-url/
func isValidUrl(toTest string) bool {
if toTest == "" {
return false
}
_, err := url.ParseRequestURI(toTest)
if err != nil {
return false
}
u, err := url.Parse(toTest)
if err != nil || u.Scheme == "" || u.Host == "" {
return false
}
return true
}
// Parse out all anchors
func parseResponse(resp *http.Response) []string {
defer resp.Body.Close()
urls := make([]string, 0)
document, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
log.Println("Error loading HTTP response body. ", err)
return urls
}
document.Find("a").Each(func(i int, element *goquery.Selection) {
href, exists := element.Attr("href")
if exists && isValidUrl(href) {
urls = append(urls, href)
}
})
return urls
}
// Fetch all the anchor links on a page and push them to the channel
func scrapeUrl(url string) []string {
resp, err := http.Get(url)
if err != nil {
return []string{}
}
urls := parseResponse(resp)
fmt.Println(url)
for _, link := range urls {
fmt.Println("\t", link)
}
return urls
}
func (c *Crawler) checkUrls() {
// Loop through and make sure you have not seen the URL
n := 0
for urls := range c.workChan {
// Puhs unseen into the channel to process unseen links
for _, url := range urls {
// Process only uncrawled pages
if !c.crawledPages[url] {
n += 1
c.wg.Add(1)
c.crawledPages[url] = true
c.unseenLinksChan <- url
}
// Exit condition
if n >= int(c.maxCrawls) {
c.wg.Wait()
return // done
}
}
}
}
func (c *Crawler) createWorkers() {
// Set up our routines to scape the urls
for i := 0; i < c.maxRoutines; i++ {
go func() {
for url := range c.unseenLinksChan {
foundLinks := scrapeUrl(url)
// Push to url checking channel
go func() { c.workChan <- foundLinks }()
c.wg.Done()
}
}()
}
}
func (c *Crawler) Crawl() map[string]bool {
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
go func() {
for range sig {
// sig is a ^C, handle it
fmt.Println("Crawled", len(c.crawledPages), "pages")
os.Exit(1)
}
}()
// Submit first job
go func() {
c.workChan <- []string{c.rootUrl}
}()
c.createWorkers()
c.checkUrls()
return c.crawledPages
}
func parseArgs(args []string) (*Opts, error) {
opts := &Opts{}
if len(args) < 2 {
return nil, fmt.Errorf("Not enough arguements supplied")
}
opts.rootUrl = args[1]
if !isValidUrl(opts.rootUrl) {
return nil, fmt.Errorf("Invalid URL format")
}
if len(args) == 3 {
opts.maxCrawls, _ = strconv.ParseUint(args[2], 0, 64)
}
return opts, nil
}
func main() {
opts, err := parseArgs(os.Args)
if err != nil {
fmt.Println(err.Error())
os.Exit(-1)
}
crawler := NewCrawler(opts)
http.DefaultClient.Timeout = time.Second * 10
crawledPages := crawler.Crawl()
fmt.Println("Crawled", len(crawledPages), "pages")
os.Exit(0)
}