1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//! The official Rust client for the [Mercury Parser].
//!
//! With just one API request, Mercury takes any web article and returns only the
//! relevant content — headline, author, body text, relevant images and more — free
//! from any clutter. It’s reliable, easy-to-use and free.
//!
//! * [Homepage]
//! * [Source Code]
//!
//! ## Usage
//!
//! The examples in this document assume you already have a Mercury Parser API key. If
//! you do not already have one, you can [sign up here][Mercury Parser].
//!
//! ### Installation
//!
//! Add this to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! futures = "0.1"
//! mercury = "0.1"
//! tokio-core = "0.1"
//! ```
//!
//! ### Example
//!
//! Additional examples can be found on [GitHub][Source Code].
//!
//! ```
//! # extern crate dotenv;
//! # extern crate futures;
//! # extern crate mercury;
//! # extern crate tokio_core;
//! #
//! # use std::{env, error};
//! #
//! # use dotenv::dotenv;
//! # use futures::Future;
//! # use mercury::Mercury;
//! # use tokio_core::reactor::Core;
//! #
//! # type Error = Box<error::Error>;
//! #
//! # fn main() {
//! #     dotenv().ok();
//! #     example().unwrap();
//! # }
//! #
//! # fn example() -> Result<(), Error> {
//! // Create a new event loop with tokio.
//! let mut core = Core::new()?;
//! let handle = core.handle();
//!
//! // Load your API key from the environment.
//! let key = env::var("MERCURY_API_KEY")?;
//!
//! // Pass a handle to the event loop and the API key to the Mercury constructor.
//! let client = Mercury::new(&handle, key)?;
//!
//! // The parse method returns a Future that will resolve to a parsed Article.
//! let future = client.parse("https://example.com").inspect(|article| {
//!     println!("{:#?}", article);
//! });
//!
//! // Block the current thread until the future completes.
//! core.run(future)?;
//! #
//! # Ok(())
//! # }
//! ```
//!
//! [Homepage]: https://mercury.postlight.com
//! [Mercury Parser]: https://mercury.postlight.com/web-parser
//! [Source Code]: https://github.com/postlight/mercury-rs

extern crate chrono;
#[macro_use]
extern crate error_chain;
extern crate futures;
#[macro_use]
extern crate hyper;
extern crate hyper_tls;
extern crate native_tls;
extern crate num_cpus;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate tokio_core;

mod article;

/// Types representing errors that can occur.
pub mod error;

use std::rc::Rc;

use futures::{stream, Future, IntoFuture, Poll, Stream};
use hyper_tls::HttpsConnector;
use hyper::{Get, Request, Uri};
use hyper::client::{Client, HttpConnector};
use tokio_core::reactor::Handle;

pub use article::*;
pub use error::Error;

const ENDPOINT: &'static str = "https://mercury.postlight.com/parser";

type Connect = HttpsConnector<HttpConnector>;

/// A client used to make requests to the [Mercury Parser].
///
/// [Mercury Parser]: https://mercury.postlight.com/web-parser
#[derive(Debug)]
pub struct Mercury(Rc<Inner>);

impl Mercury {
    /// Create a new Mercury client.
    ///
    /// # Example
    ///
    /// ```
    /// # extern crate dotenv;
    /// # extern crate futures;
    /// # extern crate mercury;
    /// # extern crate tokio_core;
    /// #
    /// # use std::{env, error};
    /// #
    /// # use dotenv::dotenv;
    /// # use futures::Future;
    /// # use mercury::Mercury;
    /// # use tokio_core::reactor::Core;
    /// #
    /// # type Error = Box<error::Error>;
    /// #
    /// # fn main() {
    /// #     dotenv().ok();
    /// #     example().unwrap();
    /// # }
    /// #
    /// # fn example() -> Result<(), Error> {
    /// let core = Core::new()?;
    /// let handle = core.handle();
    ///
    /// let key = env::var("MERCURY_API_KEY")?;
    /// let client = Mercury::new(&handle, key)?;
    /// #
    /// # Ok(())
    /// # }
    /// ```
    pub fn new(handle: &Handle, key: String) -> Result<Mercury, Error> {
        Inner::new(handle, key).map(Rc::new).map(Mercury)
    }

