From ca276b66837ac04bf92de257e5e65d2992f1a547 Mon Sep 17 00:00:00 2001 From: Jordan Date: Thu, 24 Nov 2022 00:55:37 -0700 Subject: archive, keep: send authenticated POST requests to /save/ IA endpoint --- archive.go | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) (limited to 'archive.go') 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 } -- cgit v1.2.3-54-g00ecf