From 50e27034d8947ae432d4e1c9082c0f510815498c Mon Sep 17 00:00:00 2001 From: VC Date: Tue, 19 May 2020 15:21:23 +0200 Subject: [PATCH 1/3] Adaptation to elefren instead of Mammut --- Cargo.lock | 279 +++++++++++++++++++++++++++++++++++++++++++++-------- Cargo.toml | 4 +- src/lib.rs | 62 ++++++------ 3 files changed, 274 insertions(+), 71 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6c4a08c..0a528e0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -69,6 +69,16 @@ dependencies = [ "libc", ] +[[package]] +name = "base64" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "489d6c0ed21b11d038c31b6ceccca973e65d73ba3bd8ecb9a2babf5546164643" +dependencies = [ + "byteorder", + "safemem", +] + [[package]] name = "base64" version = "0.10.1" @@ -117,6 +127,12 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" +[[package]] +name = "bytecount" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b92204551573580e078dc80017f36a213eb77a0450e4ddd8cfa0f3f2d1f0178f" + [[package]] name = "byteorder" version = "1.3.4" @@ -149,6 +165,19 @@ dependencies = [ "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]] name = "cc" version = "1.0.50" @@ -307,6 +336,12 @@ dependencies = [ "subtle", ] +[[package]] +name = "data-encoding" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11c0346158a19b3627234e15596f5e465c360fcdb97d817bcb255e0510f5a788" + [[package]] name = "derive_more" version = "0.99.7" @@ -372,6 +407,29 @@ version = "1.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "encoding_rs" version = "0.8.22" @@ -387,6 +445,7 @@ version = "0.12.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d371106cc88ffdfb1eabd7111e432da544f16f3e2d7bf1dfe8bf575f1df045cd" dependencies = [ + "backtrace", "version_check 0.9.1", ] @@ -604,6 +663,12 @@ dependencies = [ "wasi", ] +[[package]] +name = "glob" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" + [[package]] name = "h2" version = "0.1.26" @@ -770,6 +835,23 @@ dependencies = [ "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]] name = "hyper-tls" version = "0.3.2" @@ -796,25 +878,6 @@ dependencies = [ "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]] name = "idna" version = "0.1.5" @@ -855,6 +918,17 @@ dependencies = [ "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]] name = "itoa" version = "0.4.5" @@ -907,23 +981,6 @@ dependencies = [ "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]] name = "matches" version = "0.1.8" @@ -1148,6 +1205,44 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "pin-project" version = "0.4.8" @@ -1226,6 +1321,15 @@ dependencies = [ "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]] name = "quote" version = "1.0.2" @@ -1235,6 +1339,19 @@ dependencies = [ "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]] name = "rand" version = "0.6.5" @@ -1479,6 +1596,21 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "schannel" version = "0.1.17" @@ -1491,12 +1623,12 @@ dependencies = [ [[package]] name = "scootaloo" -version = "0.1.9" +version = "0.2.0" dependencies = [ "clap", "egg-mode", + "elefren", "htmlescape", - "mammut", "reqwest", "serde", "tokio 0.2.13", @@ -1537,6 +1669,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" dependencies = [ "semver-parser", + "serde", ] [[package]] @@ -1576,6 +1709,18 @@ dependencies = [ "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]] name = "serde_urlencoded" version = "0.5.5" @@ -1600,6 +1745,28 @@ dependencies = [ "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]] name = "slab" version = "0.4.2" @@ -1665,6 +1832,22 @@ dependencies = [ "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]] name = "tempfile" version = "3.1.0" @@ -2043,6 +2226,17 @@ version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "want" version = "0.2.0" @@ -2098,6 +2292,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 85c4a9f..e937f5e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "scootaloo" -version = "0.1.9" +version = "0.2.0" authors = ["VC "] edition = "2018" @@ -14,7 +14,7 @@ clap = "^2.33" tokio = "^0.2" egg-mode = "^0.14" -mammut = "^0.13" +elefren = "^0.20" reqwest = "^0.9" diff --git a/src/lib.rs b/src/lib.rs index 5b7f75e..aa2eda5 100644 --- a/src/lib.rs +++ b/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> { +/// build toot text from tweet +fn build_basic_status(tweet: &Tweet) -> Result> { 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> { 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 = 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(); From eb0213eeac7627c115f96a1e62713f5ec30c08b5 Mon Sep 17 00:00:00 2001 From: VC Date: Tue, 19 May 2020 15:36:01 +0200 Subject: [PATCH 2/3] Add some comment on the fact that the NewStatus can fail --- src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index aa2eda5..98a371f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -394,6 +394,8 @@ pub fn run(config: Config) { // publish status mastodon.new_status(status).unwrap(); + // this will panic if it cannot publish the status, which is a good thing, it allows the + // last_tweet gathered not to be written // write the current state (tweet ID) to avoid copying it another time write_state(&config.scootaloo.last_tweet_path, tweet.id).unwrap_or_else(|e| From 11e94d6a3ebbe80ca4344dd22b0031ece947681b Mon Sep 17 00:00:00 2001 From: VC Date: Tue, 19 May 2020 15:43:05 +0200 Subject: [PATCH 3/3] elefren use --- src/lib.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 98a371f..07ab08b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,10 +28,12 @@ use egg_mode::{ }; // elefren -use elefren::prelude::*; -use elefren::apps::App; -use elefren::status_builder::StatusBuilder; -use elefren::scopes::Scopes; +use elefren::{ + prelude::*, + apps::App, + status_builder::StatusBuilder, + scopes::Scopes, +}; // reqwest use reqwest::Client;