diff options
author | NerdyPepper <[email protected]> | 2018-06-19 14:11:53 +0100 |
---|---|---|
committer | NerdyPepper <[email protected]> | 2018-06-19 14:11:53 +0100 |
commit | 7e9e1d7850c95cc514c8cac54d8e6443d4c73742 (patch) | |
tree | 62d00cea67dfd110bb2920c7b76cb36afd7f60dc | |
parent | 4bf31c45284b869a690d510300aa0a29a572f73c (diff) |
Get add function to parse search results
-rw-r--r-- | src/content.rs | 25 |
1 files 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 @@ | |||
1 | extern crate reqwest; | ||
2 | extern crate serde_json; | ||
3 | extern crate cursive; | ||
4 | |||
5 | reqwest::Response; | ||
6 | |||
1 | pub fn query_url_gen(title: &str) -> String { | 7 | pub fn query_url_gen(title: &str) -> String { |
2 | 8 | ||
3 | title.replace(" ", "%20"); | 9 | title.replace(" ", "%20"); |
@@ -16,7 +22,7 @@ pub fn query_url_gen(title: &str) -> String { | |||
16 | 22 | ||
17 | pub fn search_url_gen(search: &str) -> String { | 23 | pub fn search_url_gen(search: &str) -> String { |
18 | // /w/api.php?action=opensearch&format=json&search=dota%202&limit=5; | 24 | // /w/api.php?action=opensearch&format=json&search=dota%202&limit=5; |
19 | 25 | ||
20 | search.replace(" ", "%20"); | 26 | search.replace(" ", "%20"); |
21 | 27 | ||
22 | let url = "https://en.wikipedia.org"; | 28 | let url = "https://en.wikipedia.org"; |
@@ -28,7 +34,7 @@ pub fn search_url_gen(search: &str) -> String { | |||
28 | 34 | ||
29 | } | 35 | } |
30 | 36 | ||
31 | pub fn get_extract(title: &str, red: Response) -> String { | 37 | pub fn get_extract(title: &str, res: Response) -> String { |
32 | let mut v: Value = serde_json::from_str(&res.text().unwrap()).unwrap(); | 38 | let mut v: Value = serde_json::from_str(&res.text().unwrap()).unwrap(); |
33 | 39 | ||
34 | // Fetch page and pageids of requested title(s) | 40 | // Fetch page and pageids of requested title(s) |
@@ -46,8 +52,17 @@ pub fn get_extract(title: &str, red: Response) -> String { | |||
46 | } | 52 | } |
47 | 53 | ||
48 | pub fn get_title(title: &str, res: Response) -> String { | 54 | pub fn get_title(title: &str, res: Response) -> String { |
49 | let mut v: Value = serde_json::from_str(&res.text().unwrap()).unwrap_or_else( |e| { | 55 | let mut v: Value = serde_json::from_str(&res.text().unwrap()) |
50 | panic!("Recieved invalid json"); | 56 | .unwrap_or_else( |e| { |
51 | } ); | 57 | panic!("Recieved error {:?}", e); |
58 | } ); | ||
52 | format!("{}", &v["query"]["normalized"][0]["to"]) | 59 | format!("{}", &v["query"]["normalized"][0]["to"]) |
53 | } | 60 | } |
61 | |||
62 | pub fn get_search_results(search: &str, res: Response) -> Vec<String> { | ||
63 | let mut v: Value = serde_json::from_str(&res.text().unwrap()) | ||
64 | .unwrap_or_else( |e| { | ||
65 | panic!("Recieved error {:?}", e); | ||
66 | } ); | ||
67 | &v[1] | ||
68 | } | ||