aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNerdyPepper <[email protected]>2018-06-19 16:19:47 +0100
committerNerdyPepper <[email protected]>2018-06-19 16:19:47 +0100
commit2e5296aea2c6aa6dfc90b9daf6f20b82ead89bc0 (patch)
tree2a060d4d0444b8ff347c25f8323f5f4ed82adc90 /src
parentb878be7919a079b17768a25f274db786ddc42c99 (diff)
Squash 21 bugs, refactor searching function
Diffstat (limited to 'src')
-rw-r--r--src/content.rs57
1 files changed, 44 insertions, 13 deletions
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;
2extern crate serde_json; 2extern crate serde_json;
3extern crate cursive; 3extern crate cursive;
4 4
5reqwest::Response; 5use serde_json::Value;
6use reqwest::Response;
6 7
7pub fn query_url_gen(title: &str) -> String { 8pub fn query_url_gen(title: &str) -> String {
8 9
9 title.replace(" ", "%20"); 10 title.replace(" ", "%20");
10 11
11 // query config 12 // query config
12 let url = "https://en.wikipedia.org"; 13 let mut url = String::from("https://en.wikipedia.org");
13 url.push_str("w/api.php?"); 14 url.push_str("w/api.php?");
14 url.push_str("action=query&"); 15 url.push_str("action=query&");
15 url.push_str("format=json&"); 16 url.push_str("format=json&");
16 url.push_str("prop=extracts&"); 17 url.push_str("prop=extracts&");
17 url.push_str(format!("titles={}&", title)); 18 url.push_str("titles=");
19 url.push_str(title);
20 url.push_str("&");
18 url.push_str("explaintext=1"); 21 url.push_str("explaintext=1");
19 22
20 url 23 url
@@ -25,16 +28,20 @@ pub fn search_url_gen(search: &str) -> String {
25 28
26 search.replace(" ", "%20"); 29 search.replace(" ", "%20");
27 30
28 let url = "https://en.wikipedia.org"; 31 let mut url = String::from("https://en.wikipedia.org");
29 url.push_str("w/api.php?"); 32 url.push_str("w/api.php?");
30 url.push_str("action=opensearch&"); 33 url.push_str("action=opensearch&");
31 url.push_str("format=json&"); 34 url.push_str("format=json&");
32 url.push_str(format!("search={}&", search)); 35 url.push_str("search=");
36 url.push_str(search);
37 url.push_str("&");
33 url.push_str("limit=5"); 38 url.push_str("limit=5");
34 39
40 url
41
35} 42}
36 43
37pub fn get_extract(title: &str, res: Response) -> String { 44pub fn get_extract(title: &str, mut res: reqwest::Response) -> String {
38 let mut v: Value = serde_json::from_str(&res.text().unwrap()).unwrap(); 45 let mut v: Value = serde_json::from_str(&res.text().unwrap()).unwrap();
39 46
40 // Fetch page and pageids of requested title(s) 47 // Fetch page and pageids of requested title(s)
@@ -51,7 +58,7 @@ pub fn get_extract(title: &str, res: Response) -> String {
51 } 58 }
52} 59}
53 60
54pub fn get_title(title: &str, res: Response) -> String { 61pub fn get_title(title: &str, mut res: Response) -> String {
55 let mut v: Value = serde_json::from_str(&res.text().unwrap()) 62 let mut v: Value = serde_json::from_str(&res.text().unwrap())
56 .unwrap_or_else( |e| { 63 .unwrap_or_else( |e| {
57 panic!("Recieved error {:?}", e); 64 panic!("Recieved error {:?}", e);
@@ -59,10 +66,34 @@ pub fn get_title(title: &str, res: Response) -> String {
59 format!("{}", &v["query"]["normalized"][0]["to"]) 66 format!("{}", &v["query"]["normalized"][0]["to"])
60} 67}
61 68
62pub fn get_search_results(search: &str, res: Response) -> Vec<String> { 69pub fn get_search_results(search: &str) -> Vec<String> {
63 let mut v: Value = serde_json::from_str(&res.text().unwrap()) 70
64 .unwrap_or_else( |e| { 71 let url = search_url_gen(search);
65 panic!("Recieved error {:?}", e); 72 let res = reqwest::get(&url[..]);
66 } ); 73
67 &v[1] 74 match res {
75 Ok(mut res) => {
76 if res.status().is_success() {
77 let mut v: Value = serde_json::from_str(&res.text().unwrap())
78 .unwrap_or_else( |e| {
79 panic!("Recieved error {:?}", e);
80 } );
81
82 let mut results: Vec<String> = vec![];
83 for item in v[1].as_array().unwrap() {
84 match item {
85 Value::String(x) => results.push(x.to_string()),
86 _ => (),
87 }
88 }
89 results
90 } else {
91 panic!("Encountered Error {}", res.status());
92 }
93 },
94 _ => {
95 panic!("Unable to parse url");
96 }
97 }
98
68} 99}