diff options
author | NerdyPepper <[email protected]> | 2018-06-16 09:45:11 +0100 |
---|---|---|
committer | NerdyPepper <[email protected]> | 2018-06-16 09:45:11 +0100 |
commit | 3982993f18538932bfdd8d310b63bca02324fece (patch) | |
tree | d82b71971b7cb1bca99fdc3e0ffef10ca7d0d9d4 /src |
Finish basic extracts
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 35 |
1 files changed, 35 insertions, 0 deletions
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 @@ | |||
1 | extern crate reqwest; | ||
2 | extern crate serde_json; | ||
3 | |||
4 | use serde_json::Value; | ||
5 | |||
6 | fn main() { | ||
7 | println!("{}", get_extract("scale")); | ||
8 | } | ||
9 | |||
10 | fn get_extract(title: &str) -> String { | ||
11 | 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); | ||
12 | let res = reqwest::get(&url); | ||
13 | |||
14 | match res { | ||
15 | Ok(mut res) => { | ||
16 | if res.status().is_success() { | ||
17 | let mut v: Value = serde_json::from_str(&res.text().unwrap()).unwrap(); | ||
18 | |||
19 | // Fetch page and pageids of requested title(s) | ||
20 | let pageid = &v["query"]["pageids"][0]; | ||
21 | let pageid_str = match pageid { | ||
22 | Value::String(id) => id, | ||
23 | _ => panic!("wut"), | ||
24 | }; | ||
25 | |||
26 | format!("{:#}", &v["query"]["pages"][pageid_str]["extract"]) | ||
27 | } else { | ||
28 | format!("Error while parsing url.\nRecieved {}", res.status()) | ||
29 | } | ||
30 | }, | ||
31 | Err(_) => { | ||
32 | format!("Failed to parse URL") | ||
33 | } | ||
34 | } | ||
35 | } | ||