mirror of
https://framagit.org/veretcle/oolatoocs.git
synced 2025-07-20 20:41:17 +02:00
feat: move media generation list to twitter.rs to avoid clutter
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -972,7 +972,7 @@ checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "oolatoocs"
|
name = "oolatoocs"
|
||||||
version = "1.3.0"
|
version = "1.3.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "oolatoocs"
|
name = "oolatoocs"
|
||||||
version = "1.3.0"
|
version = "1.3.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
44
src/lib.rs
44
src/lib.rs
@@ -18,13 +18,9 @@ use utils::{generate_multi_tweets, strip_everything};
|
|||||||
|
|
||||||
mod twitter;
|
mod twitter;
|
||||||
#[allow(unused_imports)]
|
#[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 rusqlite::Connection;
|
||||||
use std::error::Error;
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
pub async fn run(config: &Config) {
|
pub async fn run(config: &Config) {
|
||||||
@@ -62,43 +58,7 @@ pub async fn run(config: &Config) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// treats medias
|
// treats medias
|
||||||
let mut medias: Vec<u64> = vec![];
|
let medias = generate_media_ids(&config.twitter, &toot.media_attachments).await;
|
||||||
|
|
||||||
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),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// posts corresponding tweet
|
// posts corresponding tweet
|
||||||
let tweet_id = post_tweet(&config.twitter, &tweet_content, &medias, &reply_to)
|
let tweet_id = post_tweet(&config.twitter, &tweet_content, &medias, &reply_to)
|
||||||
|
@@ -1,6 +1,8 @@
|
|||||||
use crate::config::TwitterConfig;
|
use crate::config::TwitterConfig;
|
||||||
use crate::error::OolatoocsError;
|
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 oauth1_request::Token;
|
||||||
use reqwest::{
|
use reqwest::{
|
||||||
multipart::{Form, Part},
|
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
|
/// 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,
|
config: &TwitterConfig,
|
||||||
u: &str,
|
u: &str,
|
||||||
d: &Option<String>,
|
d: &Option<String>,
|
||||||
@@ -191,7 +234,7 @@ async fn metadata_create(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// This posts video/gif to Twitter and returns the media id from Twitter
|
/// 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,
|
config: &TwitterConfig,
|
||||||
u: &str,
|
u: &str,
|
||||||
t: &str,
|
t: &str,
|
||||||
|
Reference in New Issue
Block a user