/
result.go
57 lines (48 loc) · 1.12 KB
/
result.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
package client
import (
"fmt"
"regexp"
"strings"
)
type QueryResult struct {
Count int
Pages []string
}
type Page struct {
Title string
Lines []string
Links []string
}
func (p *Page) ExtractExternalLinks() []string {
includes := []string{"http://", "https://"}
excludes := []string{".png", ".gif", ".jpg", ".jpeg", ".svg"}
whitespace := " "
match := func(line string, keywords []string) string {
for _, keyword := range keywords {
if strings.Contains(line, keyword) {
return keyword
}
}
return ""
}
linkURLs := []string{}
for _, line := range p.Lines {
if matched := match(line, includes); matched != "" {
if match(line, excludes) != "" {
continue
}
foundBracket, _ := regexp.MatchString(fmt.Sprintf("\\[.*%s.*\\]", matched), line)
if strings.Index(line, matched) != -1 {
line = line[strings.Index(line, matched):]
}
if strings.Index(line, whitespace) != -1 {
line = line[:strings.Index(line, whitespace)]
}
if foundBracket && strings.Index(line, "]") == len(line)-1 {
line = line[:len(line)-1]
}
linkURLs = append(linkURLs, line)
}
}
return linkURLs
}