Attempt to get weather data, move handling messages to a new function

This commit is contained in:
Darren VanBuren 2016-07-26 22:58:06 -07:00
parent 5bbb554186
commit bd16ae1514
2 changed files with 199 additions and 3 deletions

158
Cargo.lock generated
View file

@ -2,6 +2,7 @@
name = "rust-bot" name = "rust-bot"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"hyper 0.9.10 (registry+https://github.com/rust-lang/crates.io-index)",
"irc 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", "irc 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@ -10,6 +11,17 @@ name = "bitflags"
version = "0.7.0" version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "cookie"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"openssl 0.7.14 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
"url 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "encoding" name = "encoding"
version = "0.2.32" version = "0.2.32"
@ -81,6 +93,51 @@ dependencies = [
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "hpack"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "httparse"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "hyper"
version = "0.9.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cookie 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"httparse 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"mime 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"num_cpus 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)",
"openssl 0.7.14 (registry+https://github.com/rust-lang/crates.io-index)",
"openssl-verify 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
"solicit 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
"traitobject 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"typeable 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"unicase 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"url 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "idna"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-bidi 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-normalization 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "irc" name = "irc"
version = "0.11.3" version = "0.11.3"
@ -101,6 +158,11 @@ dependencies = [
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "language-tags"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "0.2.1" version = "0.2.1"
@ -119,6 +181,32 @@ dependencies = [
"pnacl-build-helper 1.4.10 (registry+https://github.com/rust-lang/crates.io-index)", "pnacl-build-helper 1.4.10 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "log"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "matches"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "mime"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num_cpus"
version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "openssl" name = "openssl"
version = "0.7.14" version = "0.7.14"
@ -154,6 +242,14 @@ dependencies = [
"openssl-sys 0.7.14 (registry+https://github.com/rust-lang/crates.io-index)", "openssl-sys 0.7.14 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "openssl-verify"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"openssl 0.7.14 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "pkg-config" name = "pkg-config"
version = "0.3.8" version = "0.3.8"
@ -180,6 +276,28 @@ name = "rustc-serialize"
version = "0.3.19" version = "0.3.19"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rustc_version"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"semver 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "semver"
version = "0.1.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "solicit"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"hpack 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "tempdir" name = "tempdir"
version = "0.3.4" version = "0.3.4"
@ -198,6 +316,46 @@ dependencies = [
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "traitobject"
version = "0.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "typeable"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "unicase"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "unicode-bidi"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "unicode-normalization"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "url"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"idna 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "user32-sys" name = "user32-sys"
version = "0.2.0" version = "0.2.0"

View file

@ -1,6 +1,11 @@
extern crate irc; extern crate irc;
extern crate hyper;
use std::io::Read;
use irc::client::prelude::*; use irc::client::prelude::*;
use hyper::{Client};
static WEATHER_API_BASE: &'static str = "http://api.wunderground.com/api/";
fn main() { fn main() {
println!("Hello, world!"); println!("Hello, world!");
@ -11,10 +16,43 @@ fn main() {
let message = message.unwrap(); let message = message.unwrap();
println!("Received message: {}", message); println!("Received message: {}", message);
match message.command { match message.command {
Command::PRIVMSG(ref target, ref msg) => if msg.contains("rust-bot") { Command::PRIVMSG(ref target, ref msg) => handle_privmsg(target, msg, &server),
server.send_privmsg(target, "Hello!").unwrap();
},
_ => (), _ => (),
} }
} }
} }
fn handle_privmsg(target: &String, msg: &String, server: &IrcServer) {
if msg.contains("rust-bot") {
server.send_privmsg(target, "Hello!").unwrap();
} else if msg.contains("test_get_weather") {
match server.config().options {
None => println!("Options not configured!"),
Some(ref options) => match options.get("wunderground_api_key") {
None => println!("wunderground_api_key not configured!"),
Some(api_key) => test_get_weather(api_key),
}
}
server.send_privmsg(target, "Check console for output!").unwrap();
} else if msg.contains("bot-quit") {
std::process::exit(0);
}
}
fn test_get_weather(api_key: &String) {
let client = Client::new();
let data_type = "conditions";
let location = "WA/Maple_Valley";
let url = &format!("{}{}/{}/q/{}.json", WEATHER_API_BASE, api_key, data_type, location);
println!("Attempting to fetch {}", url);
let mut response = match client.get(url).send() {
Ok(response) => response,
Err(_) => panic!("Error!"),
};
let mut buf = String::new();
match response.read_to_string(&mut buf) {
Ok(_) => (),
Err(_) => panic!("Error reading to buffer"),
};
println!("buf: {}", buf);
}