mirror of
https://framagit.org/veretcle/oolatoocs.git
synced 2025-07-21 13:24:18 +02:00
feat: move media generation list to twitter.rs to avoid clutter
This commit is contained in:
44
src/lib.rs
44
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<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)
|
||||
|
Reference in New Issue
Block a user