aboutsummaryrefslogtreecommitdiff
path: root/src/content.rs
diff options
context:
space:
mode:
authorNerdyPepper <[email protected]>2018-06-17 03:55:43 +0100
committerNerdyPepper <[email protected]>2018-06-17 03:55:43 +0100
commit938423224b0cfed3d7a36494bdf56b62e186b1c1 (patch)
tree5a4df550a9017e65a670ae1afcd196397c0dc645 /src/content.rs
parent3982993f18538932bfdd8d310b63bca02324fece (diff)
Refactor into content.rs
Diffstat (limited to 'src/content.rs')
-rw-r--r--src/content.rs38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/content.rs b/src/content.rs
new file mode 100644
index 0000000..36d4da6
--- /dev/null
+++ b/src/content.rs
@@ -0,0 +1,38 @@
1pub fn url_gen(title: &str) -> String {
2
3// /w/api.php?action=query&format=json&prop=extracts&titles=rust&explaintext=1
4 title.replace(" ", "%20");
5
6 // query config
7 let url = "https://en.wikipedia.org";
8 url.push_str("w/api.php?");
9 url.push_str("action=query&");
10 url.push_str("format=json&");
11 url.push_str("prop=extracts&");
12 url.push_str(format!("titles={}", title));
13 url.push_str("explaintext=1");
14}
15
16pub fn get_extract(title: &str, red: Response) -> String {
17 let mut v: Value = serde_json::from_str(&res.text().unwrap()).unwrap();
18
19 // Fetch page and pageids of requested title(s)
20 let pageid = &v["query"]["pageids"][0];
21 let pageid_str = match pageid {
22 Value::String(id) => id,
23 _ => panic!("wut"),
24 };
25
26 if pageid_str == "-1" {
27 String::from("No such page")
28 } else {
29 format!("{}", &v["query"]["pages"][pageid_str]["extract"])
30 }
31}
32
33pub fn get_title(title: &str, res: Response) -> String {
34 let mut v: Value = serde_json::from_str(&res.text().unwrap()).unwrap_or_else( |e| {
35 panic!("Recieved invalid json");
36 } );
37 format!("{}", &v["query"]["normalized"][0]["to"])
38}