From 7e9e1d7850c95cc514c8cac54d8e6443d4c73742 Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Tue, 19 Jun 2018 18:41:53 +0530 Subject: Get add function to parse search results --- src/content.rs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/content.rs b/src/content.rs index 17ad40a..993346a 100644 --- a/src/content.rs +++ b/src/content.rs @@ -1,3 +1,9 @@ +extern crate reqwest; +extern crate serde_json; +extern crate cursive; + +reqwest::Response; + pub fn query_url_gen(title: &str) -> String { title.replace(" ", "%20"); @@ -16,7 +22,7 @@ pub fn query_url_gen(title: &str) -> String { pub fn search_url_gen(search: &str) -> String { // /w/api.php?action=opensearch&format=json&search=dota%202&limit=5; - + search.replace(" ", "%20"); let url = "https://en.wikipedia.org"; @@ -28,7 +34,7 @@ pub fn search_url_gen(search: &str) -> String { } -pub fn get_extract(title: &str, red: Response) -> String { +pub fn get_extract(title: &str, res: Response) -> String { let mut v: Value = serde_json::from_str(&res.text().unwrap()).unwrap(); // Fetch page and pageids of requested title(s) @@ -46,8 +52,17 @@ pub fn get_extract(title: &str, red: Response) -> String { } 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"); - } ); + let mut v: Value = serde_json::from_str(&res.text().unwrap()) + .unwrap_or_else( |e| { + panic!("Recieved error {:?}", e); + } ); 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] +} -- cgit v1.2.3