Created
December 27, 2017 21:36
-
-
Save criloz/2aae69f5e84c737155ea3110219fa928 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate futures; | |
extern crate hyper; | |
extern crate tokio_core; | |
#[macro_use] | |
extern crate serde_derive; | |
extern crate serde; | |
extern crate serde_json; | |
extern crate hyper_tls; | |
extern crate time; | |
use time::PreciseTime; | |
use futures::{Future, Stream}; | |
use hyper::Client; | |
use tokio_core::reactor::Core; | |
use hyper_tls::HttpsConnector; | |
use std::io::{self, Write}; | |
#[derive(Serialize, Deserialize, Debug)] | |
struct Story { | |
by: String, | |
id: i32, | |
score: i32, | |
time: i32, | |
title: String, | |
} | |
fn main() { | |
let start = PreciseTime::now(); | |
let mut core = Core::new().unwrap(); | |
let client = ::hyper::Client::configure() | |
.connector(::hyper_tls::HttpsConnector::new(4, &core.handle()).unwrap()) | |
.build(&core.handle()); | |
let uri = "https://hacker-news.firebaseio.com/v0/topstories.json".parse().unwrap(); | |
let work = client.get(uri).and_then(|res| { | |
res.body().concat2().and_then(move |body| { | |
let v: Vec<i32> = serde_json::from_slice(&body).unwrap(); | |
let mut vs = Vec::new(); | |
for i in 0..499 { | |
let url = format!( | |
"https://hacker-news.firebaseio.com/v0/item/{}.json", | |
v[i], | |
).parse().unwrap(); | |
vs.push(client.clone().get(url)); | |
} | |
let xx = ::futures::future::join_all(vs); | |
xx.and_then(|res2|{ | |
let mut vs2 = Vec::new(); | |
for r in res2{ | |
vs2.push(r.body().concat2()); | |
} | |
let xx = ::futures::future::join_all(vs2); | |
xx.and_then(|res3|{ | |
let mut count=0; | |
for r in res3{ | |
count+=1; | |
let value:Story = serde_json::from_slice(&r).unwrap(); | |
println!("{:?}, {:?}", count, value.title); | |
} | |
Ok(()) | |
}) | |
}) | |
}) | |
}); | |
core.run(work).unwrap(); | |
let end = PreciseTime::now(); | |
println!("{} seconds for whatever you did.", start.to(end)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment