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:
@@ -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,
|
||||
|
Reference in New Issue
Block a user