aboutsummaryrefslogtreecommitdiff
path: root/archive.go
diff options
context:
space:
mode:
Diffstat (limited to 'archive.go')
-rw-r--r--archive.go36
1 files changed, 30 insertions, 6 deletions
diff --git a/archive.go b/archive.go
index 124c42e..9397797 100644
--- a/archive.go
+++ b/archive.go
@@ -2,22 +2,30 @@ package main
import (
"encoding/json"
+ "fmt"
"log"
"net"
"net/http"
+ "net/url"
"regexp"
"strconv"
+ "strings"
"time"
)
var (
API_AVAILABILITY string = "https://archive.org/wayback/available?url="
- API_SAVE string = "https://web.archive.org/save/"
+ API_SAVE string = "https://web.archive.org/save"
TIMEOUT time.Duration = 10
client *http.Client = &http.Client{Timeout: TIMEOUT * time.Second}
)
+type Save struct {
+ URL string `json:"url"`
+ Job string `json:"job_id"`
+}
+
type Wayback struct {
Snapshots Snapshot `json:"archived_snapshots,omitempty"`
}
@@ -60,15 +68,31 @@ func isArchived(url string) (bool, int) {
return av.Snapshots.Recent.Available, status
}
-func archive(url string) int {
+func archive(accessKey string, secretKey string, URL string) (int, string) {
+
+ params := url.Values{}
+ params.Set("url", URL)
+ req, _ := http.NewRequest(http.MethodPost, API_SAVE,
+ strings.NewReader(params.Encode()),
+ )
+ req.Header.Set("Accept", "application/json")
+ auth := fmt.Sprintf("LOW %s:%s", accessKey, secretKey)
+ req.Header.Set("Authorization", auth)
+ req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
- req, err := http.NewRequest("GET", API_SAVE+url, nil)
resp, err := client.Do(req)
if err != nil {
if e, _ := err.(net.Error); !e.Timeout() {
- log.Println("archive:", err)
+ log.Println("SAVE", err)
}
- return 0
+ return 0, ""
+ }
+ defer resp.Body.Close()
+ save := &Save{}
+ decoder := json.NewDecoder(resp.Body)
+ if err := decoder.Decode(save); err != nil {
+ log.Println("SAVE", err)
+ return 0, ""
}
- return resp.StatusCode
+ return resp.StatusCode, save.Job
}