aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/content.rs21
1 files changed, 2 insertions, 19 deletions
diff --git a/src/content.rs b/src/content.rs
index 117c6d0..ab2f08e 100644
--- a/src/content.rs
+++ b/src/content.rs
@@ -8,9 +8,6 @@ use serde_json::Value;
8use reqwest::Response; 8use reqwest::Response;
9 9
10pub fn query_url_gen(title: &str) -> String { 10pub fn query_url_gen(title: &str) -> String {
11
12// /w/api.php?action=query&format=json&prop=extracts&titles=dota_2&exlimit=20&explaintext=1
13
14 // query config 11 // query config
15 let mut url = String::from("https://en.wikipedia.org"); 12 let mut url = String::from("https://en.wikipedia.org");
16 url.push_str("/w/api.php?"); 13 url.push_str("/w/api.php?");
@@ -22,15 +19,11 @@ pub fn query_url_gen(title: &str) -> String {
22 url.push_str(title); 19 url.push_str(title);
23 url.push_str("&"); 20 url.push_str("&");
24 url.push_str("explaintext=1"); 21 url.push_str("explaintext=1");
25
26 url 22 url
27} 23}
28 24
29pub fn search_url_gen(search: &str) -> String { 25pub fn search_url_gen(search: &str) -> String {
30 // /w/api.php?action=opensearch&format=json&search=dota%202&limit=5;
31
32 search.replace(" ", "%20"); 26 search.replace(" ", "%20");
33
34 let mut url = String::from("https://en.wikipedia.org"); 27 let mut url = String::from("https://en.wikipedia.org");
35 url.push_str("/w/api.php?"); 28 url.push_str("/w/api.php?");
36 url.push_str("action=opensearch&"); 29 url.push_str("action=opensearch&");
@@ -38,14 +31,13 @@ pub fn search_url_gen(search: &str) -> String {
38 url.push_str("search="); 31 url.push_str("search=");
39 url.push_str(search); 32 url.push_str(search);
40 url.push_str("&"); 33 url.push_str("&");
41 url.push_str("limit=5"); 34 url.push_str("limit=10");
42 35
43 url 36 url
44 37
45} 38}
46 39
47pub fn get_extract(mut res: Response) -> Result<String, reqwest::Error> { 40pub fn get_extract(mut res: Response) -> Result<String, reqwest::Error> {
48
49 let v: Value = match serde_json::from_str(&res.text()?) { 41 let v: Value = match serde_json::from_str(&res.text()?) {
50 Ok(x) => x, 42 Ok(x) => x,
51 Err(x) => panic!("Failed to parse json\nReceived error {}", x), 43 Err(x) => panic!("Failed to parse json\nReceived error {}", x),
@@ -58,9 +50,9 @@ pub fn get_extract(mut res: Response) -> Result<String, reqwest::Error> {
58 50
59 match &v["query"]["pages"][pageid_str]["extract"] { 51 match &v["query"]["pages"][pageid_str]["extract"] {
60 Value::String(extract) => { 52 Value::String(extract) => {
61
62 // format to plain text 53 // format to plain text
63 extract.replace("\\\\", "\\"); 54 extract.replace("\\\\", "\\");
55
64 Ok(format!("{}", extract)) 56 Ok(format!("{}", extract))
65 } 57 }
66 // ignore non strings 58 // ignore non strings
@@ -68,16 +60,7 @@ pub fn get_extract(mut res: Response) -> Result<String, reqwest::Error> {
68 } 60 }
69} 61}
70 62
71pub fn get_title(title: &str, mut res: Response) -> Result<String, reqwest::Error> {
72 let v: Value = serde_json::from_str(&res.text()?)
73 .unwrap_or_else( |e| {
74 panic!("Recieved error {:?}", e);
75 } );
76 Ok(format!("{}", &v["query"]["normalized"][0]["to"]))
77}
78
79pub fn get_search_results(search: &str) -> Result<Vec<String>, reqwest::Error> { 63pub fn get_search_results(search: &str) -> Result<Vec<String>, reqwest::Error> {
80
81 let url = search_url_gen(search); 64 let url = search_url_gen(search);
82 let mut res = reqwest::get(&url[..])?; 65 let mut res = reqwest::get(&url[..])?;
83 let v: Value = serde_json::from_str(&res.text().unwrap()) 66 let v: Value = serde_json::from_str(&res.text().unwrap())