1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//! This crate provides an executable tw-rs, as well as several functions to return
//! tweets and to tweet. 
//!
//! ```c
//! $ tw send "tweeting from the command line :)"
//! ```
//!
//! If you get stuck:
//!
//! ```c
//! tw help
//! ```
#![feature(test)]
#[macro_use] 

extern crate nom;
extern crate oauth_client_fix as oauth_client;
extern crate core;
extern crate base64;

use std::collections::HashMap;
use oauth_client::Token;
use nom::IResult;
use types::Tweet;

pub mod parse;
pub mod types;
pub mod test;

/// Reads credentials from a string, i.e. gets them from a file.
///
/// # Examples
///
/// Put the following into a file:
///
/// ```c
/// api-key: API_KEY_HERE
/// api-sec: API_SECRET_HERE
/// tok: OAUTH_TOKEN_HERE
/// tok-sec: TOKEN_SECRET_HERE
/// ```
pub fn get_credentials(contents: &str) -> (Token, Token) {
    let mut iter = contents.split_whitespace();
    iter.next();
    let api_key = iter.next().expect("api key not found");
    iter.next();
    let api_sec = iter.next().expect("api secret not found");
    iter.next();
    let tok = iter.next().expect("token not found");
    iter.next();
    let tok_sec = iter.next().expect("token secret");
    let key = oauth_client::Token::new(api_key, api_sec);
    let token = oauth_client::Token::new(tok,tok_sec);
    (key, token)
}

/// Display the raw JSON of a response; useful for debugging.
pub fn profile_raw(api_key: &Token, token: &Token) {
    let mut param = HashMap::new();
    let _ = param.insert("screen_name".into(), "".into());
    let _ = param.insert("count".into(), "20".into()); // TODO accept number of tweets to get
    let bytes_raw = oauth_client::get(api::USER_PROFILE, api_key, Some(token), Some(&param)).unwrap();
    let resp = String::from_utf8(bytes_raw).unwrap();
    println!("response:\n{}", resp);
}


/// Return profile for a given user. 
pub fn get_profile(screen_name: &str, num: u8, api_key: &Token, token: &Token) -> Option<Vec<Tweet>> {
    let mut param = HashMap::new();
    let num_str = num.to_string();
    let _ = param.insert("screen_name".into(), screen_name.into());
    let _ = param.insert("count".into(), num_str.into()); 
    let bytes_raw = oauth_client::get(api::USER_PROFILE, api_key, Some(token), Some(&param)).unwrap();
    // convert vector of u8's to &[u8] (array slice)
    let bytes_slice = bytes_raw.as_slice();
    // parse as a Result<Vec<Tweet>>
    parse::parse_tweets_string(bytes_slice)
}


/// Display profile for a given user. Takes screen name and number of tweets to return as
/// parameters. Boolean argument is whether to print out user ids. 
///
/// Note that Twitter's API allow for a maximum of 3200 tweets at a time by this method. 
///
/// # Examples
/// 
/// ```ignore
/// print_profile(realDonaldTrump, 100, false, API_KEY, TOKEN);
/// ```
pub fn print_profile(screen_name: &str, num: u8, show_ids: bool, api_key: &Token, token: &Token) {
    let mut param = HashMap::new();
    let num_str = num.to_string();
    let _ = param.insert("screen_name".into(), screen_name.into());
    let _ = param.insert("count".into(), num_str.into()); // TODO accept number of tweets to get
    let bytes_raw = oauth_client::get(api::USER_PROFILE, api_key, Some(token), Some(&param)).unwrap();
    // convert vector of u8's to &[u8] (array slice)
    let bytes_slice = bytes_raw.as_slice();
    let parsed_maybe = parse::parse_tweets(bytes_slice);
    if let IResult::Done(_,parsed) = parsed_maybe {
        for to_show in parsed {
            if show_ids {
                println!("{:?}", to_show);
            }
            else
            {
                println!("{}", to_show);
            }
        }
    }
    else {
        println!("Parse error when attempting to read tweet data.");
    }
}

