aboutsummaryrefslogtreecommitdiff
path: root/src/content.rs
diff options
context:
space:
mode:
authorNerdyPepper <[email protected]>2018-06-19 14:11:53 +0100
committerNerdyPepper <[email protected]>2018-06-19 14:11:53 +0100
commit7e9e1d7850c95cc514c8cac54d8e6443d4c73742 (patch)
tree62d00cea67dfd110bb2920c7b76cb36afd7f60dc /src/content.rs
parent4bf31c45284b869a690d510300aa0a29a572f73c (diff)
Get add function to parse search results
Diffstat (limited to 'src/content.rs')
-rw-r--r--src/content.rs25
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 @@
1extern crate reqwest;
2extern crate serde_json;
3extern crate cursive;
4
5reqwest::Response;
6
1pub fn query_url_gen(title: &str) -> String { 7pub 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
17pub fn search_url_gen(search: &str) -> String { 23pub 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
31pub fn get_extract(title: &str, red: Response) -> String { 37pub 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
48pub fn get_title(title: &str, res: Response) -> String { 54pub 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
62pub 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}