From 609fa1ab6351dd58a8c9cd245aeec0eaf647c9e9 Mon Sep 17 00:00:00 2001 From: jan Date: Thu, 7 Apr 2016 18:16:03 +0200 Subject: feature TopAnime hinzugefuegt, wird automatisch geupdated diff --git a/Cargo.toml b/Cargo.toml index 8ce7c59..65fd153 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,5 @@ authors = ["jan "] [dependencies] telegram-bot = "0.4" time = "0.1" +regex = "0.1" +rustc-serialize = "0.3" diff --git a/src/feature/mod.rs b/src/feature/mod.rs index a52bb2c..b57c179 100644 --- a/src/feature/mod.rs +++ b/src/feature/mod.rs @@ -2,9 +2,11 @@ use std::boxed::Box; extern crate telegram_bot; use telegram_bot::{Api, Message}; +extern crate rustc_serialize; pub mod jisoku; pub mod tasterank; +pub mod topanime; pub enum FeatureResult { Handled, @@ -18,5 +20,7 @@ pub trait Feature { } pub fn init() -> Vec> { - vec![Box::new(tasterank::Tasterank::new()), Box::new(jisoku::Jisoku::new())] + vec![Box::new(tasterank::Tasterank::new()), + Box::new(jisoku::Jisoku::new()), + Box::new(topanime::TopAnime::new())] } diff --git a/src/feature/topanime.rs b/src/feature/topanime.rs new file mode 100644 index 0000000..cccc976 --- /dev/null +++ b/src/feature/topanime.rs @@ -0,0 +1,119 @@ +use std::io; +use std::io::prelude::*; +use std::fs::File; +use std::usize; + +extern crate telegram_bot; +use telegram_bot::{Api, Message, MessageType}; +extern crate time; +extern crate regex; +use self::regex::Regex; +use super::rustc_serialize::json; + +use feature::FeatureResult; +use feature::Feature; + +#[derive(RustcDecodable)] +pub struct Anime { + name: String, + score: f64, +} + +impl Anime { + pub fn new(name: &str, score: f64) -> Anime { + Anime { + name: name.to_owned(), + score: score, + } + } +} + +pub struct TopAnime { + animes: Vec, + last_access: f64, +} + +impl Feature for TopAnime { + fn name(&self) -> &'static str { + "Tasterank" + } + fn init(&mut self) {} + fn handle(&mut self, a: Api, m: Message) -> Result { + if let MessageType::Text(s) = m.msg { + let re = Regex::new(r"(?i)luggas top ([0-9]{1,3}) anime").unwrap(); + let cap = re.captures(&s); + let s_num = match cap { + Some(s) => s.at(1), + None => return Ok(FeatureResult::Skip), + }; + let s_num = match s_num { + Some(s) => s, + None => return Ok(FeatureResult::Skip), + }; + + // parse to int + let num = match s_num.parse::() { + Ok(n) => n, + Err(e) => { + println!("{}", e); + return Ok(FeatureResult::Skip); + } + }; + let start = time::precise_time_ns(); + if let Err(e) = self.update_animes() { + println!("{}", e); + return Ok(FeatureResult::Skip); + } + println!("(benchm) updating animes: {}ms", + (time::precise_time_ns() - start) / 1000000); + + let mut msg = String::new(); + for i in 0..num { + let i = i as usize; + if self.animes.len() <= i { + break; + } + msg.push_str(&format!("{}. {} ({}%)\n", + i + 1, + self.animes[i].name, + self.animes[i].score)); + } + println!("(benchm) anime before send: {}ms", + (time::precise_time_ns() - start) / 1000000); + if let Err(e) = a.send_message(m.chat.id(), msg, None, None, Some(m.message_id), None) { + println!("{}", e); + } + println!("(benchm) anime: {}ms", + (time::precise_time_ns() - start) / 1000000); + return Ok(FeatureResult::Handled); + } + Ok(FeatureResult::Skip) + } +} + +impl TopAnime { + pub fn new() -> TopAnime { + let mut t = TopAnime { + animes: vec![], + last_access: 0f64, + }; + t.init(); + t + } + fn update_animes(&mut self) -> Result<(), io::Error> { + let now = time::precise_time_s(); + if now - self.last_access < 3600.0f64 { + return Ok(()); + } + let mut f = try!(File::open("data/animescore.json")); + let mut buf = String::new(); + try!(f.read_to_string(&mut buf)); + let v: Vec = match json::decode(&buf) { + Ok(v) => v, + Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidData, format!("{}", e))), + }; + self.animes = v; + self.last_access = now; + Ok(()) + } +} diff --git a/src/main.rs b/src/main.rs index ef5c8e8..0880467 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ extern crate telegram_bot; use telegram_bot::{Api, ListeningMethod, ListeningAction}; +extern crate rustc_serialize; +use rustc_serialize::json; mod feature; use feature::{Feature, FeatureResult}; -- cgit v0.10.1