wayback/wayback.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
package wayback import ( "context" "encoding/json" "fmt" "io" "net/http" "regexp" ) type Client struct { httpClient *http.Client } const userAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36" var ( host = "archive.org" dest = "https://web." + host base = "https://web.archive.org/save/" endpoint = "https://archive.org/wayback/available" ) // Wayback is the handle of saving webpages to archive.org func (wbrc *Client) Archive(ctx context.Context, u string) (result string, err error) { if wbrc.httpClient == nil { wbrc.httpClient = &http.Client{ CheckRedirect: noRedirect, } } result, err = wbrc.archive(ctx, u) if err != nil { return } return } func (wbrc *Client) archive(ctx context.Context, uri string) (string, error) { req, err := http.NewRequestWithContext(ctx, http.MethodGet, base+uri, nil) if err != nil { return "", err } req.Header.Add("User-Agent", userAgent) resp, err := wbrc.httpClient.Do(req) if err != nil { return "", err } defer resp.Body.Close() var loc string loc = resp.Header.Get("Content-Location") if len(loc) > 0 { return loc, nil } loc = resp.Header.Get("Location") if len(loc) > 0 { return loc, nil } links := resp.Header.Get("Link") re := regexp.MustCompile(`(?m)http[s]?:\/\/web\.archive\.org/web/[-a-zA-Z0-9@:%_\+.~#?&//=]*`) if match := re.FindAllString(links, -1); len(match) > 0 { loc = match[len(match)-1] return fmt.Sprintf("%v", loc), nil } loc = resp.Request.URL.String() if match := re.FindAllString(loc, -1); len(match) > 0 { return fmt.Sprintf("%v", loc), nil } loc, err = wbrc.latest(ctx, uri) if err != nil { loc = base + uri } // HTTP 509 Bandwidth Limit Exceeded if resp.StatusCode == 509 { return fmt.Sprint(loc), nil } if resp.StatusCode != 200 { return fmt.Sprint(loc), nil } return loc, nil } func (wbrc *Client) latest(_ context.Context, u string) (string, error) { // https://web.archive.org/*/https://example.org result := fmt.Sprintf("%s/*/%s", dest, u) uri := endpoint + "?url=" + u resp, err := wbrc.httpClient.Get(uri) if err != nil { return "", err } defer resp.Body.Close() data, err := io.ReadAll(resp.Body) if err != nil { return "", err } var dat map[string]interface{} if err := json.Unmarshal(data, &dat); err != nil { return "", err } if archived, ok := dat["archived_snapshots"].(map[string]interface{}); ok { if closest, ok := archived["closest"].(map[string]interface{}); ok { if closest["available"].(bool) && closest["status"] == "200" { return closest["url"].(string), nil } } } return result, fmt.Errorf("Not found") } func noRedirect(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }