Created
December 30, 2017 16:52
-
-
Save evilpie/c3d020e42583e8de68ccf736dd418900 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 reqwest; | |
#[macro_use] | |
extern crate error_chain; | |
#[macro_use] | |
extern crate serde_derive; | |
extern crate serde; | |
extern crate serde_json; | |
extern crate url; | |
use url::Url; | |
error_chain! { | |
foreign_links { | |
ReqError(reqwest::Error); | |
IoError(std::io::Error); | |
UrlError(url::ParseError); | |
} | |
} | |
#[derive(Deserialize, Debug)] | |
struct Reddit { | |
kind: String, | |
data: RedditData | |
} | |
#[derive(Deserialize, Debug)] | |
struct RedditData { | |
modhash: String, | |
whitelist_status: String, | |
children: Vec<RedditPostContainer>, | |
after: Option<String>, | |
before: Option<String> | |
} | |
#[derive(Deserialize, Debug)] | |
struct RedditPostContainer { | |
kind: String, | |
data: RedditPost, | |
} | |
#[derive(Deserialize, Debug)] | |
struct RedditPost { | |
domain: String, | |
title: String, | |
subreddit: String, | |
id: String, | |
url: String, | |
is_self: bool, | |
over_18: bool, | |
score: i32, | |
} | |
fn naughty_posts(reddit: &Reddit) -> Vec<&RedditPost> { | |
reddit.data.children.iter().map(|ctr| &ctr.data).filter(|p| !p.is_self && p.over_18).collect() | |
} | |
fn get_list(after: String) -> Result<String> { | |
let url = Url::parse_with_params("https://www.reddit.com/r/all.json", | |
&[("after", after)])?; | |
let mut resp = reqwest::get(url)?; | |
assert!(resp.status().is_success()); | |
let reddit: Reddit = resp.json()?; | |
for post in naughty_posts(&reddit) { | |
println!("[{}] {}", post.score, post.title); | |
println!(" url: {}", post.url); | |
println!(" r/{}", post.subreddit); | |
} | |
Ok(reddit.data.after.unwrap()) | |
} | |
fn main() { | |
let mut after: String = "".into(); | |
for _ in 1..15 { | |
println!("After: {}", after); | |
after = get_list(after).unwrap(); | |
println!(""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment