-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_test.go
51 lines (44 loc) · 1.13 KB
/
example_test.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
package robots_test
import (
"net/http"
"github.com/benjaminestes/robots"
)
func ExampleRobots() {
robotsURL, err := robots.Locate("https://www.example.com/page.html")
if err != nil {
// Handle error - couldn't parse input URL.
}
resp, err := http.Get(robotsURL)
if err != nil {
// Handle error.
}
defer resp.Body.Close()
r, err := robots.From(200, resp.Body)
if err != nil {
// Handle error - couldn't read from input.
}
if r.Test("Crawlerbot", "/") {
// You're good to crawl "/".
}
if r.Tester("Crawlerbot")("/page.html") {
// You're good to crawl "/page.html".
}
for _, sitemap := range r.Sitemaps() {
// As the caller, we are responsible for ensuring that
// the sitemap URL is in scope of the robots.txt file
// we used before we try to access it.
sitemapRobotsURL, err := robots.Locate(sitemap)
if err != nil {
// Couldn't parse sitemap URL - probably we should skip.
continue
}
if sitemapRobotsURL == robotsURL && r.Test("Crawlerbot", sitemap) {
resp, err := http.Get(sitemap)
if err != nil {
// Handle error.
}
defer resp.Body.Close()
// ...do something with sitemap.
}
}
}