-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebarchive.go
More file actions
124 lines (107 loc) · 3.16 KB
/
webarchive.go
File metadata and controls
124 lines (107 loc) · 3.16 KB
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
package webarchive
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"path/filepath"
"strings"
"time"
)
// Archive represents the Wayback Machine archive utility.
type Archive struct {
Query string // Query represents the domain or URL to query in the Wayback Machine.
HTTPClient *http.Client // HTTPClient is an optional custom HTTP client. If nil, a default client with a 10-second timeout is used.
}
// Result represents the result of fetching URLs from the Wayback Machine.
type Result struct {
URLs []*url.URL // URLs is a slice of parsed URLs retrieved from the Wayback Machine.
StringURLs []string // Strign URLs is a slice of non parsed URLs (as string) retrieved from the Wayback Machine.
}
// NewArchive creates a new Archive instance with the specified query and optional HTTP client.
func NewArchive(query string, client *http.Client) (*Archive, error) {
defaultClient := &http.Client{
Timeout: time.Second * 10,
}
if client == nil {
client = defaultClient
}
return &Archive{
Query: query,
HTTPClient: client,
}, nil
}
// FetchURLs fetches URLs from the Wayback Machine for the specified query.
// It queries the Wayback Machine CDX API and parses the retrieved URLs.
func (a *Archive) FetchURLs() (*Result, error) {
var result []*url.URL
waybackURL := fmt.Sprintf("https://web.archive.org/cdx/search/cdx?url=%s/*&output=txt&collapse=urlkey&fl=original&page=/", a.Query)
req, err := http.NewRequest("GET", waybackURL, nil)
if err != nil {
return nil, err
}
resp, err := a.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
URLs := strings.Split(string(body), "\n")
for _, u := range URLs {
if u != "" {
parsedURL, err := url.ParseRequestURI(u)
if err == nil {
result = append(result, parsedURL)
}
}
}
return &Result{URLs: result}, nil
}
// FormatAsJSON formats the result of URLs as JSON.
// It returns a JSON representation of the Result struct.
func (r *Result) FormatAsJSON() ([]byte, error) {
jsonData, err := json.Marshal(r)
if err != nil {
return nil, err
}
return jsonData, nil
}
// hasParams checks if a URL has parameters.
// It is used internally for local filtering.
func hasParams(u *url.URL) bool {
return u.RawQuery != ""
}
// HasParams filters URLs with parameters from the result.
// It returns a slice of URLs with parameters.
func (r *Result) HasParams() ([]*url.URL, error) {
var filteredURLs []*url.URL
for _, u := range r.URLs {
if hasParams(u) {
filteredURLs = append(filteredURLs, u)
}
}
return filteredURLs, nil
}
// FilterByExtension filters URLs by a specific file extension from the result.
// It returns a slice of URLs with the specified extension.
func (r *Result) FilterByExtension(ext string) ([]*url.URL, error) {
var filteredURLs []*url.URL
for _, u := range r.URLs {
if hasParams(u) {
if filepath.Ext(u.Path) == ext {
filteredURLs = append(filteredURLs, u)
}
}
}
return filteredURLs, nil
}
func (r *Result) FormatAsString() []string {
for _, url := range r.URLs {
r.StringURLs = append(r.StringURLs, url.String())
}
return r.StringURLs
}