Get and parse weather data, using rustc_serialize.

This commit is contained in:
Darren VanBuren 2016-07-27 04:58:29 -07:00
parent bd16ae1514
commit 4df059bfc1
3 changed files with 33 additions and 14 deletions

1
Cargo.lock generated
View file

@ -4,6 +4,7 @@ version = "0.1.0"
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)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]

View file

@ -4,5 +4,6 @@ version = "0.1.0"
authors = ["Darren VanBuren <onekopaka@theoks.net>"]
[dependencies]
irc = "0.11.0"
irc = "0.11"
hyper = "0.9"
rustc-serialize = "0.3"

View file

@ -1,9 +1,11 @@
extern crate irc;
extern crate hyper;
extern crate rustc_serialize;
use std::io::Read;
use irc::client::prelude::*;
use hyper::{Client};
use rustc_serialize::json::{self};
static WEATHER_API_BASE: &'static str = "http://api.wunderground.com/api/";
@ -14,32 +16,33 @@ fn main() {
server.identify().unwrap();
for message in server.iter() {
let message = message.unwrap();
println!("Received message: {}", message);
//println!("Received message: {}", message);
match message.command {
Command::PRIVMSG(ref target, ref msg) => handle_privmsg(target, msg, &server),
Command::PRIVMSG(ref target, ref msg) => handle_privmsg(target, msg, &message, &server),
_ => (),
}
}
}
fn handle_privmsg(target: &String, msg: &String, server: &IrcServer) {
if msg.contains("rust-bot") {
fn handle_privmsg(target: &String, message_body: &String, message_obj: &Message, server: &IrcServer) {
println!("Received message in {} from {}: {}", target, message_obj.source_nickname().unwrap(), message_body);
let source_nick = message_obj.source_nickname();
if message_body.contains("rust-bot") {
server.send_privmsg(target, "Hello!").unwrap();
} else if msg.contains("test_get_weather") {
} else if message_body.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),
Some(api_key) => server.send_privmsg(target, &format!("{}: {}", source_nick.unwrap(), &test_get_weather(api_key))).unwrap(),
}
}
server.send_privmsg(target, "Check console for output!").unwrap();
} else if msg.contains("bot-quit") {
} else if message_body.contains("bot-quit") {
std::process::exit(0);
}
}
fn test_get_weather(api_key: &String) {
fn test_get_weather(api_key: &String) -> String {
let client = Client::new();
let data_type = "conditions";
let location = "WA/Maple_Valley";
@ -47,12 +50,26 @@ fn test_get_weather(api_key: &String) {
println!("Attempting to fetch {}", url);
let mut response = match client.get(url).send() {
Ok(response) => response,
Err(_) => panic!("Error!"),
Err(_) => panic!("Error fetching JSON!"),
};
let mut buf = String::new();
match response.read_to_string(&mut buf) {
let mut response_body = String::new();
match response.read_to_string(&mut response_body) {
Ok(_) => (),
Err(_) => panic!("Error reading to buffer"),
};
println!("buf: {}", buf);
let mut reply_message = String::new();
if let Ok(parsed_response) = json::Json::from_str(&response_body) {
if let Some(ref current_observation) = parsed_response.find("current_observation") {
let ref display_location = current_observation.find("display_location").unwrap();
let display_location_string = display_location.find("full").unwrap().as_string().unwrap();
let temperature_string = current_observation.find("temperature_string").unwrap().as_string().unwrap();
let weather = current_observation.find("weather").unwrap().as_string().unwrap();
let wind_string = current_observation.find("wind_string").unwrap().as_string().unwrap();
let humidity = current_observation.find("relative_humidity").unwrap().as_string().unwrap();
let observation_time = current_observation.find("observation_time").unwrap().as_string().unwrap();
reply_message = format!("It is {} and {} in {}. Wind: {}, Humidity: {}, {}",
weather, temperature_string, display_location_string, wind_string, humidity, observation_time);
}
}
reply_message
}