aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/content.rs49
1 files changed, 39 insertions, 10 deletions
diff --git a/src/content.rs b/src/content.rs
index 5be4206..19293d2 100644
--- a/src/content.rs
+++ b/src/content.rs
@@ -2,6 +2,8 @@ extern crate reqwest;
2extern crate serde_json; 2extern crate serde_json;
3extern crate cursive; 3extern crate cursive;
4 4
5use cursive::Cursive;
6use cursive::views::Dialog;
5use serde_json::Value; 7use serde_json::Value;
6use reqwest::Response; 8use reqwest::Response;
7 9
@@ -11,7 +13,7 @@ pub fn query_url_gen(title: &str) -> String {
11 13
12 // query config 14 // query config
13 let mut url = String::from("https://en.wikipedia.org"); 15 let mut url = String::from("https://en.wikipedia.org");
14 url.push_str("w/api.php?"); 16 url.push_str("/w/api.php?");
15 url.push_str("action=query&"); 17 url.push_str("action=query&");
16 url.push_str("format=json&"); 18 url.push_str("format=json&");
17 url.push_str("prop=extracts&"); 19 url.push_str("prop=extracts&");
@@ -29,7 +31,7 @@ pub fn search_url_gen(search: &str) -> String {
29 search.replace(" ", "%20"); 31 search.replace(" ", "%20");
30 32
31 let mut url = String::from("https://en.wikipedia.org"); 33 let mut url = String::from("https://en.wikipedia.org");
32 url.push_str("w/api.php?"); 34 url.push_str("/w/api.php?");
33 url.push_str("action=opensearch&"); 35 url.push_str("action=opensearch&");
34 url.push_str("format=json&"); 36 url.push_str("format=json&");
35 url.push_str("search="); 37 url.push_str("search=");
@@ -41,21 +43,22 @@ pub fn search_url_gen(search: &str) -> String {
41 43
42} 44}
43 45
44pub fn get_extract(title: &str, mut res: reqwest::Response) -> String { 46pub fn get_extract(title: &str) -> Result<String, reqwest::Error> {
45 let mut v: Value = serde_json::from_str(&res.text().unwrap()).unwrap(); 47 let url = query_url_gen(title);
48 let res = reqwest::get(&url[..])?;
46 49
47 // Fetch page and pageids of requested title(s) 50 let mut v: Value = match serde_json::from_str(&res.text()?) {
51 Ok(x) => x,
52 Err(x) => ,
53 };
48 let pageid = &v["query"]["pageids"][0]; 54 let pageid = &v["query"]["pageids"][0];
49 let pageid_str = match pageid { 55 let pageid_str = match pageid {
50 Value::String(id) => id, 56 Value::String(id) => id,
51 _ => panic!("wut"), 57 _ => panic!("wut"),
52 }; 58 };
53 59
54 if pageid_str == "-1" { 60 Ok(format!("{}", &v["query"]["pages"][pageid_str]["extract"]))
55 String::from("No such page") 61
56 } else {
57 format!("{}", &v["query"]["pages"][pageid_str]["extract"])
58 }
59} 62}
60 63
61pub fn get_title(title: &str, mut res: Response) -> String { 64pub fn get_title(title: &str, mut res: Response) -> String {
@@ -97,3 +100,29 @@ pub fn get_search_results(search: &str) -> Vec<String> {
97 } 100 }
98 101
99} 102}
103
104pub fn pop_error(s: &mut Cursive, msg: &str) {
105 s.add_layer(Dialog::text("An error occurred\n:(")
106 .title("Oopsie woopsie")
107 .button("Ok", |s| s.quit()));
108}
109
110fn handler(e: reqwest::Error) -> &str {
111 if e.is_http() {
112 match e.url() {
113 None => format!("No URL given"),
114 Some(url) => format!("Problem making request to: {}", url),
115 }
116 }
117 // Inspect the internal error and output it
118 if e.is_serialization() {
119 let serde_error = match e.get_ref() {
120 None => return,
121 Some(err) => err,
122 };
123 format!("problem parsing information {}", serde_error)
124
125 if e.is_redirect() {
126 format!("server redirecting too many times or making loop")
127 }
128}