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
//! This crate provides an executable Command Line Iinterface Tweeter, 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
//! ```
#[macro_use] extern crate nom;

extern crate oauth_client_fix as oauth_client;
extern crate core;

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

pub mod parse;
pub mod types;

/// 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("");
    iter.next();
    let api_sec = iter.next().expect("");
    iter.next();
    let tok = iter.next().expect("");
    iter.next();
    let tok_sec = iter.next().expect("");
    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(), "15".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 resp = String::from_utf8(bytes_raw).unwrap();
    println!("response:\n{}", resp);
}

/*
/// Return profile for a given user. 
pub fn get_profile<'a>(screen_name: &str, num: u8, api_key: Token, token: Token) -> Result<Vec<Tweet<'a>>,()> {
    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 bytestring = String::from_utf8(bytes_raw).unwrap();
    let bytes_slice = bytestring.as_bytes();
    // parse as an IResult
    let parsed_maybe = parse::parse_tweets(bytes_slice);
    match parsed_maybe {
        IResult::Done(_,parsed) => Ok(parsed),
        _ => Err(panic!("Tweet failed to parse!")),
    }
}
*/

/// 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
/// 
/// ```
/// 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 resp = String::from_utf8(bytes_raw).unwrap();
    let bytes_slice = resp.as_bytes();
    let parsed_maybe = parse::parse_tweets(bytes_slice);
    if let IResult::Done(_,parsed) = parsed_maybe {
        for i in 0..parsed.len() {
            if show_ids {
                println!("{:?}", parsed[i]);
            }
            else
            {
                println!("{}", parsed[i]);
            }
        }
    }
    else {
        println!("Parse error when attempting to read tweet data.");
    }
}

/// Send a tweet
///
/// # Examples
///
/// ```
/// 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();
    let resp = String::from_utf8(bytes_raw).unwrap();
    let bytes_slice = resp.as_bytes();
    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
///
/// ```
/// 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();
    let resp = String::from_utf8(bytes_raw).unwrap();
    let bytes_slice = resp.as_bytes();
    let parsed_maybe = parse::parse_tweets(bytes_slice);
    // FIXME just pop it off so it's faster? we only want one.
    if let IResult::Done(_,parsed) = parsed_maybe {
        println!("{}", parsed[0]);
    }
    else {
        println!("Parse error when attempting to read tweet data.");
    }
}

/// 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
/// 
/// ```
/// 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();
    // convert vector of u8's to &[u8] (array slice)
    let resp = String::from_utf8(bytes_raw).unwrap();
    let bytes_slice = resp.as_bytes();
    let parsed_maybe = parse::parse_tweets(bytes_slice);
    if let IResult::Done(_,parsed) = parsed_maybe {
        for i in 0..parsed.len() {
            if show_ids {
                println!("{:?}", parsed[i]);
            }
            else
            {
                println!("{}", parsed[i]);
            }
        }
    }
    else {
        println!("Parse error when attempting to read tweet data.");
    }
}

/// Delete a tweet by its id
pub fn delete_tweet(num: u64, api_key: Token, token: Token) {
	let num_str = num.to_string();
	let url = api::DELETE.to_string() + num_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(num: u64, api_key: Token, token: Token) {
	let num_str = num.to_string();
	let url = api::RETWEET.to_string() + num_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 retweeted successfully!");
}

/// 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 DELETE: &'static str = "https://api.twitter.com/1.1/statuses/destroy/";
}