diff options
Diffstat (limited to 'src/content.rs')
-rw-r--r-- | src/content.rs | 38 |
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 @@ | |||
1 | pub 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 | |||
16 | pub 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 | |||
33 | pub 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 | } | ||