mirror of
https://framagit.org/veretcle/oolatoocs.git
synced 2025-07-20 20:41:17 +02:00
feat: async upload of medias
This commit is contained in:
68
src/lib.rs
68
src/lib.rs
@@ -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 can’t 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
|
||||
|
Reference in New Issue
Block a user