/// Send a tweet
///
/// # Examples
///
/// ```ignore
/// tweet("having a good day :)", API_KEY, TOKEN);
/// ```
pub fn tweet(sent_text: &str, api_key: &Token, token: &Token) {
    let mut param = HashMap::new();
    let _ = param.insert("status".into(), sent_text.into());
    let bytes_raw = oauth_client::post(api::STATUS_UPDATE, api_key, Some(token), Some(&param)).unwrap();
    // convert vector of u8's to &[u8] (array slice)
    let bytes_slice = bytes_raw.as_slice();
    let parsed_maybe = parse::parse_tweets(bytes_slice);
    if let IResult::Done(_,parsed) = parsed_maybe {
        println!("{}", parsed[0]);
    }
    else {
        println!("Parse error when attempting to read tweet data.");
    }
}

/// Reply to a tweet
///
/// # Examples
///
/// ```ignore
/// reply("@friend that sounds like a good idea!", 844370958781579265, API_KEY, TOKEN);
/// ```
pub fn reply(sent_text: &str, reply_to: u64, api_key: &Token, token: &Token) {
    let mut param = HashMap::new();
    let reply_to_str = reply_to.to_string();
    let _ = param.insert("status".into(), sent_text.into());
    let _ = param.insert("in_reply_to_status_id".into(), reply_to_str.into());
    let bytes_raw = oauth_client::post(api::STATUS_UPDATE, api_key, Some(token), Some(&param)).unwrap();
    // convert vector of u8's to &[u8] (array slice)
    let bytes_slice = bytes_raw.as_slice();
    let parsed_maybe = parse::parse_tweets(bytes_slice);
    if let IResult::Done(_,parsed) = parsed_maybe {
        println!("{}", parsed[0]);
    }
    else {
        println!("Parse error when attempting to read tweet data.");
    }
}

/// Follow a user given their screen name
pub fn follow(screen_name: &str, api_key: &Token, token: &Token) {
    let mut param = HashMap::new();
    let _ = param.insert("screen_name".into(), screen_name.into());
    let _ = oauth_client::post(api::FOLLOW, api_key, Some(token), Some(&param)).unwrap();
    println!("{} followed succesfully!", screen_name);
}

/// Unfollow a user given their screen name
pub fn unfollow(screen_name: &str, api_key: &Token, token: &Token) {
    let mut param = HashMap::new();
    let _ = param.insert("screen_name".into(), screen_name.into());
    let _ = oauth_client::post(api::UNFOLLOW, api_key, Some(token), Some(&param)).unwrap();
    println!("{} unfollowed succesfully!", screen_name);
}
/// Display timeline. Takes number of tweets to return as
/// a parameter. Second argument is whether to display the id of the tweets.
///
/// Note that Twitter's API allow for a maximum of 3200 tweets at a time by this method. 
///
/// # Examples
/// 
/// ```ignore
/// print_timeline(5, false, API_KEY, TOKEN);
/// ```
pub fn print_timeline(num: u8, show_ids:bool, api_key: &Token, token: &Token) {
    let num_str = num.to_string();
    let mut param = HashMap::new();
    let _ = param.insert("count".into(), num_str.into()); 
    let bytes_raw = oauth_client::get(api::TIMELINE, api_key, Some(token), Some(&param)).unwrap();
    let bytes_slice = bytes_raw.as_slice();
    let parsed_maybe = parse::parse_tweets(bytes_slice);
    if let IResult::Done(_,parsed) = parsed_maybe {
        for to_show in parsed {
            if show_ids {
                println!("{:?}", to_show);
            }
            else
            {
                println!("{}", to_show);
            }
        }
    }
    else {
        println!("Parse error when attempting to read tweet data.");
    }
}

/// Delete a tweet by its id
pub fn delete_tweet(tweet_id: u64, api_key: &Token, token: &Token) {
	let tweet_id_str = tweet_id.to_string();
	let url = api::DELETE.to_string() + tweet_id_str.as_str() + ".json";
	let _ = oauth_client::post(url.as_str(), api_key, Some(token), None).unwrap();
	// we don't really care about the return value - TODO better message
	println!("Tweet deleted successfully!");
}
	
