-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobots.go
52 lines (47 loc) · 1.16 KB
/
robots.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
package collyresponsible
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/temoto/robotstxt"
)
func GetRobots(ctx context.Context, website, userAgent string, limiter *RequestLimiter) (*robotstxt.RobotsData, error) {
if strings.HasSuffix(website, "/") {
website = website[:len(website)-1]
}
head, err := http.NewRequestWithContext(ctx, http.MethodHead, fmt.Sprintf("%s/%s", website, RobotsTxt), nil)
if err != nil {
return nil, err
}
head.Header.Add(UserAgentHeader, userAgent)
//
resp, err := http.DefaultClient.Do(head)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return robotstxt.FromString("# No robots.txt found")
}
//
limiter.Sleep()
//
get, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("%s/%s", website, RobotsTxt), nil)
if err != nil {
return nil, err
}
get.Header.Add(UserAgentHeader, userAgent)
//
getResp, err := http.DefaultClient.Do(get)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return robotstxt.FromString("# No robots.txt found")
}
robots, err := robotstxt.FromResponse(getResp)
if err != nil {
return nil, err
}
return robots, nil
}