// auto-imports use crate::ScootalooError; use crate::config::TwitterConfig; use crate::util::cache_media; // std use std::error::Error; // egg-mode use egg_mode::{ Token, KeyPair, entities::{MediaEntity, MediaType}, user::UserID, tweet::{ Tweet, user_timeline, }, }; /// Gets Twitter oauth2 token pub fn get_oauth2_token(config: &TwitterConfig) -> Token { let con_token = KeyPair::new(String::from(&config.consumer_key), String::from(&config.consumer_secret)); let access_token = KeyPair::new(String::from(&config.access_key), String::from(&config.access_secret)); Token::Access { consumer: con_token, access: access_token, } } /// Gets Twitter user timeline pub async fn get_user_timeline(config: &TwitterConfig, token: Token, lid: Option) -> Result, Box> { // fix the page size to 200 as it is the maximum Twitter authorizes let (_, feed) = user_timeline(UserID::from(String::from(&config.username)), true, false, &token) .with_page_size(200) .older(lid) .await?; Ok(feed.to_vec()) } /// Retrieves a single media from a tweet and store it in a temporary file pub async fn get_tweet_media(m: &MediaEntity, t: &str) -> Result> { match m.media_type { MediaType::Photo => { return cache_media(&m.media_url_https, t).await; }, _ => { match &m.video_info { Some(v) => { for variant in &v.variants { if variant.content_type == "video/mp4" { return cache_media(&variant.url, t).await; } } return Err(ScootalooError::new(&format!("Media Type for {} is video but no mp4 file URL is available", &m.url)).into()); }, None => { return Err(ScootalooError::new(&format!("Media Type for {} is video but does not contain any video_info", &m.url)).into()); }, } }, }; }