aboutsummaryrefslogtreecommitdiff
path: root/archive.go
diff options
context:
space:
mode:
authorJordan <me@jordan.im>2021-12-03 07:19:28 -0700
committerJordan <me@jordan.im>2021-12-03 07:19:28 -0700
commitb0835fc63877c63e88e75840c5d52d3167ad5e10 (patch)
tree9db697711cab98fa9c49f87d40d6f7212baae19c /archive.go
downloadkeep-b0835fc63877c63e88e75840c5d52d3167ad5e10.tar.gz
keep-b0835fc63877c63e88e75840c5d52d3167ad5e10.zip
initial commit
Diffstat (limited to 'archive.go')
-rw-r--r--archive.go73
1 files changed, 73 insertions, 0 deletions
diff --git a/archive.go b/archive.go
new file mode 100644
index 0000000..c88cff7
--- /dev/null
+++ b/archive.go
@@ -0,0 +1,73 @@
+package main
+
+import (
+ "encoding/json"
+ "log"
+ "net/http"
+ "strconv"
+ "time"
+)
+
+var (
+ API_AVAILABILITY string = "http://archive.org/wayback/available?url="
+ API_SAVE string = "https://web.archive.org/save/"
+
+ TIMEOUT time.Duration = 25
+ client *http.Client = &http.Client{Timeout: TIMEOUT * time.Second}
+
+ blacklist = []string{"cdn.discordapp.com", "discord.com", "tenor.com",
+ "c.tenor.com", "archive.org", "web.archive.org", "youtu.be",
+ "youtube.com", "www.youtube.com"}
+)
+
+type Wayback struct {
+ Snapshots Snapshot `json:"archived_snapshots,omitempty"`
+}
+
+type Snapshot struct {
+ Recent Closest `json:"closest"`
+}
+
+type Closest struct {
+ Available bool `json:"available"`
+ Status string `json:"status"`
+}
+
+func isBlacklisted(host string) bool {
+
+ for _, h := range blacklist {
+
+ if host == h {
+ return true
+ }
+ }
+ return false
+}
+
+func isArchived(url string) (bool, int) {
+
+ req, err := http.NewRequest("GET", API_AVAILABILITY+url, nil)
+ resp, err := client.Do(req)
+ if err != nil {
+ log.Println(err)
+ return false, 0
+ }
+ av := &Wayback{}
+ decoder := json.NewDecoder(resp.Body)
+ if err := decoder.Decode(av); err != nil {
+ log.Println(err)
+ return false, 0
+ }
+ status, _ := strconv.Atoi(av.Snapshots.Recent.Status)
+ return av.Snapshots.Recent.Available, status
+}
+
+func archive(url string) int {
+
+ req, err := http.NewRequest("GET", API_SAVE+url, nil)
+ resp, err := client.Do(req)
+ if err != nil {
+ return 0
+ }
+ return resp.StatusCode
+}