From 938423224b0cfed3d7a36494bdf56b62e186b1c1 Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Sun, 17 Jun 2018 08:25:43 +0530 Subject: Refactor into content.rs --- src/content.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/content.rs (limited to 'src/content.rs') diff --git a/src/content.rs b/src/content.rs new file mode 100644 index 0000000..36d4da6 --- /dev/null +++ b/src/content.rs @@ -0,0 +1,38 @@ +pub fn url_gen(title: &str) -> String { + +// /w/api.php?action=query&format=json&prop=extracts&titles=rust&explaintext=1 + title.replace(" ", "%20"); + + // query config + let url = "https://en.wikipedia.org"; + url.push_str("w/api.php?"); + url.push_str("action=query&"); + url.push_str("format=json&"); + url.push_str("prop=extracts&"); + url.push_str(format!("titles={}", title)); + url.push_str("explaintext=1"); +} + +pub fn get_extract(title: &str, red: Response) -> String { + 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"), + }; + + if pageid_str == "-1" { + String::from("No such page") + } else { + format!("{}", &v["query"]["pages"][pageid_str]["extract"]) + } +} + +pub fn get_title(title: &str, res: Response) -> String { + let mut v: Value = serde_json::from_str(&res.text().unwrap()).unwrap_or_else( |e| { + panic!("Recieved invalid json"); + } ); + format!("{}", &v["query"]["normalized"][0]["to"]) +} -- cgit v1.2.3