From 3d97df6f57a43e30a1fc06bdf29882a6ae991f8b Mon Sep 17 00:00:00 2001 From: Darren VanBuren Date: Thu, 28 Jul 2016 02:23:43 -0700 Subject: [PATCH] Make Weather command input locations --- Cargo.lock | 1 + Cargo.toml | 1 + src/main.rs | 35 +++++++++++++++++++++++++++-------- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0a8ba81..b3c0696 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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)", + "lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.1.73 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/Cargo.toml b/Cargo.toml index 01df7ce..9af032a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,4 @@ irc = "0.11" hyper = "0.9" rustc-serialize = "0.3" regex = "0.1" +lazy_static = "0.2" diff --git a/src/main.rs b/src/main.rs index e685f4d..d6aca58 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ extern crate irc; extern crate hyper; extern crate rustc_serialize; extern crate regex; +#[macro_use] extern crate lazy_static; use std::io::Read; use irc::client::prelude::*; @@ -16,21 +17,21 @@ fn main() { let server = IrcServer::new("config.json").unwrap(); server.identify().unwrap(); + let my_nick = server.current_nickname(); + let mentions_regex = Regex::new(&format!("({}(:|,) )(.+)$", my_nick)).unwrap(); for message in server.iter() { let message = message.unwrap(); //println!("Received message: {}", message); match message.command { - Command::PRIVMSG(ref target, ref msg) => handle_privmsg(target, msg, &message, &server), + Command::PRIVMSG(ref target, ref msg) => handle_privmsg(target, msg, &message, &mentions_regex, &server), _ => (), } } } -fn handle_privmsg(target: &String, message_body: &String, message_obj: &Message, server: &IrcServer) { +fn handle_privmsg(target: &String, message_body: &String, message_obj: &Message, mentions_regex: &Regex, server: &IrcServer) { println!("Received message in {} from {}: {}", target, message_obj.source_nickname().unwrap(), message_body); let source_nick = message_obj.source_nickname().unwrap(); - let my_nick = server.current_nickname(); - let mentions_regex = Regex::new(&format!("({}(:|,) )(.+)$", my_nick)).unwrap(); let mut inner_message = String::new(); match mentions_regex.captures(message_body) { Some(capture) => match capture.at(3) { @@ -44,11 +45,30 @@ fn handle_privmsg(target: &String, message_body: &String, message_obj: &Message, if inner_message == String::from("test") { server.send_privmsg(target, &format!("{}: Hello!", source_nick)).unwrap(); } else if inner_message.starts_with("weather") { + // Weather + lazy_static! {static ref WEATHER_REGEX: Regex = Regex::new("(weather )(.+), (\\w+)$").unwrap(); } 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) => server.send_privmsg(target, &format!("{}: {}", source_nick, &test_get_weather(api_key))).unwrap(), + Some(api_key) => { + let mut location_city = String::new(); + let mut location_area = String::new(); + match WEATHER_REGEX.captures(&inner_message) { + Some(capture) => {match capture.at(2) { + Some(city_match) => location_city = String::from(city_match), + None => (), + }; + match capture.at(3) { + Some(area_match) => location_area = String::from(area_match), + None => (), + }; + server.send_privmsg(target, &format!("{}: {}", source_nick, &test_get_weather(api_key, &location_city, &location_area))).unwrap(); + }, + None => (), + } + + }, } } } else if message_body.contains("bot-quit") { @@ -56,11 +76,10 @@ fn handle_privmsg(target: &String, message_body: &String, message_obj: &Message, } } -fn test_get_weather(api_key: &String) -> String { +fn test_get_weather(api_key: &String, location_city: &String, location_area: &String) -> 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); + let url = &format!("{}{}/{}/q/{}/{}.json", WEATHER_API_BASE, api_key, data_type, location_area, location_city); println!("Attempting to fetch {}", url); let mut response = match client.get(url).send() { Ok(response) => response,