summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main.rs51
1 files changed, 49 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs
index 2ec24c0..22c5530 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,6 +1,11 @@
+#![feature(custom_derive,plugin)]
+#![plugin(serde_macros)]
extern crate hyper;
extern crate irc;
+extern crate serde;
+extern crate serde_json;
+use std::collections::HashMap;
use std::io::Read;
use hyper::Client;
@@ -8,7 +13,35 @@ use hyper::header::Connection;
use irc::client::prelude::*;
+#[derive(Debug, PartialEq, Serialize, Deserialize)]
+struct WikiPage {
+ ns: u32,
+ pageid: u32,
+ title: String,
+ extract: String,
+}
+
+#[derive(Debug, PartialEq, Serialize, Deserialize)]
+struct WikiRedirect {
+ from: String,
+ to: String,
+}
+
+#[derive(Debug, PartialEq, Serialize, Deserialize)]
+struct WikiQueryResponse {
+ batchcomplete: String,
+ query: WikiQuery,
+}
+
+#[derive(Debug, PartialEq, Serialize, Deserialize)]
+struct WikiQuery {
+ redirects: Option<Vec<WikiRedirect>>,
+ pages: HashMap<String, WikiPage>,
+}
+
fn main() {
+ let q: WikiQueryResponse = serde_json::from_str("{\"batchcomplete\":\"\",\"query\":{\"pages\":{\"18123\":{\"pageid\":18123,\"ns\":0,\"title\":\"Lisp machine\",\"extract\":\"Lisp machines are general-purpose computers designed to efficiently run Lisp as their main software language, usually through hardware support. They are an example of a high-level language computer architecture, and in a sense, they were the first commercial single-user workstations. Despite being modest in number (perhaps 7,000 units total as of 1988), Lisp machines commercially pioneered many now-commonplace technologies \\u2013 including effective garbage collection, laser printing, windowing systems, computer mice, high-resolution bit-mapped graphics, computer graphic rendering, and networking innovations like Chaosnet. Several companies built and sold Lisp Machines in the 1980s: Symbolics (3600, 3640, XL1200, MacIvory, and other models), Lisp Machines Incorporated (LMI Lambda), Texas Instruments (Explorer and MicroExplorer), and Xerox (Interlisp-D workstations). The operating systems were written in Lisp Machine Lisp, InterLisp (Xerox), and later partly in Common Lisp.\"}}}}").unwrap();
+ println!("{:?}", q);
let config = Config {
nickname: Some(format!("lidavidm_prime")),
server: Some(format!("irc.freenode.net")),
@@ -28,14 +61,28 @@ fn main() {
server.send_privmsg(&message.args[0], &response).unwrap();
let mut client = Client::new();
- let mut res = client.get("https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exintro=&explaintext=&titles={}&redirects=")
+ let mut res = client
+ .get(&format!("https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exintro=&explaintext=&titles={}&redirects=", article.replace(" ", "%20")))
+ .header(hyper::header::UserAgent("lidavidm_irc_bot/0.1 (https://git.lidavidm.me; li.davidm96@gmail.com) hyper/0.7.2".to_owned()))
.header(Connection::close())
.send().unwrap();
let mut body = String::new();
res.read_to_string(&mut body).unwrap();
- println!("Response: {}", body);
+ let response: serde_json::Result<WikiQueryResponse> = serde_json::from_str(&body);
+ if let Ok(q) = response {
+ for page in q.query.pages.values() {
+ let url = format!("https://en.wikipedia.org/?curid={}", page.pageid);
+ server.send_privmsg(&message.args[0], &format!("{}: {}", page.title, url)).unwrap();
+ server.send_privmsg(&message.args[0], &page.extract).unwrap();
+ break;
+ }
+ }
+ else {
+ server.send_privmsg(&message.args[0], "Sorry, I couldn't retrieve the wiki page.").unwrap();
+ println!("{} {:?}", body, response);
+ }
}
}
}