From 2e5296aea2c6aa6dfc90b9daf6f20b82ead89bc0 Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Tue, 19 Jun 2018 20:49:47 +0530 Subject: Squash 21 bugs, refactor searching function --- src/content.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/content.rs b/src/content.rs index 993346a..5be4206 100644 --- a/src/content.rs +++ b/src/content.rs @@ -2,19 +2,22 @@ extern crate reqwest; extern crate serde_json; extern crate cursive; -reqwest::Response; +use serde_json::Value; +use reqwest::Response; pub fn query_url_gen(title: &str) -> String { title.replace(" ", "%20"); // query config - let url = "https://en.wikipedia.org"; + let mut url = String::from("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("titles="); + url.push_str(title); + url.push_str("&"); url.push_str("explaintext=1"); url @@ -25,16 +28,20 @@ pub fn search_url_gen(search: &str) -> String { search.replace(" ", "%20"); - let url = "https://en.wikipedia.org"; + let mut url = String::from("https://en.wikipedia.org"); url.push_str("w/api.php?"); url.push_str("action=opensearch&"); url.push_str("format=json&"); - url.push_str(format!("search={}&", search)); + url.push_str("search="); + url.push_str(search); + url.push_str("&"); url.push_str("limit=5"); + url + } -pub fn get_extract(title: &str, res: Response) -> String { +pub fn get_extract(title: &str, mut res: reqwest::Response) -> String { let mut v: Value = serde_json::from_str(&res.text().unwrap()).unwrap(); // Fetch page and pageids of requested title(s) @@ -51,7 +58,7 @@ pub fn get_extract(title: &str, res: Response) -> String { } } -pub fn get_title(title: &str, res: Response) -> String { +pub fn get_title(title: &str, mut res: Response) -> String { let mut v: Value = serde_json::from_str(&res.text().unwrap()) .unwrap_or_else( |e| { panic!("Recieved error {:?}", e); @@ -59,10 +66,34 @@ pub fn get_title(title: &str, res: Response) -> String { format!("{}", &v["query"]["normalized"][0]["to"]) } -pub fn get_search_results(search: &str, res: Response) -> Vec { - let mut v: Value = serde_json::from_str(&res.text().unwrap()) - .unwrap_or_else( |e| { - panic!("Recieved error {:?}", e); - } ); - &v[1] +pub fn get_search_results(search: &str) -> Vec { + + let url = search_url_gen(search); + 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_or_else( |e| { + panic!("Recieved error {:?}", e); + } ); + + let mut results: Vec = vec![]; + for item in v[1].as_array().unwrap() { + match item { + Value::String(x) => results.push(x.to_string()), + _ => (), + } + } + results + } else { + panic!("Encountered Error {}", res.status()); + } + }, + _ => { + panic!("Unable to parse url"); + } + } + } -- cgit v1.2.3