mirror of
https://framagit.org/veretcle/scootaloo.git
synced 2025-07-20 17:11:19 +02:00
feat: add copyprofile subcommand
This commit is contained in:
65
src/lib.rs
65
src/lib.rs
@@ -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("Can’t 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),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user