/// Rewteet a tweet by its id
pub fn retweet(tweet_id: u64, api_key: &Token, token: &Token) {
	let tweet_id_str = tweet_id.to_string();
	let url = api::RETWEET.to_string() + tweet_id_str.as_str() + ".json";
    let bytes_raw = oauth_client::post(url.as_str(), api_key, Some(token), None).unwrap();
    let bytes_slice = bytes_raw.as_slice();
    let parsed_maybe = parse::parse_tweets(bytes_slice);
    if let IResult::Done(_,parsed) = parsed_maybe {
        println!("{}\n    Retweet", parsed[0]);
    }
    else {
        println!("Parse error when attempting to read tweet data. Did you already retweet this tweet?");
    }
}

/// Unrewteet a tweet by its id
pub fn unretweet(tweet_id: u64, api_key: &Token, token: &Token) {
	let tweet_id_str = tweet_id.to_string();
	let url = api::UNRETWEET.to_string() + tweet_id_str.as_str() + ".json";
    let bytes_raw = oauth_client::post(url.as_str(), api_key, Some(token), None).unwrap();
    let bytes_slice = bytes_raw.as_slice();
    let parsed_maybe = parse::parse_tweets(bytes_slice);
    if let IResult::Done(_,parsed) = parsed_maybe {
        println!("{}\n    Retweet", parsed[0]);
    }
    else {
        println!("Parse error when attempting to read tweet data. Did you retweet this tweet?");
    }
}

/// Favorite a tweet by its id
pub fn favorite_tweet(tweet_id: u64, api_key: &Token, token: &Token) {
	let tweet_id_str = tweet_id.to_string();
    let mut param = HashMap::new();
    let _ = param.insert("id".into(), tweet_id_str.into());
    let bytes_raw = oauth_client::post(api::FAVORITE, api_key, Some(token), Some(&param)).unwrap();
    let bytes_slice = bytes_raw.as_slice();
    let parsed_maybe = parse::parse_tweets(bytes_slice);
    if let IResult::Done(_,parsed) = parsed_maybe {
        println!("{}\n    Favorited", parsed[0]);
    }
    else {
        println!("Parse error when attempting to read tweet data. Did you already favorite this tweet?");
    }
}

/// Unfavorite a tweet by its id
pub fn unfavorite_tweet(tweet_id: u64, api_key: &Token, token: &Token) {
	let tweet_id_str = tweet_id.to_string();
    let mut param = HashMap::new();
    let _ = param.insert("id".into(), tweet_id_str.into());
    let bytes_raw = oauth_client::post(api::UNFAVORITE, api_key, Some(token), Some(&param)).unwrap(); 
    let bytes_slice = bytes_raw.as_slice();
    let parsed_maybe = parse::parse_tweets(bytes_slice);
    if let IResult::Done(_,parsed) = parsed_maybe {
        println!("{}\n    Unfavorited", parsed[0]);
    }
    else {
        println!("Parse error when attempting to read tweet data. Did you favorite this tweet?");
    }
}

/// urls for the twitter api 
pub mod api {
    pub const USER_PROFILE: &'static str = "https://api.twitter.com/1.1/statuses/user_timeline.json";
    pub const TIMELINE: &'static str = "https://api.twitter.com/1.1/statuses/home_timeline.json";
    pub const STATUS_UPDATE: &'static str = "https://api.twitter.com/1.1/statuses/update.json";
    pub const RETWEET: &'static str = "https://api.twitter.com/1.1/statuses/retweet/";
    pub const UNRETWEET: &'static str = "https://api.twitter.com/1.1/statuses/unretweet/";
    pub const DELETE: &'static str = "https://api.twitter.com/1.1/statuses/destroy/";
    pub const UPLOAD: &'static str = "https://upload.twitter.com/1.1/media/upload.json";
    pub const FAVORITE: &'static str = "https://api.twitter.com/1.1/favorites/create.json";
    pub const UNFAVORITE: &'static str = "https://api.twitter.com/1.1/favorites/destroy.json";
    pub const FOLLOW: &'static str = "https://api.twitter.com/1.1/friendships/create.json";
    pub const UNFOLLOW: &'static str = "https://api.twitter.com/1.1/friendships/destroy.json";
}