It starts to look like something…

This commit is contained in:
VC
2020-03-01 09:35:19 +01:00
parent 52f98690a5
commit b6748975c3
4 changed files with 70 additions and 51 deletions

View File

@@ -1,5 +1,8 @@
// std
use std::fs::read_to_string;
use std::{
fs::{read_to_string, write},
error::Error,
};
// clap
use clap::{App, Arg};
@@ -13,13 +16,33 @@ use egg_mode::{
KeyPair,
tweet::{
user_timeline,
Tweet,
},
};
// tokio
use tokio::runtime::current_thread::block_on_all;
/**********
* Generic usage functions
***********/
fn read_state(s: &str) -> Option<u64> {
let state = read_to_string(s);
if let Ok(s) = state {
return s.parse::<u64>().ok();
}
None
}
fn write_state(f: &str, s: u64) -> Result<(), std::io::Error> {
write(f, format!("{}", s))
}
/**********
* Config structure
***********/
/// General configuration Struct
#[derive(Debug, Deserialize)]
pub struct Config {
twitter: TwitterConfig,
@@ -32,9 +55,11 @@ struct TwitterConfig {
consumer_secret: String,
access_key: String,
access_secret: String,
last_tweet_path: String,
}
impl Config {
/// parses configuration from command line and TOML config file
pub fn new() -> Config {
let matches = App::new(env!("CARGO_PKG_NAME"))
.version(env!("CARGO_PKG_VERSION"))
@@ -61,19 +86,32 @@ impl Config {
}
}
/*********
* Main function
*********/
/// This is where the magic happens
pub fn run(config: Config) {
let con_token = KeyPair::new(config.twitter.consumer_key, config.twitter.consumer_secret);
let access_token = KeyPair::new(config.twitter.access_key, config.twitter.access_secret);
let last_tweet_id = read_state(&config.twitter.last_tweet_path);
let con_token = KeyPair::new(String::from(&config.twitter.consumer_key), String::from(&config.twitter.consumer_secret));
let access_token = KeyPair::new(String::from(&config.twitter.access_key), String::from(&config.twitter.access_secret));
let token = Token::Access {
consumer: con_token,
access: access_token,
};
let timeline = user_timeline(&config.twitter.username, false, false, &token)
let (_timeline, mut feed) = block_on_all(user_timeline(&config.twitter.username, false, false, &token)
.with_page_size(200)
.older(None);
.older(last_tweet_id)).unwrap();
timeline.types();
for tweet in &feed {
println!("{}: <@{}> {}", tweet.id, tweet.user.as_ref().unwrap().screen_name, tweet.text);
}
write_state(&config.twitter.last_tweet_path, feed[0].id).unwrap_or_else(|e|
panic!("Cant write the last tweet retrieved: {}", e)
);
}