Adaptation to elefren instead of Mammut

This commit is contained in:
VC
2020-05-19 15:21:23 +02:00
parent a8f0817121
commit 50e27034d8
3 changed files with 274 additions and 71 deletions

279
Cargo.lock generated
View File

@@ -69,6 +69,16 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "base64"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "489d6c0ed21b11d038c31b6ceccca973e65d73ba3bd8ecb9a2babf5546164643"
dependencies = [
"byteorder",
"safemem",
]
[[package]] [[package]]
name = "base64" name = "base64"
version = "0.10.1" version = "0.10.1"
@@ -117,6 +127,12 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7"
[[package]]
name = "bytecount"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b92204551573580e078dc80017f36a213eb77a0450e4ddd8cfa0f3f2d1f0178f"
[[package]] [[package]]
name = "byteorder" name = "byteorder"
version = "1.3.4" version = "1.3.4"
@@ -149,6 +165,19 @@ dependencies = [
"ppv-lite86", "ppv-lite86",
] ]
[[package]]
name = "cargo_metadata"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5d1b4d380e1bab994591a24c2bdd1b054f64b60bef483a8c598c7c345bc3bbe"
dependencies = [
"error-chain",
"semver",
"serde",
"serde_derive",
"serde_json",
]
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.0.50" version = "1.0.50"
@@ -307,6 +336,12 @@ dependencies = [
"subtle", "subtle",
] ]
[[package]]
name = "data-encoding"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "11c0346158a19b3627234e15596f5e465c360fcdb97d817bcb255e0510f5a788"
[[package]] [[package]]
name = "derive_more" name = "derive_more"
version = "0.99.7" version = "0.99.7"
@@ -372,6 +407,29 @@ version = "1.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3" checksum = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3"
[[package]]
name = "elefren"
version = "0.20.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfca8d8d0147086081224e22183a37a7b98e3230b945a717f1b5a0eed5fb07af"
dependencies = [
"chrono",
"doc-comment",
"hyper-old-types",
"isolang",
"log",
"reqwest",
"serde",
"serde_derive",
"serde_json",
"serde_qs",
"serde_urlencoded",
"skeptic",
"tap-reader",
"try_from",
"url 1.7.2",
]
[[package]] [[package]]
name = "encoding_rs" name = "encoding_rs"
version = "0.8.22" version = "0.8.22"
@@ -387,6 +445,7 @@ version = "0.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d371106cc88ffdfb1eabd7111e432da544f16f3e2d7bf1dfe8bf575f1df045cd" checksum = "d371106cc88ffdfb1eabd7111e432da544f16f3e2d7bf1dfe8bf575f1df045cd"
dependencies = [ dependencies = [
"backtrace",
"version_check 0.9.1", "version_check 0.9.1",
] ]
@@ -604,6 +663,12 @@ dependencies = [
"wasi", "wasi",
] ]
[[package]]
name = "glob"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb"
[[package]] [[package]]
name = "h2" name = "h2"
version = "0.1.26" version = "0.1.26"
@@ -770,6 +835,23 @@ dependencies = [
"want 0.3.0", "want 0.3.0",
] ]
[[package]]
name = "hyper-old-types"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6896be51ecf3966c0fa14ff2da3233dbb9aef57ccea1be1afe55f105f4d4c9c4"
dependencies = [
"base64 0.9.3",
"bytes 0.4.12",
"httparse",
"language-tags",
"log",
"mime",
"percent-encoding 1.0.1",
"time",
"unicase",
]
[[package]] [[package]]
name = "hyper-tls" name = "hyper-tls"
version = "0.3.2" version = "0.3.2"
@@ -796,25 +878,6 @@ dependencies = [
"tokio-tls", "tokio-tls",
] ]
[[package]]
name = "hyperx"
version = "0.15.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad5125567a8328ced412a29a52a3ca0a2520c617d40a8bee62d12d877eec236c"
dependencies = [
"base64 0.10.1",
"bytes 0.4.12",
"cfg-if",
"http 0.1.21",
"httparse",
"language-tags",
"log",
"mime",
"percent-encoding 1.0.1",
"time",
"unicase",
]
[[package]] [[package]]
name = "idna" name = "idna"
version = "0.1.5" version = "0.1.5"
@@ -855,6 +918,17 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "isolang"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "265ef164908329e47e753c769b14cbb27434abf0c41984dca201484022f09ce5"
dependencies = [
"phf",
"phf_codegen",
"serde",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "0.4.5" version = "0.4.5"
@@ -907,23 +981,6 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "mammut"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1771d6ce236f93ec0bc3086945a189c94953e59edc0a6908fae36019d53c8220"
dependencies = [
"chrono",
"doc-comment",
"hyperx",
"log",
"reqwest",
"serde",
"serde_derive",
"serde_json",
"url 1.7.2",
]
[[package]] [[package]]
name = "matches" name = "matches"
version = "0.1.8" version = "0.1.8"
@@ -1148,6 +1205,44 @@ version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
[[package]]
name = "phf"
version = "0.7.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3da44b85f8e8dfaec21adae67f95d93244b2ecf6ad2a692320598dcc8e6dd18"
dependencies = [
"phf_shared",
]
[[package]]
name = "phf_codegen"
version = "0.7.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b03e85129e324ad4166b06b2c7491ae27fe3ec353af72e72cd1654c7225d517e"
dependencies = [
"phf_generator",
"phf_shared",
]
[[package]]
name = "phf_generator"
version = "0.7.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09364cc93c159b8b06b1f4dd8a4398984503483891b0c26b867cf431fb132662"
dependencies = [
"phf_shared",
"rand 0.6.5",
]
[[package]]
name = "phf_shared"
version = "0.7.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "234f71a15de2288bcb7e3b6515828d22af7ec8598ee6d24c3b526fa0a80b67a0"
dependencies = [
"siphasher",
]
[[package]] [[package]]
name = "pin-project" name = "pin-project"
version = "0.4.8" version = "0.4.8"
@@ -1226,6 +1321,15 @@ dependencies = [
"url 2.1.1", "url 2.1.1",
] ]
[[package]]
name = "pulldown-cmark"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eef52fac62d0ea7b9b4dc7da092aa64ea7ec3d90af6679422d3d7e0e14b6ee15"
dependencies = [
"bitflags",
]
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.2" version = "1.0.2"
@@ -1235,6 +1339,19 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "rand"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293"
dependencies = [
"fuchsia-cprng",
"libc",
"rand_core 0.3.1",
"rdrand",
"winapi 0.3.8",
]
[[package]] [[package]]
name = "rand" name = "rand"
version = "0.6.5" version = "0.6.5"
@@ -1479,6 +1596,21 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfa8506c1de11c9c4e4c38863ccbe02a305c8188e85a05a784c9e11e1c3910c8" checksum = "bfa8506c1de11c9c4e4c38863ccbe02a305c8188e85a05a784c9e11e1c3910c8"
[[package]]
name = "safemem"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef703b7cb59335eae2eb93ceb664c0eb7ea6bf567079d843e09420219668e072"
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]] [[package]]
name = "schannel" name = "schannel"
version = "0.1.17" version = "0.1.17"
@@ -1491,12 +1623,12 @@ dependencies = [
[[package]] [[package]]
name = "scootaloo" name = "scootaloo"
version = "0.1.9" version = "0.2.0"
dependencies = [ dependencies = [
"clap", "clap",
"egg-mode", "egg-mode",
"elefren",
"htmlescape", "htmlescape",
"mammut",
"reqwest", "reqwest",
"serde", "serde",
"tokio 0.2.13", "tokio 0.2.13",
@@ -1537,6 +1669,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
dependencies = [ dependencies = [
"semver-parser", "semver-parser",
"serde",
] ]
[[package]] [[package]]
@@ -1576,6 +1709,18 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "serde_qs"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "35965fa1d2413717053d67c2df1f5c3e1763fbf77200ea7e767523707bd5a0af"
dependencies = [
"data-encoding",
"error-chain",
"percent-encoding 1.0.1",
"serde",
]
[[package]] [[package]]
name = "serde_urlencoded" name = "serde_urlencoded"
version = "0.5.5" version = "0.5.5"
@@ -1600,6 +1745,28 @@ dependencies = [
"opaque-debug", "opaque-debug",
] ]
[[package]]
name = "siphasher"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b8de496cf83d4ed58b6be86c3a275b8602f6ffe98d3024a869e124147a9a3ac"
[[package]]
name = "skeptic"
version = "0.13.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6fb8ed853fdc19ce09752d63f3a2e5b5158aeb261520cd75eb618bd60305165"
dependencies = [
"bytecount",
"cargo_metadata",
"error-chain",
"glob",
"pulldown-cmark",
"serde_json",
"tempdir",
"walkdir",
]
[[package]] [[package]]
name = "slab" name = "slab"
version = "0.4.2" version = "0.4.2"
@@ -1665,6 +1832,22 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "tap-reader"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f13fc301d415a8cd4529ba679720c59f07369bcff573618a6e8d5afebefb6f3"
[[package]]
name = "tempdir"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8"
dependencies = [
"rand 0.4.6",
"remove_dir_all",
]
[[package]] [[package]]
name = "tempfile" name = "tempfile"
version = "3.1.0" version = "3.1.0"
@@ -2043,6 +2226,17 @@ version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "078775d0255232fb988e6fccf26ddc9d1ac274299aaedcedce21c6f72cc533ce" checksum = "078775d0255232fb988e6fccf26ddc9d1ac274299aaedcedce21c6f72cc533ce"
[[package]]
name = "walkdir"
version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "777182bc735b6424e1a57516d35ed72cb8019d85c8c9bf536dccb3445c1a2f7d"
dependencies = [
"same-file",
"winapi 0.3.8",
"winapi-util",
]
[[package]] [[package]]
name = "want" name = "want"
version = "0.2.0" version = "0.2.0"
@@ -2098,6 +2292,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi 0.3.8",
]
[[package]] [[package]]
name = "winapi-x86_64-pc-windows-gnu" name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0" version = "0.4.0"

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "scootaloo" name = "scootaloo"
version = "0.1.9" version = "0.2.0"
authors = ["VC <veretcle+framagit@mateu.be>"] authors = ["VC <veretcle+framagit@mateu.be>"]
edition = "2018" edition = "2018"
@@ -14,7 +14,7 @@ clap = "^2.33"
tokio = "^0.2" tokio = "^0.2"
egg-mode = "^0.14" egg-mode = "^0.14"
mammut = "^0.13" elefren = "^0.20"
reqwest = "^0.9" reqwest = "^0.9"

View File

@@ -27,15 +27,11 @@ use egg_mode::{
}, },
}; };
// mammut // elefren
use mammut::{ use elefren::prelude::*;
Mastodon, use elefren::apps::App;
Data, use elefren::status_builder::StatusBuilder;
Registration, use elefren::scopes::Scopes;
apps::{AppBuilder, Scopes},
status_builder::StatusBuilder,
media_builder::MediaBuilder,
};
// reqwest // reqwest
use reqwest::Client; use reqwest::Client;
@@ -148,11 +144,11 @@ fn get_mastodon_token(masto: &MastodonConfig) -> Mastodon {
token: Cow::from(String::from(&masto.token)), token: Cow::from(String::from(&masto.token)),
}; };
Mastodon::from_data(data) Mastodon::from(data)
} }
/// build toot from tweet /// build toot text from tweet
fn build_basic_status(tweet: &Tweet) -> Result<StatusBuilder, Box<dyn Error>> { fn build_basic_status(tweet: &Tweet) -> Result<String, Box<dyn Error>> {
let mut toot = String::from(&tweet.text); let mut toot = String::from(&tweet.text);
let decoded_urls = decode_urls(&tweet.entities.urls); 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; 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 /// 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 /// Most of this function is a direct copy/paste of the official `mammut` crate
pub fn register(host: &str) { pub fn register(host: &str) {
let app = AppBuilder { let mut builder = App::builder();
client_name: env!("CARGO_PKG_NAME"), builder.client_name(Cow::from(String::from(env!("CARGO_PKG_NAME"))))
redirect_uris: "urn:ietf:wg:oauth:2.0:oob", .redirect_uris(Cow::from(String::from("urn:ietf:wg:oauth:2.0:oob")))
scopes: Scopes::Write, .scopes(Scopes::write_all())
website: Some("https://framagit.org/veretcle/scootaloo"), .website(Cow::from(String::from("https://framagit.org/veretcle/scootaloo")));
};
let mut registration = Registration::new(host); let app = builder.build().expect("Cannot build the app");
registration.register(app).expect("Registration failed!");
let url = registration.authorise().expect("Cannot generate registration URI!"); 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!("Click this link to authorize on Mastodon: {}", url);
println!("Paste the returned authorization code: "); 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!"); stdin().read_line(&mut input).expect("Unable to read back registration code!");
let code = input.trim(); 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(); 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 // 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, Ok(t) => t,
Err(e) => { Err(e) => {
println!("Could not create status from tweet {}: {}", tweet.id ,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 // reupload the attachments if any
if let Some(m) = &tweet.extended_entities { if let Some(m) = &tweet.extended_entities {
for media in &m.media { 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) => { Ok(m) => {
remove_file(&local_tweet_media_path).unwrap_or_else(|e| remove_file(&local_tweet_media_path).unwrap_or_else(|e|
println!("Attachment for {} has been upload, but Im unable to remove the existing file: {}", &local_tweet_media_path, e) println!("Attachment for {} has been upload, but Im 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 status_medias.push(mastodon_media_ids);
match status.media_ids {
Some(ref mut i) => i.push(mastodon_media_ids),
None => status.media_ids = Some(vec![mastodon_media_ids]),
};
// last step, removing the reference to the media from with the toots text // last step, removing the reference to the media from with the toots 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 // publish status
mastodon.new_status(status).unwrap(); mastodon.new_status(status).unwrap();