feat: add video/gif medias

This commit is contained in:
VC
2023-11-09 18:40:28 +01:00
parent 6fca84c3be
commit eba13ba095
5 changed files with 411 additions and 17 deletions

2
Cargo.lock generated
View File

@@ -1058,7 +1058,7 @@ checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
[[package]] [[package]]
name = "oolatoocs" name = "oolatoocs"
version = "0.1.0" version = "0.3.0"
dependencies = [ dependencies = [
"clap", "clap",
"dissolve", "dissolve",

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "oolatoocs" name = "oolatoocs"
version = "0.1.0" version = "0.3.0"
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
@@ -13,10 +13,10 @@ log = "^0.4"
megalodon = "^0.11" megalodon = "^0.11"
oauth1-request = "^0.6" oauth1-request = "^0.6"
regex = "1.10.2" regex = "1.10.2"
reqwest = { version = "0.11.22", features = ["json"] } reqwest = { version = "0.11.22", features = ["json", "stream", "multipart"] }
rusqlite = "^0.27" rusqlite = "^0.27"
serde = { version = "^1.0", features = ["derive"] } serde = { version = "^1.0", features = ["derive"] }
tokio = { version = "^1.33", features = ["rt-multi-thread", "macros"] } tokio = { version = "^1.33", features = ["rt-multi-thread", "macros", "time"] }
toml = "^0.8" toml = "^0.8"
[profile.release] [profile.release]

25
src/error.rs Normal file
View File

@@ -0,0 +1,25 @@
use std::{
error::Error,
fmt::{Display, Formatter, Result},
};
#[derive(Debug)]
pub struct OolatoocsError {
details: String,
}
impl OolatoocsError {
pub fn new(msg: &str) -> OolatoocsError {
OolatoocsError {
details: msg.to_string(),
}
}
}
impl Error for OolatoocsError {}
impl Display for OolatoocsError {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f, "{}", self.details)
}
}

View File

@@ -1,3 +1,6 @@
mod error;
pub use error::OolatoocsError;
mod config; mod config;
pub use config::{parse_toml, Config}; pub use config::{parse_toml, Config};
@@ -15,8 +18,9 @@ use utils::strip_everything;
mod twitter; mod twitter;
#[allow(unused_imports)] #[allow(unused_imports)]
use twitter::{post_tweet, upload_media}; use twitter::{post_tweet, upload_chunk_media, upload_simple_media};
use megalodon::entities::attachment::AttachmentType;
use rusqlite::Connection; use rusqlite::Connection;
#[tokio::main] #[tokio::main]
@@ -36,9 +40,46 @@ pub async fn run(config: &Config) {
let Ok(tweet_content) = strip_everything(&toot.content, &toot.tags) else { let Ok(tweet_content) = strip_everything(&toot.content, &toot.tags) else {
continue; // skip in case we cant strip something continue; // skip in case we cant 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 // if we wanted to cut toot in half, now would be the right time to do so
// treating medias (nothing for now)
let tweet_id = post_tweet(&config.twitter, &tweet_content, &[]) 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;
}
};
medias.push(id);
}
println!("{:?}", medias);
let tweet_id = post_tweet(&config.twitter, &tweet_content, &medias)
.await .await
.unwrap_or_else(|e| panic!("Cannot Tweet {}: {}", toot.id, e)); .unwrap_or_else(|e| panic!("Cannot Tweet {}: {}", toot.id, e));

View File

