-
-
Save danneu/401d8f18941e1297f15f26be480710d3 to your computer and use it in GitHub Desktop.
This file contains 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
#[derive(Deserialize, Debug)] | |
pub struct Submission { | |
#[serde(rename = "id")] | |
pub thing_id: String, | |
pub author: String, | |
pub title: String, | |
pub created_utc: f32, | |
pub score: i32, | |
pub url: String, | |
} | |
// Payload: | |
// | |
// [ { data: { children: [ { data: Submission } ] } } | |
// , {} | |
// ] | |
// | |
// Path: [0] -> .data -> .children -> [0] -> .data | |
pub fn fetch_submission(client: &Client, thing_id: &str) -> Result<Option<Submission>, Error> { | |
let url: Url = format!("https://www.reddit.com/{}.json", thing_id).parse().unwrap(); | |
let mut response = client.get(url).send()?; | |
assert!(response.status().is_success()); | |
let body = response.json::<serde_json::Value>()?; | |
let submission: Option<Submission> = match body { | |
Value::Array(arr) => { | |
match arr.get(0) { | |
Some(Value::Object(map)) => { | |
match map.get("data") { | |
Some(Value::Object(map)) => { | |
match map.get("children") { | |
Some(Value::Array(arr)) => { | |
match arr.get(0) { | |
Some(Value::Object(map)) => { | |
match map.get("data") { | |
Some(Value::Object(map)) => { | |
serde_json::from_value(Value::Object(map.clone()))? | |
}, | |
_ => None | |
} | |
}, | |
_ => None | |
} | |
}, | |
_ => None | |
} | |
}, | |
_ => None | |
} | |
}, | |
_ => None | |
} | |
}, | |
_ => None | |
}; | |
Ok(submission) | |
} |
Author
danneu
commented
Sep 11, 2018
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment