diff options
-rw-r--r-- | src/content.rs | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/src/content.rs b/src/content.rs index ed1d8c5..90fd936 100644 --- a/src/content.rs +++ b/src/content.rs | |||
@@ -2,7 +2,7 @@ extern crate cursive; | |||
2 | extern crate regex; | 2 | extern crate regex; |
3 | extern crate reqwest; | 3 | extern crate reqwest; |
4 | extern crate serde_json; | 4 | extern crate serde_json; |
5 | extern crate urlencoding; | 5 | extern crate url; |
6 | 6 | ||
7 | use self::regex::Regex; | 7 | use self::regex::Regex; |
8 | use cursive::theme::Effect; | 8 | use cursive::theme::Effect; |
@@ -13,32 +13,48 @@ use reqwest::Url; | |||
13 | use serde_json::Value; | 13 | use serde_json::Value; |
14 | use CONFIGURATION; | 14 | use CONFIGURATION; |
15 | 15 | ||
16 | use content::url::percent_encoding::{ utf8_percent_encode, DEFAULT_ENCODE_SET }; | ||
17 | |||
18 | use std::fs::File; | ||
19 | use std::io::prelude::*; | ||
20 | |||
16 | pub fn query_url_gen(title: &str) -> Url { | 21 | pub fn query_url_gen(title: &str) -> Url { |
17 | Url::parse_with_params( | 22 | let url = Url::parse_with_params( |
18 | &(CONFIGURATION.wiki_url.clone() + "/w/api.php"), | 23 | &(CONFIGURATION.wiki_url.clone() + "/w/api.php"), |
19 | &[ | 24 | &[ |
20 | ("action", "query"), | 25 | ("action", "query"), |
21 | ("format", "json"), | 26 | ("format", "json"), |
22 | ("prop", "extracts|links"), | 27 | ("prop", "extracts|links"), |
23 | ("indexpageids", "1"), | 28 | ("indexpageids", "1"), |
24 | ("titles", &urlencoding::encode(&title.replace(" ", "_"))), | 29 | ("titles", &utf8_percent_encode(&title.replace(" ", "_"), DEFAULT_ENCODE_SET).to_string()[..]), |
25 | ("redirects", "1"), | 30 | ("redirects", "1"), |
26 | ("pllimit", "100"), | 31 | ("pllimit", "100"), |
27 | ("explaintext", "1"), | 32 | ("explaintext", "1"), |
28 | ], | 33 | ], |
29 | ).unwrap() | 34 | ).unwrap(); |
35 | |||
36 | let mut f = File::open("~/.taizen_logs").unwrap(); | ||
37 | f.write_all(url.as_str().as_bytes()).unwrap(); | ||
38 | |||
39 | return url; | ||
30 | } | 40 | } |
31 | 41 | ||
32 | pub fn search_url_gen(search: &str) -> Url { | 42 | pub fn search_url_gen(search: &str) -> Url { |
33 | Url::parse_with_params( | 43 | let url = Url::parse_with_params( |
34 | &(CONFIGURATION.wiki_url.clone() + "/w/api.php"), | 44 | &(CONFIGURATION.wiki_url.clone() + "/w/api.php"), |
35 | &[ | 45 | &[ |
36 | ("action", "opensearch"), | 46 | ("action", "opensearch"), |
37 | ("format", "json"), | 47 | ("format", "json"), |
38 | ("search", &urlencoding::encode(search)), | 48 | ("search", &utf8_percent_encode(&search, DEFAULT_ENCODE_SET).to_string()[..]), |
39 | ("limit", "20"), | 49 | ("limit", "20"), |
40 | ], | 50 | ], |
41 | ).unwrap() | 51 | ).unwrap(); |
52 | |||
53 | let mut f = File::create("taizen_logs.txt").unwrap(); | ||
54 | f.write_all(url.as_str().as_bytes()).expect("failed to write unicode"); | ||
55 | f.write_all(search.as_bytes()).unwrap(); | ||
56 | |||
57 | return url; | ||
42 | } | 58 | } |
43 | 59 | ||
44 | pub fn get_extract(v: &Value) -> Result<String, reqwest::Error> { | 60 | pub fn get_extract(v: &Value) -> Result<String, reqwest::Error> { |
@@ -104,8 +120,14 @@ pub fn get_search_results(search: &str) -> Result<Vec<String>, reqwest::Error> { | |||
104 | for item in v[1].as_array().unwrap() { | 120 | for item in v[1].as_array().unwrap() { |
105 | if let Value::String(x) = item { | 121 | if let Value::String(x) = item { |
106 | results.push(x.to_string()) | 122 | results.push(x.to_string()) |
107 | } | 123 | }; |
108 | } | 124 | } |
125 | |||
126 | // let mut f = File::open("taizen_logs.txt").unwrap(); | ||
127 | // for result in &results { | ||
128 | // f.write_all(result.as_bytes()).unwrap(); | ||
129 | // } | ||
130 | |||
109 | Ok(results) | 131 | Ok(results) |
110 | } | 132 | } |
111 | 133 | ||