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

@@ -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<u64> {
let mut medias: Vec<u64> = 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::<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),
}
}
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<String>,
@@ -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,