-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrap_robotstxt.go
161 lines (136 loc) · 3.06 KB
/
scrap_robotstxt.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
package main
import (
"bufio"
"io"
"net/url"
"slices"
"strings"
"time"
)
const (
userAgent = "User-agent"
ruleAllow = "Allow"
ruleDisallow = "Disallow"
ruleCrawlDelay = "Crawl-delay"
)
type robotstxt map[string][]string
func (r robotstxt) crawlDelay() (time.Duration, bool) {
s, found := r[ruleCrawlDelay]
if !found {
return 0, false
}
n, err := time.ParseDuration(s[0])
if err != nil {
return 0, false
}
return n, true
}
func (r robotstxt) pathAllowed(path string) bool {
disallowed, found := r[ruleDisallow]
if !found {
return true
}
i := IndexPathPrefix(disallowed, path)
if i == -1 {
return true
}
allowed, found := r[ruleAllow]
if !found {
return false
}
j := IndexPathPrefix(allowed, path)
if j == -1 {
return false
}
// Only non-conflicting paths are parsed out of
// robots.txt, hence the larger prefix must have
// appeared first.
return len(disallowed[i]) < len(allowed[j])
}
func tokenCase(s string) string {
if len(s) == 0 {
return s
}
return strings.ToUpper(s[:1]) + strings.ToLower(s[1:])
}
func splitTokenAndValue(s string) (string, string) {
var token, value string
s, _, _ = strings.Cut(s, "#")
parts := strings.SplitN(s, ":", 2)
if len(parts) >= 1 {
token = tokenCase(strings.TrimSpace(parts[0]))
}
if len(parts) == 2 {
value = strings.TrimSpace(parts[1])
}
return token, value
}
func IndexPathPrefix(paths []string, value string) int {
return slices.IndexFunc(paths, func(p string) bool {
return strings.HasPrefix(value, p)
})
}
func parseCrawlDelay(value string) (time.Duration, error) {
delayTime, err := time.ParseDuration(value)
if err == nil {
return delayTime, nil
}
// Assume seconds if value has no unit
return time.ParseDuration(value + "s")
}
func scrapRobotsTxt(input io.Reader) robotstxt {
rules := map[string][]string{}
skipToNextValue := false
matchingAgent := true
scanner := bufio.NewScanner(input)
for scanner.Scan() {
line := scanner.Text()
if line == "" || strings.HasPrefix(line, "#") {
continue
}
token, value := splitTokenAndValue(line)
if token == "" || value == "" {
continue
}
if token == userAgent {
if !skipToNextValue {
matchingAgent = value == "*"
if matchingAgent {
skipToNextValue = true
}
}
continue
}
if !matchingAgent {
continue
}
skipToNextValue = false
if token == ruleCrawlDelay {
// First Crawl-delay is accepted, similar to Allow and Disallow
if len(rules[ruleCrawlDelay]) > 0 {
continue
}
delayTime, err := parseCrawlDelay(value)
if err != nil {
continue
}
rules[ruleCrawlDelay] = []string{delayTime.String()}
} else if token == ruleAllow || token == ruleDisallow {
value, err := url.PathUnescape(value)
if err != nil {
continue
}
// robots.txt uses the first matching rule. Don't add paths that
// will never be used
if IndexPathPrefix(rules[ruleAllow], value) != -1 {
continue
}
if IndexPathPrefix(rules[ruleDisallow], value) != -1 {
continue
}
rules[token] = append(rules[token], value)
}
}
io.ReadAll(input)
return rules
}