@@ -1,16 +1,33 @@
use crate::config::TwitterConfig; use crate::config::TwitterConfig;
use crate::error::OolatoocsError;
use log::debug;
use oauth1_request::Token; use oauth1_request::Token;
use reqwest::Client; use reqwest::{
multipart::{Form, Part},
Body, Client,
};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::error::Error; use std::error::Error;
use tokio::time::{sleep, Duration};
/// I dont know, dont ask me const TWITTER_API_TWEET_URL: &str = "https://api.twitter.com/2/tweets";
const TWITTER_UPLOAD_MEDIA_URL: &str = "https://upload.twitter.com/1.1/media/upload.json";
const TWITTER_METADATA_MEDIA_URL: &str =
"https://upload.twitter.com/1.1/media/metadata/create.json";
// I dont know, dont ask me
#[derive(oauth1_request::Request)] #[derive(oauth1_request::Request)]
struct EmptyRequest {} struct EmptyRequest {}
#[derive(Serialize, Debug)] #[derive(Serialize, Debug)]
pub struct Tweet { pub struct Tweet {
pub text: String, pub text: String,
pub media: TweetMediasIds,
}
#[derive(Serialize, Debug)]
pub struct TweetMediasIds {
pub media_ids: Vec<String>,
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
@@ -23,6 +40,47 @@ pub struct TweetResponseData {
pub id: String, pub id: String,
} }
#[derive(Deserialize, Debug)]
struct UploadMediaResponse {
media_id: u64,
processing_info: Option<UploadMediaResponseProcessingInfo>,
}
#[derive(Deserialize, Debug)]
struct UploadMediaResponseProcessingInfo {
state: UploadMediaResponseProcessingInfoState,
check_after_secs: Option<u64>,
}
#[derive(Deserialize, Debug)]
enum UploadMediaResponseProcessingInfoState {
#[serde(rename = "failed")]
Failed,
#[serde(rename = "succeeded")]
Succeeded,
#[serde(rename = "pending")]
Pending,
#[serde(rename = "in_progress")]
InProgress,
}
#[derive(Serialize, Debug)]
struct MediaMetadata {
media_id: u64,
alt_text: MediaMetadataAltText,
}
#[derive(Serialize, Debug)]
struct MediaMetadataAltText {
text: String,
}
#[derive(Serialize, Debug, oauth1_request::Request)]
struct UploadMediaCommand {
command: String,
media_id: String,
}
/// This function returns the OAuth1 Token object from TwitterConfig /// This function returns the OAuth1 Token object from TwitterConfig
fn get_token(config: &TwitterConfig) -> Token { fn get_token(config: &TwitterConfig) -> Token {
oauth1_request::Token::from_parts( oauth1_request::Token::from_parts(
@@ -33,32 +91,302 @@ fn get_token(config: &TwitterConfig) -> Token {
) )
} }
/// This function uploads media 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
#[allow(dead_code)] pub async fn upload_simple_media(
pub async fn upload_media(_u: &str) -> Result<u64, Box<dyn Error>> { config: &TwitterConfig,
Ok(0) u: &str,
d: &Option<String>,
) -> Result<u64, Box<dyn Error>> {
// initiate request parameters
let empty_request = EmptyRequest {}; // Why? Because fuck you, thats why!
let token = get_token(config);
// retrieve the length and bytes stream from the given URL
let dl = reqwest::get(u).await?;
let content_length = dl
.content_length()
.ok_or(format!("Cannot get content length for {}", u))?;
let stream = dl.bytes_stream();
debug!("Ref download URL: {}", u);
// upload the media
let client = Client::new();
let res: UploadMediaResponse = client
.post(TWITTER_UPLOAD_MEDIA_URL)
.header(
"Authorization",
oauth1_request::post(
TWITTER_UPLOAD_MEDIA_URL,
&empty_request,
&token,
oauth1_request::HMAC_SHA1,
),
)
.multipart(Form::new().part(
"media",
Part::stream_with_length(Body::wrap_stream(stream), content_length),
))
.send()
.await?
.json()
.await?;
debug!("Media ID: {}", res.media_id);
// update the metadata
if let Some(metadata) = d {
debug!("Metadata found! Processing…");
metadata_create(config, res.media_id, metadata).await?;
}
Ok(res.media_id)
}
/// This function updates the metadata given the current media_id and token
async fn metadata_create(config: &TwitterConfig, id: u64, m: &str) -> Result<(), Box<dyn Error>> {
let token = get_token(config);
let empty_request = EmptyRequest {};
let media_metadata = MediaMetadata {
media_id: id,
alt_text: MediaMetadataAltText {
text: m.to_string(),
},
};
debug!("Metadata to process: {}", m);
let client = Client::new();
let metadata = client
.post(TWITTER_METADATA_MEDIA_URL)
.header(
"Authorization",
oauth1_request::post(
TWITTER_METADATA_MEDIA_URL,
&empty_request,
&token,
oauth1_request::HMAC_SHA1,
),
)
.json(&media_metadata)
.send()
.await?;
debug!("Metadata processed with return code: {}", metadata.status());
Ok(())
}
/// This posts video/gif to Twitter and returns the media id from Twitter
pub async fn upload_chunk_media(
config: &TwitterConfig,
u: &str,
t: &str,
) -> Result<u64, Box<dyn Error>> {
let empty_request = EmptyRequest {};
let token = get_token(config);
// retrieve the length, type and bytes stream from the given URL
let mut dl = reqwest::get(u).await?;
let content_length = dl
.content_length()
.ok_or(format!("Cannot get content length for {}", u))?;
let content_headers = dl.headers().clone();
let content_type = content_headers
.get("Content-Type")
.ok_or(format!("Cannot get content type for {}", u))?
.to_str()?;
debug!("Init the slot for uploading media: {}", u);
// init the slot for uploading
let client = Client::new();
let orig_media_id: UploadMediaResponse = client
.post(TWITTER_UPLOAD_MEDIA_URL)
.header(
"Authorization",
oauth1_request::post(
TWITTER_UPLOAD_MEDIA_URL,
&empty_request,
&token,
oauth1_request::HMAC_SHA1,
),
)
.multipart(
Form::new()
.text("command", "INIT")
.text("media_type", content_type.to_owned())
.text("total_bytes", content_length.to_string())
.text("media_category", t.to_string()),
)
.send()
.await?
.json()
.await?;
debug!("Slot initiated with ID: {}", orig_media_id.media_id);
debug!("Appending media to ID: {}", orig_media_id.media_id);
// append the media to the corresponding slot
let mut segment: u8 = 0;
while let Some(chunk) = dl.chunk().await? {
debug!(
"Appending segment {} for media ID {}",
segment, orig_media_id.media_id
);
let chunk_size: u64 = chunk.len().try_into().unwrap();
let res = client
.post(TWITTER_UPLOAD_MEDIA_URL)
.header(
"Authorization",
oauth1_request::post(
TWITTER_UPLOAD_MEDIA_URL,
&empty_request,
&token,
oauth1_request::HMAC_SHA1,
),
)
.multipart(
Form::new()
.text("command", "APPEND")
.text("media_id", orig_media_id.media_id.to_string())
.text("segment_index", segment.to_string())
.part("media", Part::stream_with_length(chunk, chunk_size)),
)
.send()
.await?;
if !res.status().is_success() {
return Err(
OolatoocsError::new(&format!("Cannot upload part {} of {}", segment, u)).into(),
);
}
segment += 1;
}
debug!("Finalize media ID: {}", orig_media_id.media_id);
// Finalizing task
let fin: UploadMediaResponse = client
.post(TWITTER_UPLOAD_MEDIA_URL)
.header(
"Authorization",
oauth1_request::post(
TWITTER_UPLOAD_MEDIA_URL,
&empty_request,
&token,
oauth1_request::HMAC_SHA1,
),
)
.multipart(
Form::new()
.text("command", "FINALIZE")
.text("media_id", orig_media_id.media_id.to_string()),
)
.send()
.await?
.json()
.await?;
if let Some(p_info) = fin.processing_info {
if let Some(wait_sec) = p_info.check_after_secs {
debug!(
"Processing is not finished yet for ID {}, waiting {} secs",
orig_media_id.media_id, wait_sec
);
// getting here, we have a status and a check_after_secs
// this status can be anything but we will check it afterwards
// whatever happens, we can wait here before proceeding
sleep(Duration::from_secs(wait_sec)).await;
let command = UploadMediaCommand {
command: "STATUS".to_string(),
media_id: orig_media_id.media_id.to_string(),
};
loop {
debug!(
"Checking on status for ID {} after waiting {} secs",
orig_media_id.media_id, wait_sec
);
let status: UploadMediaResponse = client
.get(TWITTER_UPLOAD_MEDIA_URL)
.header(
"Authorization",
oauth1_request::get(
TWITTER_UPLOAD_MEDIA_URL,
&command,
&token,
oauth1_request::HMAC_SHA1,
),
)
.query(&command)
.send()
.await?
.json()
.await?;
let p_status = status.processing_info.unwrap(); // shouldnt be None at this point
match p_status.state {
UploadMediaResponseProcessingInfoState::Failed => {
debug!("Processing has failed!");
return Err(OolatoocsError::new(&format!(
"Upload for {} (id: {}) has failed",
u, orig_media_id.media_id
))
.into());
}
UploadMediaResponseProcessingInfoState::Succeeded => {
debug!("Processing has succeeded, exiting loop!");
break;
}
UploadMediaResponseProcessingInfoState::Pending
| UploadMediaResponseProcessingInfoState::InProgress => {
debug!(
"Processing still pending, waiting {} secs more…",
p_status.check_after_secs.unwrap() // unwrap is safe here,
// check_after_secs is only present
// when status is pending
);
sleep(Duration::from_secs(p_status.check_after_secs.unwrap())).await;
continue;
}
}
}
}
}
Ok(orig_media_id.media_id)
} }
/// This posts Tweets with all the associated medias /// This posts Tweets with all the associated medias
pub async fn post_tweet( pub async fn post_tweet(
config: &TwitterConfig, config: &TwitterConfig,
content: &str, content: &str,
_medias: &[u64], medias: &[u64],
) -> Result<u64, Box<dyn Error>> { ) -> Result<u64, Box<dyn Error>> {
let uri = "https://api.twitter.com/2/tweets";
let empty_request = EmptyRequest {}; // Why? Because fuck you, thats why! let empty_request = EmptyRequest {}; // Why? Because fuck you, thats why!
let token = get_token(config); let token = get_token(config);
let tweet = Tweet { let tweet = Tweet {
text: content.to_string(), text: content.to_string(),
media: TweetMediasIds {
media_ids: medias.iter().map(|m| m.to_string()).collect(),
},
}; };
let client = Client::new(); let client = Client::new();
let res = client let res = client
.post(uri) .post(TWITTER_API_TWEET_URL)
.header( .header(
"Authorization", "Authorization",
oauth1_request::post(uri, &empty_request, &token, oauth1_request::HMAC_SHA1), oauth1_request::post(
TWITTER_API_TWEET_URL,
&empty_request,
&token,
oauth1_request::HMAC_SHA1,
),
) )
.json(&tweet) .json(&tweet)
.send() .send()