From e8bde4c779d36a9fb7083e06d67e2dcb97a4e5e1 Mon Sep 17 00:00:00 2001 From: VC Date: Mon, 20 Nov 2023 15:27:09 +0100 Subject: [PATCH] feat: move media generation list to twitter.rs to avoid clutter --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/lib.rs | 44 ++------------------------------------------ src/twitter.rs | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 50 insertions(+), 47 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f2e0afa..a7b5dce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -972,7 +972,7 @@ checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" [[package]] name = "oolatoocs" -version = "1.3.0" +version = "1.3.1" dependencies = [ "clap", "env_logger", diff --git a/Cargo.toml b/Cargo.toml index 671ca4f..0fa65d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "oolatoocs" -version = "1.3.0" +version = "1.3.1" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/lib.rs b/src/lib.rs index bf0a28d..6e6069e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,13 +18,9 @@ use utils::{generate_multi_tweets, strip_everything}; mod twitter; #[allow(unused_imports)] -use twitter::{post_tweet, upload_chunk_media, upload_simple_media}; +use twitter::{generate_media_ids, post_tweet}; -use futures::{stream, StreamExt}; -use log::{error, warn}; -use megalodon::entities::attachment::AttachmentType; use rusqlite::Connection; -use std::error::Error; #[tokio::main] pub async fn run(config: &Config) { @@ -62,43 +58,7 @@ pub async fn run(config: &Config) { }; // treats medias - let mut medias: Vec = vec![]; - - let media_attachments = toot.media_attachments.clone(); - let mut stream = stream::iter(media_attachments) - .map(|media| { - let twitter_config = config.twitter.clone(); - tokio::task::spawn(async move { - match media.r#type { - AttachmentType::Image => { - upload_simple_media(&twitter_config, &media.url, &media.description) - .await - } - AttachmentType::Gifv => { - upload_chunk_media(&twitter_config, &media.url, "tweet_gif").await - } - AttachmentType::Video => { - upload_chunk_media(&twitter_config, &media.url, "tweet_video").await - } - _ => Err::>( - OolatoocsError::new(&format!( - "Cannot treat this type of media: {}", - &media.url - )) - .into(), - ), - } - }) - }) - .buffered(4); - - while let Some(result) = stream.next().await { - match result { - Ok(Ok(v)) => medias.push(v), - Ok(Err(e)) => warn!("Cannot treat media: {}", e), - Err(e) => error!("Something went wrong when joining the main thread: {}", e), - } - } + let medias = generate_media_ids(&config.twitter, &toot.media_attachments).await; // posts corresponding tweet let tweet_id = post_tweet(&config.twitter, &tweet_content, &medias, &reply_to) diff --git a/src/twitter.rs b/src/twitter.rs index aeb6de5..264d51b 100644 --- a/src/twitter.rs +++ b/src/twitter.rs @@ -1,6 +1,8 @@ use crate::config::TwitterConfig; use crate::error::OolatoocsError; -use log::debug; +use futures::{stream, StreamExt}; +use log::{debug, error, warn}; +use megalodon::entities::attachment::{Attachment, AttachmentType}; use oauth1_request::Token; use reqwest::{ multipart::{Form, Part}, @@ -99,8 +101,49 @@ fn get_token(config: &TwitterConfig) -> Token { ) } +pub async fn generate_media_ids(config: &TwitterConfig, media_attach: &[Attachment]) -> Vec { + let mut medias: Vec = vec![]; + + let media_attachments = media_attach.to_owned(); + let mut stream = stream::iter(media_attachments) + .map(|media| { + let twitter_config = config.clone(); + tokio::task::spawn(async move { + match media.r#type { + AttachmentType::Image => { + upload_simple_media(&twitter_config, &media.url, &media.description).await + } + AttachmentType::Gifv => { + upload_chunk_media(&twitter_config, &media.url, "tweet_gif").await + } + AttachmentType::Video => { + upload_chunk_media(&twitter_config, &media.url, "tweet_video").await + } + _ => Err::>( + OolatoocsError::new(&format!( + "Cannot treat this type of media: {}", + &media.url + )) + .into(), + ), + } + }) + }) + .buffered(4); + + while let Some(result) = stream.next().await { + match result { + Ok(Ok(v)) => medias.push(v), + Ok(Err(e)) => warn!("Cannot treat media: {}", e), + Err(e) => error!("Something went wrong when joining the main thread: {}", e), + } + } + + medias +} + /// This function uploads simple images from Mastodon to Twitter and returns the media id from Twitter -pub async fn upload_simple_media( +async fn upload_simple_media( config: &TwitterConfig, u: &str, d: &Option, @@ -191,7 +234,7 @@ async fn metadata_create( } /// This posts video/gif to Twitter and returns the media id from Twitter -pub async fn upload_chunk_media( +async fn upload_chunk_media( config: &TwitterConfig, u: &str, t: &str,