From 3982993f18538932bfdd8d310b63bca02324fece Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Sat, 16 Jun 2018 14:15:11 +0530 Subject: Finish basic extracts --- src/main.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/main.rs (limited to 'src') diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..951c94c --- /dev/null +++ b/src/main.rs @@ -0,0 +1,35 @@ +extern crate reqwest; +extern crate serde_json; + +use serde_json::Value; + +fn main() { + println!("{}", get_extract("scale")); +} + +fn get_extract(title: &str) -> String { + let url = format!("https://en.wikipedia.org/w/api.php?action=query&format=json&prop=extracts&list=&meta=&indexpageids=1&continue=%7C%7Cimageinfo&titles={}&exlimit=20&explaintext=1&exsectionformat=plain", title); + let res = reqwest::get(&url); + + match res { + Ok(mut res) => { + if res.status().is_success() { + let mut v: Value = serde_json::from_str(&res.text().unwrap()).unwrap(); + + // Fetch page and pageids of requested title(s) + let pageid = &v["query"]["pageids"][0]; + let pageid_str = match pageid { + Value::String(id) => id, + _ => panic!("wut"), + }; + + format!("{:#}", &v["query"]["pages"][pageid_str]["extract"]) + } else { + format!("Error while parsing url.\nRecieved {}", res.status()) + } + }, + Err(_) => { + format!("Failed to parse URL") + } + } +} -- cgit v1.2.3