    /// Return a reference to a handle to the event loop this client is associated with.
    pub fn handle(&self) -> &Handle {
        self.client().handle()
    }

    /// Returns a reference to the API key associated with this client.
    pub fn key(&self) -> &str {
        &self.0.key
    }

    /// Send a request to the Mercury Parser API using this client.
    ///
    /// # Example
    ///
    /// ```
    /// # extern crate dotenv;
    /// # extern crate futures;
    /// # extern crate mercury;
    /// # extern crate tokio_core;
    /// #
    /// # use std::{env, error};
    /// #
    /// # use dotenv::dotenv;
    /// # use futures::Future;
    /// # use mercury::Mercury;
    /// # use tokio_core::reactor::Core;
    /// #
    /// # type Error = Box<error::Error>;
    /// #
    /// # fn main() {
    /// #     dotenv().ok();
    /// #     example().unwrap();
    /// # }
    /// #
    /// # fn example() -> Result<(), Error> {
    /// # let mut core = Core::new()?;
    /// # let handle = core.handle();
    /// #
    /// # let key = env::var("MERCURY_API_KEY")?;
    /// # let client = Mercury::new(&handle, key)?;
    /// #
    /// let future = client.parse("https://example.com").inspect(|article| {
    ///     println!("{:#?}", article);
    /// });
    /// #
    /// # core.run(future.then(|_| Ok(())))
    /// # }
    /// ```
    pub fn parse(&self, resource: &str) -> Response {
        let merc = Mercury::clone(self);
        let f = build_url(resource).into_future().and_then(move |url| {
            let mut req = Request::new(Get, url);

            header!{ (XApiKey, "X-Api-Key") => [String] }
            req.headers_mut().set(XApiKey(merc.key().to_owned()));

            merc.client()
                .request(req)
                .and_then(|resp| resp.body().map(stream::iter_ok).flatten().collect())
                .map_err(Error::from)
                .and_then(|body| match serde_json::from_slice(&body)? {
                    ParserResult::Ok(article) => Ok(article),
                    ParserResult::Err { msg, msgs } => bail!(msg.unwrap_or(msgs)),
                })
        });

        Response::new(Box::new(f))
    }

    /// Returns a reference to the underlying hyper client.
    fn client(&self) -> &Client<Connect> {
        &self.0.client
    }
}

impl Clone for Mercury {
    /// Increments the strong reference count of the underlying [`Rc`] pointer.
    ///
    /// [`Rc`]: https://doc.rust-lang.org/std/rc/struct.Rc.html
    fn clone(&self) -> Mercury {
        Mercury(Rc::clone(&self.0))
    }
}

/// A [`Future`] that will resolve to a parsed [`Article`].
///
/// [`Article`]: ./struct.Article.html
/// [`Future`]: ../futures/future/trait.Future.html
#[must_use = "futures do nothing unless polled"]
pub struct Response(Box<Future<Item = Article, Error = Error>>);

impl Response {
    fn new(f: Box<Future<Item = Article, Error = Error>>) -> Response {
        Response(f)
    }
}

impl Future for Response {
    type Item = Article;
    type Error = Error;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        self.0.poll()
    }
}

#[derive(Debug)]
struct Inner {
    client: Client<Connect>,
    key: String,
}

impl Inner {
    fn new(handle: &Handle, key: String) -> Result<Inner, Error> {
        let conn = Connect::new(num_cpus::get(), handle)?;
        let client = Client::configure().connector(conn).build(handle);

        Ok(Inner { client, key })
    }
}

#[cfg_attr(feature = "cargo-clippy", allow(large_enum_variant))]
#[derive(Deserialize)]
#[serde(untagged)]
enum ParserResult {
    Ok(Article),
    Err {
        #[serde(rename = "message")] msg: Option<String>,
        #[serde(default, rename = "messages")] msgs: String,
    },
}

fn build_url(resource: &str) -> Result<Uri, Error> {
    let mut raw = String::with_capacity(ENDPOINT.len() + resource.len() + 5);

    raw.push_str(ENDPOINT);
    raw.push_str("?url=");
    raw.push_str(resource);

    Ok(raw.parse()?)
}