mirror of
https://framagit.org/veretcle/scootaloo.git
synced 2025-07-20 17:11:19 +02:00
Adaptation to elefren instead of Mammut
This commit is contained in:
62
src/lib.rs
62
src/lib.rs
@@ -27,15 +27,11 @@ use egg_mode::{
|
||||
},
|
||||
};
|
||||
|
||||
// mammut
|
||||
use mammut::{
|
||||
Mastodon,
|
||||
Data,
|
||||
Registration,
|
||||
apps::{AppBuilder, Scopes},
|
||||
status_builder::StatusBuilder,
|
||||
media_builder::MediaBuilder,
|
||||
};
|
||||
// elefren
|
||||
use elefren::prelude::*;
|
||||
use elefren::apps::App;
|
||||
use elefren::status_builder::StatusBuilder;
|
||||
use elefren::scopes::Scopes;
|
||||
|
||||
// reqwest
|
||||
use reqwest::Client;
|
||||
@@ -148,11 +144,11 @@ fn get_mastodon_token(masto: &MastodonConfig) -> Mastodon {
|
||||
token: Cow::from(String::from(&masto.token)),
|
||||
};
|
||||
|
||||
Mastodon::from_data(data)
|
||||
Mastodon::from(data)
|
||||
}
|
||||
|
||||
/// build toot from tweet
|
||||
fn build_basic_status(tweet: &Tweet) -> Result<StatusBuilder, Box<dyn Error>> {
|
||||
/// build toot text from tweet
|
||||
fn build_basic_status(tweet: &Tweet) -> Result<String, Box<dyn Error>> {
|
||||
let mut toot = String::from(&tweet.text);
|
||||
|
||||
let decoded_urls = decode_urls(&tweet.entities.urls);
|
||||
@@ -171,7 +167,7 @@ fn build_basic_status(tweet: &Tweet) -> Result<StatusBuilder, Box<dyn Error>> {
|
||||
toot = t;
|
||||
}
|
||||
|
||||
Ok(StatusBuilder::new(toot))
|
||||
Ok(toot)
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -289,16 +285,16 @@ pub fn parse_toml(toml_file: &str) -> Config {
|
||||
/// As this function is supposed to be run only once, it will panic for every error it encounters
|
||||
/// Most of this function is a direct copy/paste of the official `mammut` crate
|
||||
pub fn register(host: &str) {
|
||||
let app = AppBuilder {
|
||||
client_name: env!("CARGO_PKG_NAME"),
|
||||
redirect_uris: "urn:ietf:wg:oauth:2.0:oob",
|
||||
scopes: Scopes::Write,
|
||||
website: Some("https://framagit.org/veretcle/scootaloo"),
|
||||
};
|
||||
let mut builder = App::builder();
|
||||
builder.client_name(Cow::from(String::from(env!("CARGO_PKG_NAME"))))
|
||||
.redirect_uris(Cow::from(String::from("urn:ietf:wg:oauth:2.0:oob")))
|
||||
.scopes(Scopes::write_all())
|
||||
.website(Cow::from(String::from("https://framagit.org/veretcle/scootaloo")));
|
||||
|
||||
let mut registration = Registration::new(host);
|
||||
registration.register(app).expect("Registration failed!");
|
||||
let url = registration.authorise().expect("Cannot generate registration URI!");
|
||||
let app = builder.build().expect("Cannot build the app");
|
||||
|
||||
let registration = Registration::new(host).register(app).expect("Cannot build registration object");
|
||||
let url = registration.authorize_url().expect("Cannot generate registration URI!");
|
||||
|
||||
println!("Click this link to authorize on Mastodon: {}", url);
|
||||
println!("Paste the returned authorization code: ");
|
||||
@@ -307,7 +303,7 @@ pub fn register(host: &str) {
|
||||
stdin().read_line(&mut input).expect("Unable to read back registration code!");
|
||||
|
||||
let code = input.trim();
|
||||
let mastodon = registration.create_access_token(code.to_string()).expect("Unable to create access token!");
|
||||
let mastodon = registration.complete(code).expect("Unable to create access token!");
|
||||
|
||||
let toml = toml::to_string(&*mastodon).unwrap();
|
||||
|
||||
@@ -349,7 +345,7 @@ pub fn run(config: Config) {
|
||||
};
|
||||
|
||||
// build basic status by just yielding text and dereferencing contained urls
|
||||
let mut status = match build_basic_status(tweet) {
|
||||
let mut status_text = match build_basic_status(tweet) {
|
||||
Ok(t) => t,
|
||||
Err(e) => {
|
||||
println!("Could not create status from tweet {}: {}", tweet.id ,e);
|
||||
@@ -357,6 +353,8 @@ pub fn run(config: Config) {
|
||||
},
|
||||
};
|
||||
|
||||
let mut status_medias: Vec<String> = vec![];
|
||||
|
||||
// reupload the attachments if any
|
||||
if let Some(m) = &tweet.extended_entities {
|
||||
for media in &m.media {
|
||||
@@ -368,7 +366,7 @@ pub fn run(config: Config) {
|
||||
},
|
||||
};
|
||||
|
||||
let mastodon_media_ids = match mastodon.media(MediaBuilder::new(Cow::from(String::from(&local_tweet_media_path)))) {
|
||||
let mastodon_media_ids = match mastodon.media(Cow::from(String::from(&local_tweet_media_path))) {
|
||||
Ok(m) => {
|
||||
remove_file(&local_tweet_media_path).unwrap_or_else(|e|
|
||||
println!("Attachment for {} has been upload, but I’m unable to remove the existing file: {}", &local_tweet_media_path, e)
|
||||
@@ -381,17 +379,19 @@ pub fn run(config: Config) {
|
||||
}
|
||||
};
|
||||
|
||||
// media has been successfully uploaded, adding it to the toot
|
||||
match status.media_ids {
|
||||
Some(ref mut i) => i.push(mastodon_media_ids),
|
||||
None => status.media_ids = Some(vec![mastodon_media_ids]),
|
||||
};
|
||||
status_medias.push(mastodon_media_ids);
|
||||
|
||||
// last step, removing the reference to the media from with the toot’s text
|
||||
status.status = status.status.replace(&media.url, "");
|
||||
status_text = status_text.replace(&media.url, "");
|
||||
}
|
||||
}
|
||||
|
||||
let status = StatusBuilder::new()
|
||||
.status(&status_text)
|
||||
.media_ids(status_medias)
|
||||
.build()
|
||||
.expect(format!("Cannot build status with text {}", &status_text).as_str());
|
||||
|
||||
// publish status
|
||||
mastodon.new_status(status).unwrap();
|
||||
|
||||
|
Reference in New Issue
Block a user