feat: add copyprofile subcommand

This commit is contained in:
VC
2022-12-01 08:27:44 +01:00
parent 25f98581a5
commit 0f5ab4158c
5 changed files with 156 additions and 11 deletions

View File

@@ -13,7 +13,7 @@ mod twitter;
use twitter::*;
mod util;
use crate::util::generate_media_ids;
use util::{base64_media, generate_media_ids};
mod state;
pub use state::{init_db, migrate_db};
@@ -23,7 +23,9 @@ use futures::StreamExt;
use html_escape::decode_html_entities;
use isolang::Language;
use log::info;
use megalodon::{megalodon::PostStatusInputOptions, Megalodon};
use megalodon::{
megalodon::PostStatusInputOptions, megalodon::UpdateCredentialsInputOptions, Megalodon,
};
use regex::Regex;
use rusqlite::Connection;
use std::sync::Arc;
@@ -242,3 +244,62 @@ pub async fn run(config: Config) {
}
}
}
/// Copies the Twitter profile into Mastodon
#[tokio::main]
pub async fn profile(config: Config, bot: Option<bool>) {
let mut stream = futures::stream::iter(config.mastodon.into_values())
.map(|mastodon_config| {
let token = get_oauth2_token(&config.twitter);
spawn(async move {
// get the user of the last tweet of the feed
let twitter_user =
get_user_timeline(&mastodon_config.twitter_screen_name, &token, None, 1)
.await?
.first()
.ok_or_else(|| ScootalooError::new("Cant extract a tweet from the feed!"))?
.clone()
.user
.ok_or_else(|| ScootalooError::new("No user in Tweet!"))?;
let mut display_name = twitter_user.name.clone();
display_name.truncate(30);
let header = match twitter_user.profile_banner_url {
Some(h) => Some(base64_media(&h).await?),
None => None,
};
let update_creds = UpdateCredentialsInputOptions {
discoverable: None,
bot,
display_name: Some(display_name),
note: twitter_user.description,
avatar: Some(
base64_media(&twitter_user.profile_image_url_https.replace("_normal", ""))
.await?,
),
header,
locked: None,
source: None,
fields_attributes: None,
};
let mastodon = get_mastodon_token(&mastodon_config);
mastodon.update_credentials(Some(&update_creds)).await?;
Ok::<(), ScootalooError>(())
})
})
.buffer_unordered(config.scootaloo.rate_limit.unwrap_or(DEFAULT_RATE_LIMIT));
while let Some(result) = stream.next().await {
match result {
Ok(Err(e)) => eprintln!("Error within thread: {}", e),
Err(e) => eprintln!("Error with thread: {}", e),
_ => (),
}
}
}