From 988f012a3ef64a38f9d0f39e817a02cf02cd198e Mon Sep 17 00:00:00 2001 From: trivernis Date: Wed, 22 Feb 2023 18:48:48 +0100 Subject: [PATCH] Remove twitter features --- src/args.rs | 4 -- src/config.rs | 6 -- src/main.rs | 22 +------ src/operations/find_and_send_twitter_posts.rs | 42 ------------- src/operations/mod.rs | 1 - src/utils/mod.rs | 1 - src/utils/twitter.rs | 60 ------------------- src/utils/urls.rs | 8 --- 8 files changed, 2 insertions(+), 142 deletions(-) delete mode 100644 src/operations/find_and_send_twitter_posts.rs delete mode 100644 src/utils/twitter.rs diff --git a/src/args.rs b/src/args.rs index c26e5f8..be5d4d5 100644 --- a/src/args.rs +++ b/src/args.rs @@ -23,10 +23,6 @@ pub enum Command { #[clap(name = "import-reddit-posts")] ImportRedditPosts(ImportUrlsOptions), - /// Looks up and imports tweets - #[clap(name = "import-tweets")] - ImportTweets(ImportUrlsOptions), - /// Looks up a list of urls and imports media found for them #[clap(name = "import-urls")] ImportUrls(ImportUrlsOptions), diff --git a/src/config.rs b/src/config.rs index 519239d..70a9e11 100644 --- a/src/config.rs +++ b/src/config.rs @@ -55,10 +55,4 @@ impl Config { self.saucenao .expect("No saucenao key configured. Please add one to the config file.") } - - /// REturns the twitter api configuration or panics if nothing is configured - pub fn into_twitter_cfg(self) -> TwitterConfig { - self.twitter - .expect("No twitter api credentials configured. Please add them to the config file.") - } } diff --git a/src/main.rs b/src/main.rs index 3aaeef7..b995116 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,6 @@ pub mod utils; use crate::config::Config; use crate::config::SauceNaoConfig; -use crate::config::TwitterConfig; use crate::error::Result; use crate::operations::find_and_send_tags::find_and_send_tags; use crate::operations::find_and_send_urls::find_and_send_urls; @@ -16,7 +15,6 @@ use hydrus_api::wrapper::service::ServiceName; use hydrus_api::wrapper::tag::Tag; use hydrus_api::{Client, Hydrus}; use operations::find_and_send_reddit_posts::find_and_send_reddit_posts; -use operations::find_and_send_twitter_posts::find_and_send_twitter_posts; use pixiv_rs::PixivClient; use rustnao::{Handler, HandlerBuilder}; use std::str::FromStr; @@ -46,8 +44,7 @@ async fn main() { send_tags_or_urls(opt, config.into_saucenao(), hydrus, false).await } Command::ImportRedditPosts(opt) => import_reddit_posts(opt, hydrus).await, - Command::ImportTweets(opt) => import_tweets(opt, config.into_twitter_cfg(), hydrus).await, - Command::ImportUrls(opt) => import_urls(opt, config, hydrus).await, + Command::ImportUrls(opt) => import_urls(opt, hydrus).await, } .expect("Failed to send tags or urls"); } @@ -124,26 +121,14 @@ async fn import_reddit_posts(opt: ImportUrlsOptions, hydrus: Hydrus) -> Result<( find_and_send_reddit_posts(&hydrus, urls).await } -#[tracing::instrument(level = "debug", skip(hydrus))] -async fn import_tweets( - opt: ImportUrlsOptions, - twitter_cfg: TwitterConfig, - hydrus: Hydrus, -) -> Result<()> { - let urls = get_urls_from_args(opt).await?; - find_and_send_twitter_posts(&hydrus, twitter_cfg, urls).await -} - -async fn import_urls(opt: ImportUrlsOptions, cfg: Config, hydrus: Hydrus) -> Result<()> { +async fn import_urls(opt: ImportUrlsOptions, hydrus: Hydrus) -> Result<()> { let urls = get_urls_from_args(opt).await?; let mut reddit_urls = Vec::new(); - let mut twitter_urls = Vec::new(); let mut unknown_urls = Vec::new(); for url in urls { match find_url_type(&url) { UrlType::Reddit => reddit_urls.push(url), - UrlType::Twitter => twitter_urls.push(url), UrlType::Other => { tracing::warn!("Unknown url type {url}"); unknown_urls.push(url) @@ -153,9 +138,6 @@ async fn import_urls(opt: ImportUrlsOptions, cfg: Config, hydrus: Hydrus) -> Res tracing::info!("Importing reddit posts..."); find_and_send_reddit_posts(&hydrus, reddit_urls).await?; - tracing::info!("Importing twitter posts..."); - find_and_send_twitter_posts(&hydrus, cfg.into_twitter_cfg(), twitter_urls).await?; - tracing::info!("Importing unknown urls..."); for url in unknown_urls { diff --git a/src/operations/find_and_send_twitter_posts.rs b/src/operations/find_and_send_twitter_posts.rs deleted file mode 100644 index b50a127..0000000 --- a/src/operations/find_and_send_twitter_posts.rs +++ /dev/null @@ -1,42 +0,0 @@ -use egg_mode::Token; -use hydrus_api::Hydrus; - -use crate::config::TwitterConfig; -use crate::error::Result; -use crate::utils::twitter::{get_token, get_tweet_media}; - -#[tracing::instrument(level = "debug", skip(hydrus))] -pub async fn find_and_send_twitter_posts( - hydrus: &Hydrus, - twitter_cfg: TwitterConfig, - post_urls: Vec, -) -> Result<()> { - let token = get_token(twitter_cfg).await?; - let total_posts = post_urls.len(); - - for (index, post) in post_urls.into_iter().enumerate() { - tracing::info!("Importing post {} of {}", index + 1, total_posts); - if let Err(e) = import_post(&post, hydrus, &token).await { - tracing::error!("Failed to import {}: {}", post, e); - } - } - - Ok(()) -} - -#[tracing::instrument(level = "debug", skip(hydrus))] -async fn import_post(post_url: &str, hydrus: &Hydrus, token: &Token) -> Result<()> { - tracing::debug!("Tweet {}", post_url); - let images = get_tweet_media(post_url, token).await?; - tracing::info!("Found {} images for tweet {}", images.len(), post_url); - - for url in images { - let mut entry = hydrus.import().url(url).run().await?; - let files = entry.files().await?; - - for mut file in files { - file.associate_urls(vec![post_url.to_string()]).await?; - } - } - Ok(()) -} diff --git a/src/operations/mod.rs b/src/operations/mod.rs index a64d8cf..6b905c2 100644 --- a/src/operations/mod.rs +++ b/src/operations/mod.rs @@ -1,4 +1,3 @@ pub mod find_and_send_reddit_posts; pub mod find_and_send_tags; -pub mod find_and_send_twitter_posts; pub mod find_and_send_urls; diff --git a/src/utils/mod.rs b/src/utils/mod.rs index dc4603a..420169a 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,6 +1,5 @@ pub mod pixiv; pub mod reddit; -pub mod twitter; pub mod urls; use crate::error::Result; use directories::ProjectDirs; diff --git a/src/utils/twitter.rs b/src/utils/twitter.rs deleted file mode 100644 index 9598abf..0000000 --- a/src/utils/twitter.rs +++ /dev/null @@ -1,60 +0,0 @@ -use std::borrow::Cow; - -use crate::{config::TwitterConfig, error::Result}; -use egg_mode::auth::Token; - -/// Returns the token that -/// can be used for twitter api requests -#[tracing::instrument(level = "debug")] -pub async fn get_token(config: TwitterConfig) -> Result { - let con_token = egg_mode::KeyPair::new( - Cow::from(config.consumer_key), - Cow::from(config.consumer_secret), - ); - let token = egg_mode::auth::bearer_token(&con_token).await?; - - Ok(token) -} - -/// Returns the media urls for a given tweet -#[tracing::instrument(level = "debug", skip(token))] -pub async fn get_tweet_media(url: &str, token: &Token) -> Result> { - let id = get_tweet_id(url)?; - let tweet = egg_mode::tweet::show(id, token).await?.response; - - if let Some(entities) = tweet.extended_entities { - let media = entities.media; - let urls: Vec = media - .into_iter() - .map(|m| { - if let Some(video_info) = m.video_info { - video_info.variants.into_iter().next().unwrap().url - } else { - m.media_url_https - } - }) - .collect(); - Ok(urls) - } else { - Ok(Vec::new()) - } -} - -/// Returns the tweet ID for a given twitter url -#[tracing::instrument(level = "debug")] -fn get_tweet_id(url: &str) -> Result { - let mut url = url; - if let Some((left, _right)) = url.split_once('?') { - url = left; - } - let id = url - .rsplit('/') - .filter(|s| !s.is_empty()) - .next() - .ok_or("No Tweet ID in twitter url")?; - let id = id - .parse::() - .map_err(|_| "Tweet ID cannot be parsed as u64")?; - - Ok(id) -} diff --git a/src/utils/urls.rs b/src/utils/urls.rs index a6dbf49..2c234b1 100644 --- a/src/utils/urls.rs +++ b/src/utils/urls.rs @@ -2,15 +2,12 @@ use lazy_regex::regex; pub enum UrlType { Reddit, - Twitter, Other, } pub fn find_url_type(url: &str) -> UrlType { if is_reddit_url(url) { UrlType::Reddit - } else if is_twitter_url(url) { - UrlType::Twitter } else { UrlType::Other } @@ -20,8 +17,3 @@ fn is_reddit_url(url: &str) -> bool { let r = regex!(r#"^http(s)?://(www\.)?(reddit\.com|redd\.it|reddit\.app\.link).*$"#i); r.is_match(url) } - -fn is_twitter_url(url: &str) -> bool { - let r = regex!(r#"^http(s)?://(www\.)?twitter\.com.*$"#); - r.is_match(url) -}