Crate archiveis[][src]

archiveis - Rust API Wrapper for Archive.is

This crate provides simple access to the Archive.is Capturing Service.

Quick Start

Creating a Client

To create a client to access the Archive.is Capturing Service, you should use the ArchiveClient struct. You can pass a specific user agent or none to use a default one. To capture a specific url all you need to do is call the capture function of the client provided with the desired url.

Archive a url

The ArchiveClient is build with hyper and therefor uses futures for its services.

extern crate archiveis;
extern crate futures;

use archiveis::ArchiveClient;
use futures::future::Future;

let client = ArchiveClient::new(Some("archiveis (https://github.com/MattsSe/archiveis-rs)"));
let url = "http://example.com/";
let capture = client.capture(url).and_then(|archived| {
    println!("targeted url: {}", archived.target_url);
    println!("url of archived site: {}", archived.archived_url);
    println!("archive.is submit token: {}", archived.submit_token);
    Ok(())
});

Archive multiple urls

archive.is uses a temporary token to validate a archive request. The ArchiveClient capture function first obtains the token via a GET request. The token is usually valid several minutes, and even if archive.is switches to a new token,the older ones are still valid. So if we need to archive multiple links, we can only need to obtain the token once and then invoke the capturing service directly with capture_with_token for each url. This can be done using the future::join functionality. In the following case the designated join_all function is used to get Future of a Vec<Archived>. An undesired sideeffect if the join_all is that this returns an Error if any of the futures failed. The Capturing service should work fine in most cases but if individual error handling is desired, the capturing futures can be wrapped inside another Result. In an And_Then we can handle those failures.

extern crate archiveis;
extern crate futures;

use archiveis::ArchiveClient;
use futures::future::{join_all, Future};

let client = ArchiveClient::new(Some("archiveis (https://github.com/MattsSe/archiveis-rs)"));

// the urls to capture
let urls = vec![
    "http://example.com/",
    "https://github.com/MattsSe/archiveis-rs",
    "https://crates.io",
];

let capture = client
    .get_unique_token()
    .and_then(|token| {
        let mut futures = Vec::new();
        for u in urls.into_iter() {
            // optionally wrap the capturing result in another Result, to handle the failures in the next step
            futures.push(client.capture_with_token(u, &token).then(|x| Ok(x)));
        }
        join_all(futures)
    }).and_then(|archives| {
        let failures: Vec<_> = archives.iter().map(Result::as_ref).filter(Result::is_err).map(Result::unwrap_err).collect();
        if failures.is_empty() {
            println!("all links successfully archived.");
        } else {
            for err in failures {
                if let archiveis::Error::MissingUrl(url) = err {
                    println!("Failed to archive url: {}", url);
                }
            }
        }
        Ok(())
    });

Structs

ArchiveClient

A Client that serves as a wrapper around the archive.is capture service

Archived

Represents a result of the capture service

Enums

Error

The Error Type used in this crate