feat: move media generation list to twitter.rs to avoid clutter

This commit is contained in:
VC
2023-11-20 15:27:09 +01:00
parent 80946ac131
commit e8bde4c779
4 changed files with 50 additions and 47 deletions

View File

@@ -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<u64> = 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::<u64, Box<dyn Error + Send + Sync>>(
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)