-
Notifications
You must be signed in to change notification settings - Fork 0
/
grabber.go
50 lines (46 loc) · 917 Bytes
/
grabber.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
package main
import (
"fmt"
"net/url"
"regexp"
"strings"
"golang.org/x/net/html"
)
func parseHtml(text string) []string {
tkn := html.NewTokenizer(strings.NewReader(text))
retv := []string{}
var isLi bool
for {
tt := tkn.Next()
switch {
case tt == html.ErrorToken:
return retv
case tt == html.StartTagToken:
t := tkn.Token()
isLi = t.Data == "a"
case tt == html.TextToken:
t := tkn.Token()
if isLi {
retv = append(retv, t.String())
}
isLi = false
}
}
}
func parseWorker(toParse chan string, toMatch chan string) {
backMatch := regexp.MustCompile(`^.*\.\./.*$`)
for {
select {
case val := <-toParse:
_, err := url.Parse(val)
if err == nil {
debug(fmt.Sprintf("Parsing: %s", val))
for _, target := range parseHtml(httpGet(val)) {
if !backMatch.MatchString(target) {
toMatch <- fmt.Sprintf("%s%s", val, target)
}
}
}
}
}
}