diff options
Diffstat (limited to 'tw')
-rw-r--r-- | tw/events.rs | 44 | ||||
-rw-r--r-- | tw/mod.rs | 157 |
2 files changed, 46 insertions, 155 deletions
diff --git a/tw/events.rs b/tw/events.rs new file mode 100644 index 0000000..0541b0a --- /dev/null +++ b/tw/events.rs @@ -0,0 +1,44 @@ +extern crate serde_json; + +pub enum Event { + Deleted { user_id: String, twete_id: String }, + RT_RT { user_id: String, twete_id: String }, + Fav_RT { user_id: String, twete_id: String }, + Fav { user_id: String, twete_id: String }, + Unfav { user_id: String, twete_id: String }, + Followed { user_id: String }, + Unfollowed { user_id: String } +} + +impl Event { + pub fn from_json(structure: serde_json::Map<String, serde_json::Value>) -> Option<Event> { + match &structure["event"].as_str().unwrap() { + &"follow" => Some(Event::Followed { + user_id: structure["source"]["id_str"].as_str().unwrap().to_owned() + }), + &"unfollow" => Some(Event::Unfollowed { + user_id: structure["source"]["id_str"].as_str().unwrap().to_owned() + }), + &"favorite" => Some(Event::Fav { + user_id: structure["source"]["id_str"].as_str().unwrap().to_owned(), + twete_id: structure["target_object"]["id_str"].as_str().unwrap().to_owned() + }), + &"unfavorite" => Some(Event::Unfav { + user_id: structure["source"]["id_str"].as_str().unwrap().to_owned(), + twete_id: structure["target_object"]["id_str"].as_str().unwrap().to_owned() + }), + &"favorited_retweet" => Some(Event::Fav_RT { + user_id: structure["source"]["id_str"].as_str().unwrap().to_owned(), + twete_id: structure["target_object"]["id_str"].as_str().unwrap().to_owned() + }), + &"retweeted_retweet" => Some(Event::RT_RT { + user_id: structure["source"]["id_str"].as_str().unwrap().to_owned(), + twete_id: structure["target_object"]["id_str"].as_str().unwrap().to_owned() + }), +// &"blocked" => Blocked { }, +// &"unblocked" => Unblocked { }, +// &"quoted_tweet" => ???, + e => { println!("unrecognized event: {}", e); None } + } + } +} @@ -11,6 +11,8 @@ use std::io::Write; use std::fs::OpenOptions; +pub mod events; + #[derive(Debug, Serialize, Deserialize)] pub struct User { pub id: String, @@ -28,161 +30,6 @@ impl Default for User { } } -pub mod events { - extern crate termion; - use self::termion::color; - - extern crate serde_json; - - pub struct Deleted { - user_id: String, - twete_id: String - } - - pub struct RT_RT { - user_id: String, - twete_id: String - } - - pub struct Fav_RT { - user_id: String, - twete_id: String - } - - pub struct Fav { - user_id: String, - twete_id: String - } - - pub struct Unfav { - user_id: String, - twete_id: String - } - - pub struct Followed { - user_id: String - } - - pub struct Unfollowed { - user_id: String - } - - impl Event for Deleted { - fn render(self: Box<Self>, _tweeter: &::tw::TwitterCache) { } - } - impl Event for RT_RT { - fn render(self: Box<Self>, tweeter: &::tw::TwitterCache) { - println!("---------------------------------"); - { - let user = tweeter.retrieve_user(&self.user_id).unwrap(); - println!(" +rt_rt : {} (@{})", user.name, user.handle); - } - { - ::render_twete(&self.twete_id, tweeter); - } - println!(""); - } - } - impl Event for Fav_RT { - fn render(self: Box<Self>, tweeter: &::tw::TwitterCache) { - println!("---------------------------------"); - { - let user = tweeter.retrieve_user(&self.user_id).unwrap(); - println!(" +rt_fav : {} (@{})", user.name, user.handle); - } - { - ::render_twete(&self.twete_id, tweeter); - } - println!(""); - } - } - impl Event for Fav { - fn render(self: Box<Self>, tweeter: &::tw::TwitterCache) { - println!("---------------------------------"); - { - let user = tweeter.retrieve_user(&self.user_id).unwrap(); - println!("{} +fav : {} (@{}){}", color::Fg(color::Yellow), user.name, user.handle, color::Fg(color::Reset)); - } - { - ::render_twete(&self.twete_id, tweeter); - } - println!(""); - } - } - impl Event for Unfav { - fn render(self: Box<Self>, tweeter: &::tw::TwitterCache) { - println!("---------------------------------"); - { - let user = tweeter.retrieve_user(&self.user_id).unwrap(); - println!("{} -fav : {} (@{}){}", color::Fg(color::Yellow), user.name, user.handle, color::Fg(color::Reset)); - } - { - ::render_twete(&self.twete_id, tweeter); - } - println!(""); - } - } - impl Event for Followed { - fn render(self: Box<Self>, tweeter: &::tw::TwitterCache) { - let user = tweeter.retrieve_user(&self.user_id).unwrap(); - println!("---------------------------------"); - println!(" +fl : {} (@{})", user.name, user.handle); - println!(""); - } - } - impl Event for Unfollowed { - fn render(self: Box<Self>, tweeter: &::tw::TwitterCache) { - let user = tweeter.retrieve_user(&self.user_id).unwrap(); - println!("---------------------------------"); - println!(" -fl : {} (@{})", user.name, user.handle); - println!(""); - } - } - - /* - impl Event for Blocked { - - } - */ - - pub trait Event { - fn render(self: Box<Self>, tweeter: &::tw::TwitterCache); - } - - impl Event { - pub fn from_json(structure: serde_json::Map<String, serde_json::Value>) -> Option<Box<Event>> { - match &structure["event"].as_str().unwrap() { - &"follow" => Some(Box::new(Followed { - user_id: structure["source"]["id_str"].as_str().unwrap().to_owned() - })), - &"unfollow" => Some(Box::new(Unfollowed { - user_id: structure["source"]["id_str"].as_str().unwrap().to_owned() - })), - &"favorite" => Some(Box::new(Fav { - user_id: structure["source"]["id_str"].as_str().unwrap().to_owned(), - twete_id: structure["target_object"]["id_str"].as_str().unwrap().to_owned() - })), - &"unfavorite" => Some(Box::new(Unfav { - user_id: structure["source"]["id_str"].as_str().unwrap().to_owned(), - twete_id: structure["target_object"]["id_str"].as_str().unwrap().to_owned() - })), - &"favorited_retweet" => Some(Box::new(Fav_RT { - user_id: structure["source"]["id_str"].as_str().unwrap().to_owned(), - twete_id: structure["target_object"]["id_str"].as_str().unwrap().to_owned() - })), - &"retweeted_retweet" => Some(Box::new(RT_RT { - user_id: structure["source"]["id_str"].as_str().unwrap().to_owned(), - twete_id: structure["target_object"]["id_str"].as_str().unwrap().to_owned() - })), -// &"blocked" => Blocked { }, -// &"unblocked" => Unblocked { }, -// &"quoted_tweet" => ???, - e => { println!("unrecognized event: {}", e); None } - } - } - } -} - impl User { pub fn from_json(json: serde_json::Value) -> Option<User> { if let serde_json::Value::Object(json_map) = json { |