Skip to content

Instantly share code, notes, and snippets.

@QuietMisdreavus
Created February 7, 2018 20:13
Show Gist options
  • Save QuietMisdreavus/3426ad7b2c2e3f5528dbdac2f0563a13 to your computer and use it in GitHub Desktop.
Save QuietMisdreavus/3426ad7b2c2e3f5528dbdac2f0563a13 to your computer and use it in GitHub Desktop.
// this is most of examples/common/mod.rs from egg-mode, i'm too lazy to write all the
// authentication stuff more than once >_>
use std;
use std::io::{Write, Read};
use egg_mode;
use tokio_core::reactor::Core;
pub struct Config {
pub token: egg_mode::Token,
pub user_id: u64,
pub screen_name: String,
}
impl Config {
pub fn load(core: &mut Core) -> Self {
//IMPORTANT: make an app for yourself at apps.twitter.com and get your
//key/secret into these files; these examples won't work without them
let consumer_key = include_str!("consumer_key").trim();
let consumer_secret = include_str!("consumer_secret").trim();
let handle = core.handle();
let con_token = egg_mode::KeyPair::new(consumer_key, consumer_secret);
let mut config = String::new();
let user_id: u64;
let username: String;
let token: egg_mode::Token;
//look at all this unwrapping! who told you it was my birthday?
if let Ok(mut f) = std::fs::File::open("twitter_settings") {
f.read_to_string(&mut config).unwrap();
let mut iter = config.split('\n');
username = iter.next().unwrap().to_string();
user_id = u64::from_str_radix(&iter.next().unwrap(), 10).unwrap();
let access_token = egg_mode::KeyPair::new(iter.next().unwrap().to_string(),
iter.next().unwrap().to_string());
token = egg_mode::Token::Access {
consumer: con_token,
access: access_token,
};
if let Err(err) = core.run(egg_mode::verify_tokens(&token, &handle)) {
println!("We've hit an error using your old tokens: {:?}", err);
println!("We'll have to reauthenticate before continuing.");
std::fs::remove_file("twitter_settings").unwrap();
} else {
println!("Welcome back, {}!", username);
}
} else {
let request_token = core.run(egg_mode::request_token(&con_token, "oob", &handle)).unwrap();
println!("Go to the following URL, sign in, and give me the PIN that comes back:");
println!("{}", egg_mode::authorize_url(&request_token));
let mut pin = String::new();
std::io::stdin().read_line(&mut pin).unwrap();
println!("");
let tok_result = core.run(egg_mode::access_token(con_token, &request_token, pin, &handle)).unwrap();
token = tok_result.0;
user_id = tok_result.1;
username = tok_result.2;
match token {
egg_mode::Token::Access { access: ref access_token, .. } => {
config.push_str(&username);
config.push('\n');
config.push_str(&format!("{}", user_id));
config.push('\n');
config.push_str(&access_token.key);
config.push('\n');
config.push_str(&access_token.secret);
},
_ => unreachable!(),
}
let mut f = std::fs::File::create("twitter_settings").unwrap();
f.write_all(config.as_bytes()).unwrap();
println!("Welcome, {}, let's get this show on the road!", username);
}
//TODO: Is there a better way to query whether a file exists?
if std::fs::metadata("twitter_settings").is_ok() {
Config {
token: token,
user_id: user_id,
screen_name: username,
}
} else {
Self::load(core)
}
}
}
[package]
name = "test-egg-mode"
version = "0.1.0"
authors = ["QuietMisdreavus <[email protected]>"]
[dependencies]
egg-mode = "0.12.0"
tokio-core = "0.1.11"
extern crate egg_mode;
extern crate tokio_core;
// this is literally examples/common/mod.rs from egg-mode, i'm too lazy to write all that
// authentication stuff more than once >_>
mod auth;
use tokio_core::reactor::Core;
use egg_mode::media::UploadBuilder;
use egg_mode::media::media_types::image_gif;
use egg_mode::tweet::DraftTweet;
// https://imgur.com/t/science_and_tech/Nr0ND
static CHEMISTRY: &'static [u8] = include_bytes!("chemistry.gif");
fn main() {
let mut core = Core::new().unwrap();
let c = auth::Config::load(&mut core);
let handle = core.handle();
println!("uploading gif...");
let builder = UploadBuilder::new(CHEMISTRY, image_gif());
let media_handle = core.run(builder.call(&c.token, &handle)).unwrap();
println!("sending tweet...");
let draft = DraftTweet::new("need to make sure i can still upload a gif in egg-mode, here's my test gif");
let draft = draft.media_ids(&[media_handle.id]);
let tweet = core.run(draft.send(&c.token, &handle)).unwrap();
println!("message: {}", tweet.text);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment