12

I can go to the Internet Wayback machine website and add a link to the site for the Wayback spider to scrape; is it possible to do this using their API as well? From what I've read you can only pull a list of the scraped sites.

1

2 Answers 2

7

Yes,

Please see the Save Page Now (SPN) API documentation here:

https://docs.google.com/document/d/19RJsRncGUw2qHqGGg9lqYZYf7KKXMDL1Mro5o1Qw6QI/

For questions please write to [email protected]

2

I could not really find the API specifications in link in @MarkGraham's answer but here is some sample code taken from my Stack Exchange Archivist userscript. It basically mimics what you do as a user when filling in the form here:

  1. a POST call to https://web.archive.org/save/ + the URL you want to archive; parameters are the url and capture_all=on

Only this step is required, but you can do more:

  1. the server returns some HTML / JavaScript with a call to spn.watchJob with the job ID, which you can use to monitor the progress
  2. a GET call to https://web.archive.org/save/status/ + the job ID + some cache breaker will return a JSON with the status. If the status is 'success', it will also contain the URL to the snapshot.
function archive(link) {
  console.log("Archiving: " + link);
  // Call Wayback Machine
  let archiveLink = "https://web.archive.org/save/" + link;
  GM.xmlHttpRequest({
    method: 'POST',
    url: archiveLink,
    data: $.param({
      "url": link,
      "capture_all": "on"
    }),
    headers: {
      "Content-Type": "application/x-www-form-urlencoded"
    },
    onload: function(response) {
      console.log(response.response);
      let match = /spn.watchJob\("([^"]+)",/g.exec(response.response);
      if (match == null) {
        console.log("Could not determine job ID for " + archiveLink);
        next(false);
        return;
      }
      // Start monitoring archiving job
      setTimeout(function () {
        monitorArchivingJob(match[1]);
      }, 2500);
    },
    onerror: function(response) {
      console.log(["An error occurred while calling: " + archiveLink,
                   response.status, response.statusText, response.readyState,
                   response.responseHeaders, response.responseText, response.finalUrl].join("\n"));
      next(false);
    }
  });
}

function monitorArchivingJob(jobID) {
  // Fetch job status
  let url = "https://web.archive.org/save/status/" + jobID + "?_t=" + Math.floor(Date.now() / 1000);
  console.log(url);
  GM.xmlHttpRequest({
    method: 'GET',
    url: url,
    onload: function(response) {
      let data = JSON.parse(response.response);

      // Check status
      if (data.status == "pending") {
        setTimeout(function () {
          monitorArchivingJob(jobID);
        }, 2500);
      } else if (data.status == "success") {
        // Success!
        let playbackURL = "https://web.archive.org/web/" + data.timestamp + "/" + data.original_url;
        console.log("Saved: " + playbackURL);
        next(true);
      } else {
        console.log("Failed to archive: " + url);
        next(false);
      }
    },
    onerror: function(response) {
      console.log(["An error occurred while calling: " + url,
                   response.status, response.statusText, response.readyState,
                   response.responseHeaders, response.responseText, response.finalUrl].join("\n"));
      next(false);
    }
  });
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.