feat: async upload of medias

This commit is contained in:
VC
2023-11-15 14:32:23 +01:00
parent 00ba8bda42
commit b73d6340c9
5 changed files with 90 additions and 50 deletions

View File

@@ -20,8 +20,11 @@ mod twitter;
#[allow(unused_imports)]
use twitter::{post_tweet, upload_chunk_media, upload_simple_media};
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) {
@@ -40,42 +43,45 @@ pub async fn run(config: &Config) {
let Ok(tweet_content) = strip_everything(&toot.content, &toot.tags) else {
continue; // skip in case we cant strip something
};
let mut medias: Vec<u64> = vec![];
// if we wanted to cut toot in half, now would be the right time to do so
// treats media
for media in toot.media_attachments {
let id = match media.r#type {
AttachmentType::Image => {
let Ok(id) =
upload_simple_media(&config.twitter, &media.url, &media.description).await
else {
continue;
};
id
}
AttachmentType::Gifv => {
let Ok(id) = upload_chunk_media(&config.twitter, &media.url, "tweet_gif").await
else {
continue;
};
id
}
AttachmentType::Video => {
let Ok(id) =
upload_chunk_media(&config.twitter, &media.url, "tweet_video").await
else {
continue;
};
id
}
_ => {
continue;
}
};
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);
medias.push(id);
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),
}
}
// threads if necessary