Some more testing on tweet entities

This commit is contained in:
VC
2020-03-01 10:13:00 +01:00
parent fdc000712e
commit 3641bf61bb

View File

@@ -15,6 +15,7 @@ use egg_mode::{
Token,
KeyPair,
tweet::{
Tweet,
user_timeline,
},
};
@@ -38,6 +39,25 @@ fn write_state(f: &str, s: u64) -> Result<(), std::io::Error> {
write(f, format!("{}", s))
}
fn get_oauth2_token(config: &Config) -> Token {
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));
Token::Access {
consumer: con_token,
access: access_token,
}
}
fn get_user_timeline(config: &Config, token: Token, lid: Option<u64>) -> Result<Vec<Tweet>, Box<dyn Error>> {
// fix the page size to 200 as it is the maximum Twitter authorizes
let (_timeline, feed) = block_on_all(user_timeline(&config.twitter.username, false, false, &token)
.with_page_size(200)
.older(lid))?;
Ok(feed.to_vec())
}
/**********
* Config structure
***********/
@@ -92,26 +112,30 @@ impl Config {
/// This is where the magic happens
pub fn run(config: Config) {
// retrieve the last tweet ID for the username
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));
// get OAuth2 token
let token = get_oauth2_token(&config);
let token = Token::Access {
consumer: con_token,
access: access_token,
};
// get user timeline feed (Vec<tweet>)
let feed = get_user_timeline(&config, token, last_tweet_id).unwrap_or_else(|e|
panic!("Something went wrong when trying to retrieve {}s timeline: {}", &config.twitter.username, e)
);
let (_timeline, mut feed) = block_on_all(user_timeline(&config.twitter.username, false, false, &token)
.with_page_size(200)
.older(last_tweet_id)).unwrap();
for tweet in &feed {
println!("{}: <@{}> {}", tweet.id, tweet.user.as_ref().unwrap().screen_name, tweet.text);
if feed.len() == 0 {
println!("Nothing to retrieve since last time, exiting…");
return;
}
for tweet in &feed {
println!("ID: {}\ttext: {}\tentities: {:#?}\tmedia: {:#?}", tweet.id, tweet.text, tweet.entities, tweet.extended_entities);
}
/*
write_state(&config.twitter.last_tweet_path, feed[0].id).unwrap_or_else(|e|
panic!("Cant write the last tweet retrieved: {}", e)
);
*/
}