-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
173 lines (144 loc) · 4.19 KB
/
main.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
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"bufio"
"compress/gzip"
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"log"
"time"
"io"
"net/http"
"os"
)
const (
version = "0.0.11"
wbSnapshotApiURL = "https://web.archive.org/cdx/search/xd?output=json&url=%s&fl=timestamp,original&collapse=digest&gzip=false&filter=statuscode:200"
wbFileURL = "https://web.archive.org/web/%sid_/%s"
parseTimeLayout = "20060102150405"
viewTimeLayout = "2006-01-02 15:04:05"
)
var (
flagUrl = flag.String("u", "", "specify url")
flagTimeout = flag.Duration("t", 5*time.Second, "specify timeout")
flagSnapshots = flag.Bool("snapshots", false, "get all snapshots")
flagDate = flag.String("date", "", "get snapshot for a specific date")
flagGetAllSnapshots = flag.Bool("all", false, "get all snapshots")
flagHelp = flag.Bool("help", false, "show help")
flagNoBanner = flag.Bool("no-banner", false, "hide banner")
)
func main() {
flag.Parse()
if !*flagNoBanner {
fmt.Printf("🪄 wb / v%s\n----\n", version)
}
if *flagHelp {
fmt.Println("Usage: \n wb [flags]\n")
flag.PrintDefaults()
os.Exit(0)
}
var urls []string
if *flagUrl != "" {
urls = []string{*flagUrl}
} else {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
urls = append(urls, scanner.Text())
}
}
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
client := http.Client{
Timeout: *flagTimeout,
Transport: &http.Transport{
TLSHandshakeTimeout: *flagTimeout,
},
}
for _, url := range urls {
if len(urls) > 1 {
fmt.Println("// Snapshots for", url)
}
snapshots, err := getSnapshots(client, url)
if err != nil {
log.Printf("failed to snapshots: %s\n", err)
continue
}
if *flagSnapshots {
fmt.Println("Snapshots for", url)
for _, s := range snapshots {
parsedTime, err := time.Parse(parseTimeLayout, s[0])
if err != nil {
log.Fatalf("failed to parse time: %s\n", err)
}
fmt.Printf("* %s | %s | %s\n", s[0], parsedTime.Format(viewTimeLayout), s[1])
}
continue
}
selectedSnapshot := snapshots[len(snapshots)-1]
if *flagDate != "" {
for _, s := range snapshots {
if s[0] == *flagDate {
selectedSnapshot = s
break
}
}
}
if *flagGetAllSnapshots {
for _, s := range snapshots {
snapshotContent, err := getSnapshotContent(client, s[0], s[1])
if err != nil {
log.Printf("failed to read input: %s\n", err)
}
io.Copy(os.Stdout, snapshotContent)
}
continue
}
snapshotContent, err := getSnapshotContent(client, selectedSnapshot[0], selectedSnapshot[1])
if err != nil {
log.Printf("failed to read input: %s\n", err)
continue
}
io.Copy(os.Stdout, snapshotContent)
}
}
func getSnapshots(c http.Client, url string) ([][]string, error) {
req, err := http.NewRequest("GET", fmt.Sprintf(wbSnapshotApiURL, url), nil)
if err != nil {
return [][]string{}, fmt.Errorf("getSnapshots: failed to generate request waybackmachine api: %w url: %s", err, url)
}
rsp, err := c.Do(req)
if err != nil {
return [][]string{}, fmt.Errorf("getSnapshots: failed to send request waybackmachine api: %w url: %s", err, url)
}
defer rsp.Body.Close()
var r [][]string
dec := json.NewDecoder(rsp.Body)
err = dec.Decode(&r)
if err != nil {
return [][]string{}, fmt.Errorf("getSnapshots: error while decoding response %w url: %s", err, url)
}
if len(r) < 1 {
return [][]string{}, fmt.Errorf("getSnapshots: no results found for this url: %s", url)
}
return r[1:], nil
}
func getSnapshotContent(c http.Client, ts, url string) (io.ReadCloser, error) {
req, err := http.NewRequest("GET", fmt.Sprintf(wbFileURL, ts, url), nil)
if err != nil {
return nil, fmt.Errorf("getSnapshotContent: failed to generate request waybackmachine api: %w url: %s", err, url)
}
req.Header.Add("Accept-Encoding", "plain")
rsp, err := c.Do(req)
if err != nil {
return nil, fmt.Errorf("getSnapshotContent: failed to send request waybackmachine api: %w url: %s", err, url)
}
var reader io.ReadCloser
switch rsp.Header.Get("Content-Encoding") {
case "gzip":
reader, err = gzip.NewReader(rsp.Body)
defer reader.Close()
default:
reader = rsp.Body
}
return reader, nil
}