aboutsummaryrefslogtreecommitdiff
path: root/commands
diff options
context:
space:
mode:
authorAndy Wortman <ixineeringeverywhere@gmail.com>2017-10-01 20:55:15 -0700
committerAndy Wortman <ixineeringeverywhere@gmail.com>2017-10-01 20:55:15 -0700
commite6ebf2c99a70bd5ee4e8d07097e6b128c3630714 (patch)
tree476947a6f2937737bc69ca073a2519bd9f15b1fe /commands
parentaf67981a2a1c28b3b5598f74d48bfd3a7490c91a (diff)
extract commands and twitter model into modules
Diffstat (limited to 'commands')
-rw-r--r--commands/del.rs0
-rw-r--r--commands/fav.rs35
-rw-r--r--commands/look_up.rs32
-rw-r--r--commands/mod.rs46
-rw-r--r--commands/quit.rs18
-rw-r--r--commands/show_cache.rs31
-rw-r--r--commands/twete.rs184
-rw-r--r--commands/view.rs20
8 files changed, 366 insertions, 0 deletions
diff --git a/commands/del.rs b/commands/del.rs
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/commands/del.rs
diff --git a/commands/fav.rs b/commands/fav.rs
new file mode 100644
index 0000000..3e2b00d
--- /dev/null
+++ b/commands/fav.rs
@@ -0,0 +1,35 @@
+use tw;
+use ::Queryer;
+
+use commands::Command;
+
+use std::str::FromStr;
+
+static FAV_TWEET_URL: &str = "https://api.twitter.com/1.1/favorites/create.json";
+static UNFAV_TWEET_URL: &str = "https://api.twitter.com/1.1/favorites/destroy.json";
+
+pub static UNFAV: Command = Command {
+ keyword: "unfav",
+ params: 1,
+ exec: unfav
+};
+
+fn unfav(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) {
+ // TODO handle this unwrap
+ let inner_twid = u64::from_str(&line).unwrap();
+ let twete = tweeter.tweet_by_innerid(inner_twid).unwrap();
+ queryer.do_api_post(&format!("{}?id={}", UNFAV_TWEET_URL, twete.id));
+}
+
+pub static FAV: Command = Command {
+ keyword: "fav",
+ params: 1,
+ exec: fav
+};
+
+fn fav(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) {
+ // TODO handle this unwrap
+ let inner_twid = u64::from_str(&line).unwrap();
+ let twete = tweeter.tweet_by_innerid(inner_twid).unwrap();
+ queryer.do_api_post(&format!("{}?id={}", FAV_TWEET_URL, twete.id));
+}
diff --git a/commands/look_up.rs b/commands/look_up.rs
new file mode 100644
index 0000000..d04f984
--- /dev/null
+++ b/commands/look_up.rs
@@ -0,0 +1,32 @@
+use tw;
+use ::Queryer;
+
+use commands::Command;
+
+pub static LOOK_UP_USER: Command = Command {
+ keyword: "look_up_user",
+ params: 1,
+ exec: look_up_user
+};
+
+fn look_up_user(line: String, tweeter: &mut tw::TwitterCache, mut queryer: &mut Queryer) {
+ if let Some(user) = tweeter.fetch_user(&line, &mut queryer) {
+ println!("{:?}", user);
+ } else {
+// println!("Couldn't retrieve {}", userid);
+ }
+}
+
+pub static LOOK_UP_TWEET: Command = Command {
+ keyword: "look_up_tweet",
+ params: 1,
+ exec: look_up_tweet
+};
+
+fn look_up_tweet(line: String, tweeter: &mut tw::TwitterCache, mut queryer: &mut Queryer) {
+ if let Some(tweet) = tweeter.fetch_tweet(&line, &mut queryer) {
+ println!("{:?}", tweet);
+ } else {
+// println!("Couldn't retrieve {}", tweetid);
+ }
+}
diff --git a/commands/mod.rs b/commands/mod.rs
new file mode 100644
index 0000000..fc66bec
--- /dev/null
+++ b/commands/mod.rs
@@ -0,0 +1,46 @@
+use tw;
+use ::Queryer;
+
+pub struct Command {
+ pub keyword: &'static str,
+ pub params: u8,
+ pub exec: fn(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer)
+}
+
+mod show_cache;
+mod twete;
+mod look_up;
+mod view;
+mod quit;
+mod fav;
+
+pub static COMMANDS: &[&Command] = &[
+ &show_cache::SHOW_CACHE,
+ &quit::QUIT,
+ &look_up::LOOK_UP_USER,
+ &look_up::LOOK_UP_TWEET,
+ &view::VIEW,
+ &fav::UNFAV,
+ &fav::FAV,
+ &twete::DEL,
+ &twete::TWETE,
+ &twete::QUOTE,
+ &twete::RETWETE,
+ &twete::REP,
+ &twete::THREAD
+ /*
+ &QUIT,
+ &LOOK_UP_USER,
+ &LOOK_UP_TWEET,
+ &VIEW,
+ &UNFAV,
+ &FAV,
+ &DEL,
+ &TWETE,
+ &QUOTE,
+ &RETWETE,
+ &REP,
+ &THREAD
+ ];
+ */
+];
diff --git a/commands/quit.rs b/commands/quit.rs
new file mode 100644
index 0000000..982c48f
--- /dev/null
+++ b/commands/quit.rs
@@ -0,0 +1,18 @@
+use tw;
+use ::Queryer;
+
+use commands::Command;
+
+use std::process::exit;
+
+pub static QUIT: Command = Command {
+ keyword: "q",
+ params: 0,
+ exec: quit
+};
+
+fn quit(_line: String, tweeter: &mut tw::TwitterCache, _queryer: &mut Queryer) {
+ println!("Bye bye!");
+ tweeter.store_cache();
+ exit(0);
+}
diff --git a/commands/show_cache.rs b/commands/show_cache.rs
new file mode 100644
index 0000000..3c31697
--- /dev/null
+++ b/commands/show_cache.rs
@@ -0,0 +1,31 @@
+use tw;
+use ::Queryer;
+
+use commands::Command;
+
+pub static SHOW_CACHE: Command = Command {
+ keyword: "show_cache",
+ params: 0,
+ exec: show_cache
+};
+
+fn show_cache(line: String, tweeter: &mut tw::TwitterCache, mut queryer: &mut Queryer) {
+ println!("----* USERS *----");
+ for (uid, user) in &tweeter.users {
+ println!("User: {} -> {:?}", uid, user);
+ }
+ println!("----* TWEETS *----");
+ for (tid, tweet) in &tweeter.tweets {
+ println!("Tweet: {} -> {:?}", tid, tweet);
+ }
+ println!("----* FOLLOWERS *----");
+ for uid in &tweeter.followers.clone() {
+ let user_res = tweeter.fetch_user(uid, &mut queryer);
+ match user_res {
+ Some(user) => {
+ println!("Follower: {} - {:?}", uid, user);
+ }
+ None => { println!(" ..."); }
+ }
+ }
+}
diff --git a/commands/twete.rs b/commands/twete.rs
new file mode 100644
index 0000000..ecc3f98
--- /dev/null
+++ b/commands/twete.rs
@@ -0,0 +1,184 @@
+use tw;
+use ::Queryer;
+
+use commands::Command;
+
+use std::str::FromStr;
+
+static DEL_TWEET_URL: &str = "https://api.twitter.com/1.1/statuses/destroy";
+static RT_TWEET_URL: &str = "https://api.twitter.com/1.1/statuses/retweet";
+static CREATE_TWEET_URL: &str = "https://api.twitter.com/1.1/statuses/update.json";
+
+pub static DEL: Command = Command {
+ keyword: "del",
+ params: 1,
+ exec: del
+};
+
+fn del(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) {
+ let inner_twid = u64::from_str(&line).unwrap();
+ let twete = tweeter.tweet_by_innerid(inner_twid).unwrap();
+ queryer.do_api_post(&format!("{}/{}.json", DEL_TWEET_URL, twete.id));
+}
+
+pub static TWETE: Command = Command {
+ keyword: "t",
+ params: 1,
+ exec: twete
+};
+
+fn twete(line: String, _tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) {
+ let text = line.trim();
+ let substituted = ::url_encode(text);
+ println!("msg len: {}", text.len());
+ println!("excessively long? {}", text.len() > 140);
+ if text.len() > 140 {
+ queryer.do_api_post(&format!("{}?status={}", CREATE_TWEET_URL, substituted));
+ } else {
+ queryer.do_api_post(&format!("{}?status={}&weighted_character_count=true", CREATE_TWEET_URL, substituted));
+ }
+// println!("{}", &format!("{}?status={}", CREATE_TWEET_URL, substituted));
+}
+
+pub static THREAD: Command = Command {
+ keyword: "thread",
+ params: 2,
+ exec: thread
+};
+
+fn thread(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) {
+ let mut text: String = line.trim().to_string();
+ if let Some(id_end_idx) = text.find(" ") {
+ let reply_bare = text.split_off(id_end_idx + 1);
+ let reply = reply_bare.trim();
+ let id_str = text.trim();
+ if reply.len() > 0 {
+ if let Some(inner_twid) = u64::from_str(&id_str).ok() {
+ if let Some(twete) = tweeter.tweet_by_innerid(inner_twid) {
+ let handle = &tweeter.retrieve_user(&twete.author_id).unwrap().handle;
+ // TODO: definitely breaks if you change your handle right now
+ if handle == &tweeter.current_user.handle {
+ let substituted = ::url_encode(reply);
+ queryer.do_api_post(&format!("{}?status={}&in_reply_to_status_id={}", CREATE_TWEET_URL, substituted, twete.id));
+ } else {
+ println!("you can only thread your own tweets");
+ // ask if it should .@ instead?
+ }
+ let substituted = ::url_encode(reply);
+ queryer.do_api_post(&format!("{}?status={}&in_reply_to_status_id={}", CREATE_TWEET_URL, substituted, twete.id));
+ }
+ }
+ } else {
+ println!("thread <id> your sik reply");
+ }
+ } else {
+ println!("thread <id> your sik reply");
+ }
+}
+
+pub static REP: Command = Command {
+ keyword: "rep",
+ params: 2,
+ exec: rep
+};
+
+fn rep(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) {
+ let mut text: String = line.trim().to_string();
+ if let Some(id_end_idx) = text.find(" ") {
+ let reply_bare = text.split_off(id_end_idx + 1);
+ let reply = reply_bare.trim();
+ let id_str = text.trim();
+ if reply.len() > 0 {
+ if let Some(inner_twid) = u64::from_str(&id_str).ok() {
+ if let Some(twete) = tweeter.tweet_by_innerid(inner_twid) {
+ // get handles to reply to...
+ let author_handle = tweeter.retrieve_user(&twete.author_id).unwrap().handle.to_owned();
+ let mut ats: Vec<String> = twete.get_mentions().into_iter().map(|x| x.to_owned()).collect(); //std::collections::HashSet::new();
+ /*
+ for handle in twete.get_mentions() {
+ ats.insert(handle);
+ }
+ */
+ ats.remove_item(&author_handle);
+ ats.insert(0, author_handle);
+ // no idea why i have to .to_owned() here --v-- what about twete.rt_tweet is a move?
+ if let Some(rt_tweet) = twete.rt_tweet.to_owned().and_then(|id| tweeter.retrieve_tweet(&id)) {
+ let rt_author_handle = tweeter.retrieve_user(&rt_tweet.author_id).unwrap().handle.to_owned();
+ ats.remove_item(&rt_author_handle);
+ ats.insert(1, rt_author_handle);
+ }
+ if let Some(qt_tweet) = twete.quoted_tweet_id.to_owned().and_then(|id| tweeter.retrieve_tweet(&id)) {
+ let qt_author_handle = tweeter.retrieve_user(&qt_tweet.author_id).unwrap().handle.to_owned();
+ ats.remove_item(&qt_author_handle);
+ ats.insert(1, qt_author_handle);
+ }
+ //let ats_vec: Vec<&str> = ats.into_iter().collect();
+ //let full_reply = format!("{} {}", ats_vec.join(" "), reply);
+ let decorated_ats: Vec<String> = ats.into_iter().map(|x| format!("@{}", x)).collect();
+ let full_reply = format!("{} {}", decorated_ats.join(" "), reply);
+ let substituted = ::url_encode(&full_reply);
+// println!("{}", (&format!("{}?status={}&in_reply_to_status_id={}", CREATE_TWEET_URL, substituted, twete.id)));
+ queryer.do_api_post(&format!("{}?status={}&in_reply_to_status_id={}", CREATE_TWEET_URL, substituted, twete.id));
+ }
+ }
+ } else {
+ println!("rep <id> your sik reply");
+ }
+ } else {
+ println!("rep <id> your sik reply");
+ }
+}
+
+pub static QUOTE: Command = Command {
+ keyword: "qt",
+ params: 2,
+ exec: quote
+};
+
+fn quote(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) {
+ let mut text: String = line.trim().to_string();
+ if let Some(id_end_idx) = text.find(" ") {
+ let reply_bare = text.split_off(id_end_idx + 1);
+ let reply = reply_bare.trim();
+ let id_str = text.trim();
+ if reply.len() > 0 {
+ if let Some(inner_twid) = u64::from_str(&id_str).ok() {
+ if let Some(twete) = tweeter.tweet_by_innerid(inner_twid) {
+ let substituted = ::url_encode(reply);
+ let attachment_url = ::url_encode(
+ &format!(
+ "https://www.twitter.com/{}/status/{}",
+ tweeter.retrieve_user(&twete.author_id).unwrap().handle,
+ twete.id
+ )
+ );
+ println!("{}", substituted);
+ queryer.do_api_post(
+ &format!("{}?status={}&attachment_url={}",
+ CREATE_TWEET_URL,
+ substituted,
+ attachment_url
+ )
+ );
+ }
+ }
+ } else {
+ println!("rep <id> your sik reply");
+ }
+ } else {
+ println!("rep <id> your sik reply");
+ }
+}
+
+pub static RETWETE: Command = Command {
+ keyword: "rt",
+ params: 1,
+ exec: retwete
+};
+
+fn retwete(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) {
+ let inner_twid = u64::from_str(&line).unwrap();
+ let twete = tweeter.tweet_by_innerid(inner_twid).unwrap();
+ queryer.do_api_post(&format!("{}/{}.json", RT_TWEET_URL, twete.id));
+}
+
diff --git a/commands/view.rs b/commands/view.rs
new file mode 100644
index 0000000..1470f90
--- /dev/null
+++ b/commands/view.rs
@@ -0,0 +1,20 @@
+use tw;
+use ::Queryer;
+
+use commands::Command;
+
+use std::str::FromStr;
+
+pub static VIEW: Command = Command {
+ keyword: "view",
+ params: 1,
+ exec: view
+};
+
+fn view(line: String, tweeter: &mut tw::TwitterCache, _queryer: &mut Queryer) {
+ // TODO handle this unwrap
+ let inner_twid = u64::from_str(&line).unwrap();
+ let twete = tweeter.tweet_by_innerid(inner_twid).unwrap();
+ ::render_twete(&twete.id, tweeter);
+ println!("link: https://twitter.com/i/web/status/{}", twete.id);
+}