-
Notifications
You must be signed in to change notification settings - Fork 0
/
capture.go
37 lines (26 loc) · 816 Bytes
/
capture.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
package archiveorg
import (
"errors"
"fmt"
"time"
log "github.com/sirupsen/logrus"
)
var NoContentLocationErr = errors.New("missing 'content-lcation' header") // Returned when a malformed response is returned by archive.org.
// Capture requests a
func Capture(url string, timeout ...time.Duration) (string, error) {
if len(timeout) == 0 {
timeout = []time.Duration{DefaultRequestTimeout}
}
pleaseCrawl := fmt.Sprintf("%v/save/%v", BaseURL, url)
log.WithField("crawl-request", pleaseCrawl).Debugf("Requesting archive.org crawl")
resp, _, err := doRequest("", pleaseCrawl, nil, timeout[0])
if err != nil {
return "", err
}
loc := resp.Header.Get("Content-Location")
if loc == "" {
return "", NoContentLocationErr
}
location := fmt.Sprintf("%v/%v", BaseURL, loc)
return location, nil
}