Add stocks command to get stock data from Google.
This commit is contained in:
parent
606db75101
commit
0529bcebc6
1 changed files with 41 additions and 0 deletions
41
src/main.rs
41
src/main.rs
|
@ -12,6 +12,7 @@ use regex::Regex;
|
||||||
use rustc_serialize::json::{self};
|
use rustc_serialize::json::{self};
|
||||||
|
|
||||||
static WEATHER_API_BASE: &'static str = "http://api.wunderground.com/api/";
|
static WEATHER_API_BASE: &'static str = "http://api.wunderground.com/api/";
|
||||||
|
static STOCKS_API_BASE: &'static str = "http://finance.google.com/finance/info?client=rust-bot&q=";
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
|
@ -98,6 +99,20 @@ fn handle_privmsg(target: &String, message_body: &String, message_obj: &Message,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if inner_message.starts_with("stock") {
|
||||||
|
// Stock regular expression
|
||||||
|
lazy_static! { static ref STOCK_REGEX: Regex = Regex::new("(stock )(.+)$").unwrap(); }
|
||||||
|
let mut symbol = String::new();
|
||||||
|
match STOCK_REGEX.captures(&inner_message) {
|
||||||
|
Some(capture) => {
|
||||||
|
match capture.at(2) {
|
||||||
|
Some(symbol_match) => symbol = String::from(symbol_match),
|
||||||
|
None => (),
|
||||||
|
}
|
||||||
|
server.send_privmsg(target, &format!("{}: {}", source_nick, &get_stocks(&symbol))).unwrap();
|
||||||
|
},
|
||||||
|
None => (),
|
||||||
|
}
|
||||||
} else if inner_message.starts_with("bot-quit") {
|
} else if inner_message.starts_with("bot-quit") {
|
||||||
if server.config().is_owner(source_nick) {
|
if server.config().is_owner(source_nick) {
|
||||||
std::process::exit(0);
|
std::process::exit(0);
|
||||||
|
@ -168,3 +183,29 @@ fn get_weather_locid(api_key: &String, locid_type: &String, locid: &String) -> S
|
||||||
}
|
}
|
||||||
reply_message
|
reply_message
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_stocks(symbol: &String) -> String {
|
||||||
|
let client = Client::new();
|
||||||
|
let url = &format!("{}{}", STOCKS_API_BASE, symbol);
|
||||||
|
println!("Attempting to fetch {}", url);
|
||||||
|
let mut response = match client.get(url).send() {
|
||||||
|
Ok(response) => response,
|
||||||
|
Err(_) => panic!("Error fetching JSON!"),
|
||||||
|
};
|
||||||
|
let mut response_body = String::new();
|
||||||
|
match response.read_to_string(&mut response_body) {
|
||||||
|
Ok(_) => (),
|
||||||
|
Err(_) => panic!("Error reading to buffer"),
|
||||||
|
};
|
||||||
|
let mut reply_message = String::new();
|
||||||
|
if let Ok(parsed_response) = json::Json::from_str(&response_body[4..]) {
|
||||||
|
let ref response_object = parsed_response.as_array().unwrap()[0];
|
||||||
|
println!("response_object: {}", response_object);
|
||||||
|
let price = response_object.find("l").unwrap().as_string().unwrap();
|
||||||
|
let exchange = response_object.find("e").unwrap().as_string().unwrap();
|
||||||
|
let change = response_object.find("c").unwrap().as_string().unwrap();
|
||||||
|
let last_updated = response_object.find("lt").unwrap().as_string().unwrap();
|
||||||
|
reply_message = format!("{} on exchange {}: {} ({}), last updated {}", symbol, exchange, price, change, last_updated);
|
||||||
|
}
|
||||||
|
reply_message
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue