mirror of
https://framagit.org/veretcle/scootaloo.git
synced 2025-07-21 17:34:37 +02:00
Compare commits
40 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
a854243cf6 | ||
![]() |
b33ffa4401 | ||
![]() |
77941e0b9a | ||
![]() |
1489f89bdb | ||
![]() |
93a27deae8 | ||
![]() |
fe3745d91f | ||
![]() |
9a1e4c8e6c | ||
![]() |
8b12f83c5d | ||
![]() |
f93bb5158b | ||
![]() |
d5db8b0d85 | ||
![]() |
fe8e81b54d | ||
![]() |
636ea8c85e | ||
![]() |
b3e7ee9d84 | ||
![]() |
7f7219ea78 | ||
![]() |
f371b8a297 | ||
![]() |
ec3956eabb | ||
![]() |
ce84c05581 | ||
![]() |
b64621368b | ||
![]() |
89de1cf7a3 | ||
![]() |
ffbe98f838 | ||
![]() |
822f4044c6 | ||
![]() |
78924f6eeb | ||
![]() |
9c14636735 | ||
![]() |
01bac63fb9 | ||
![]() |
4f5663b450 | ||
![]() |
9a9c4b4809 | ||
![]() |
9970968b47 | ||
![]() |
291c86677e | ||
![]() |
31afb1cf7d | ||
![]() |
4415c4ac12 | ||
![]() |
89f1372f9f | ||
![]() |
06904434c8 | ||
![]() |
3c64df23bc | ||
![]() |
c62f67c3b3 | ||
![]() |
3b0e7234af | ||
![]() |
62011b4b81 | ||
![]() |
5ce3bde3e7 | ||
![]() |
ab4184c0ed | ||
![]() |
de758c7bda | ||
![]() |
df75520175 |
1236
Cargo.lock
generated
1236
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "scootaloo"
|
name = "scootaloo"
|
||||||
version = "0.7.0"
|
version = "0.9.3"
|
||||||
authors = ["VC <veretcle+framagit@mateu.be>"]
|
authors = ["VC <veretcle+framagit@mateu.be>"]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
@@ -10,11 +10,11 @@ edition = "2021"
|
|||||||
chrono = "^0.4"
|
chrono = "^0.4"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
toml = "^0.5"
|
toml = "^0.5"
|
||||||
clap = "^2.34"
|
clap = "^4"
|
||||||
futures = "^0.3"
|
|
||||||
egg-mode = "^0.16"
|
egg-mode = "^0.16"
|
||||||
rusqlite = "^0.27"
|
rusqlite = "^0.27"
|
||||||
tokio = { version = "1", features = ["full"]}
|
tokio = { version = "^1", features = ["full"]}
|
||||||
|
futures = "^0.3"
|
||||||
elefren = "^0.22"
|
elefren = "^0.22"
|
||||||
html-escape = "^0.2"
|
html-escape = "^0.2"
|
||||||
reqwest = "^0.11"
|
reqwest = "^0.11"
|
||||||
|
@@ -18,9 +18,11 @@ First up, create a configuration file (default path is `/usr/local/etc/scootaloo
|
|||||||
[scootaloo]
|
[scootaloo]
|
||||||
db_path = "/var/lib/scootaloo/scootaloo.sqlite" ## file containing the SQLite Tweet corresponding Toot DB, must be writeable
|
db_path = "/var/lib/scootaloo/scootaloo.sqlite" ## file containing the SQLite Tweet corresponding Toot DB, must be writeable
|
||||||
cache_path = "/tmp/scootaloo" ## a dir where the temporary files will be download, must be writeable
|
cache_path = "/tmp/scootaloo" ## a dir where the temporary files will be download, must be writeable
|
||||||
|
rate_limiting = 4 ## optional, default 4, number of accounts handled simultaneously
|
||||||
|
|
||||||
[twitter]
|
[twitter]
|
||||||
## Consumer/Access key for Twitter (can be generated at https://developer.twitter.com/en/apps)
|
## Consumer/Access key for Twitter (can be generated at https://developer.twitter.com/en/apps)
|
||||||
|
page_size = 20 ## optional, default 200, max number of tweet retrieved
|
||||||
consumer_key = "MYCONSUMERKEY"
|
consumer_key = "MYCONSUMERKEY"
|
||||||
consumer_secret = "MYCONSUMERSECRET"
|
consumer_secret = "MYCONSUMERSECRET"
|
||||||
access_key = "MYACCESSKEY"
|
access_key = "MYACCESSKEY"
|
||||||
@@ -55,6 +57,11 @@ token = "MYTOKEN"
|
|||||||
|
|
||||||
You can add other account if you like, after the `[mastodon]` moniker. Scootaloo would theorically support an unlimited number of accounts.
|
You can add other account if you like, after the `[mastodon]` moniker. Scootaloo would theorically support an unlimited number of accounts.
|
||||||
|
|
||||||
|
You can also add a custom twitter page size in this section that would override the global (under the `twitter` moniker) and default one (200), like so:
|
||||||
|
```
|
||||||
|
twitter_page_size = 40
|
||||||
|
```
|
||||||
|
|
||||||
## Running
|
## Running
|
||||||
|
|
||||||
You can then run the application via `cron` for example. Here is the generic usage:
|
You can then run the application via `cron` for example. Here is the generic usage:
|
||||||
|
@@ -16,11 +16,13 @@ pub struct TwitterConfig {
|
|||||||
pub consumer_secret: String,
|
pub consumer_secret: String,
|
||||||
pub access_key: String,
|
pub access_key: String,
|
||||||
pub access_secret: String,
|
pub access_secret: String,
|
||||||
|
pub page_size: Option<i32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct MastodonConfig {
|
pub struct MastodonConfig {
|
||||||
pub twitter_screen_name: String,
|
pub twitter_screen_name: String,
|
||||||
|
pub twitter_page_size: Option<i32>,
|
||||||
pub base: String,
|
pub base: String,
|
||||||
pub client_id: String,
|
pub client_id: String,
|
||||||
pub client_secret: String,
|
pub client_secret: String,
|
||||||
@@ -32,6 +34,7 @@ pub struct MastodonConfig {
|
|||||||
pub struct ScootalooConfig {
|
pub struct ScootalooConfig {
|
||||||
pub db_path: String,
|
pub db_path: String,
|
||||||
pub cache_path: String,
|
pub cache_path: String,
|
||||||
|
pub rate_limit: Option<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parses the TOML file into a Config Struct
|
/// Parses the TOML file into a Config Struct
|
||||||
|
16
src/error.rs
16
src/error.rs
@@ -1,8 +1,12 @@
|
|||||||
use std::{
|
use std::{
|
||||||
|
boxed::Box,
|
||||||
|
convert::From,
|
||||||
error::Error,
|
error::Error,
|
||||||
fmt::{Display, Formatter, Result},
|
fmt::{Display, Formatter, Result},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use elefren::Error as elefrenError;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ScootalooError {
|
pub struct ScootalooError {
|
||||||
details: String,
|
details: String,
|
||||||
@@ -23,3 +27,15 @@ impl Display for ScootalooError {
|
|||||||
write!(f, "{}", self.details)
|
write!(f, "{}", self.details)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<Box<dyn Error>> for ScootalooError {
|
||||||
|
fn from(error: Box<dyn Error>) -> Self {
|
||||||
|
ScootalooError::new(&format!("Error in a subset crate: {}", error))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<elefrenError> for ScootalooError {
|
||||||
|
fn from(error: elefrenError) -> Self {
|
||||||
|
ScootalooError::new(&format!("Error in elefren crate: {}", error))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
176
src/lib.rs
176
src/lib.rs
@@ -13,124 +13,94 @@ mod twitter;
|
|||||||
use twitter::*;
|
use twitter::*;
|
||||||
|
|
||||||
mod util;
|
mod util;
|
||||||
|
use crate::util::generate_media_ids;
|
||||||
|
|
||||||
mod state;
|
mod state;
|
||||||
pub use state::{init_db, migrate_db};
|
pub use state::{init_db, migrate_db};
|
||||||
use state::{read_state, write_state, TweetToToot};
|
use state::{read_state, write_state, TweetToToot};
|
||||||
|
|
||||||
use elefren::{prelude::*, status_builder::StatusBuilder};
|
use elefren::{prelude::*, status_builder::StatusBuilder};
|
||||||
use log::{debug, error, info, warn};
|
use log::info;
|
||||||
use rusqlite::Connection;
|
use rusqlite::Connection;
|
||||||
use std::borrow::Cow;
|
use std::sync::Arc;
|
||||||
use tokio::fs::remove_file;
|
use tokio::{spawn, sync::Mutex};
|
||||||
|
|
||||||
|
use futures::StreamExt;
|
||||||
|
|
||||||
|
const DEFAULT_RATE_LIMIT: usize = 4;
|
||||||
|
const DEFAULT_PAGE_SIZE: i32 = 200;
|
||||||
|
|
||||||
/// This is where the magic happens
|
/// This is where the magic happens
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
pub async fn run(config: Config) {
|
pub async fn run(config: Config) {
|
||||||
// get OAuth2 token
|
|
||||||
let token = get_oauth2_token(&config.twitter);
|
|
||||||
|
|
||||||
for mastodon_config in config.mastodon.values() {
|
|
||||||
// open the SQLite connection
|
// open the SQLite connection
|
||||||
let conn = Connection::open(&config.scootaloo.db_path).unwrap_or_else(|e| {
|
let conn = Arc::new(Mutex::new(
|
||||||
|
Connection::open(&config.scootaloo.db_path).unwrap_or_else(|e| {
|
||||||
panic!(
|
panic!(
|
||||||
"Something went wrong when opening the DB {}: {}",
|
"Something went wrong when opening the DB {}: {}",
|
||||||
&config.scootaloo.db_path, e
|
&config.scootaloo.db_path, e
|
||||||
)
|
)
|
||||||
});
|
}),
|
||||||
|
));
|
||||||
|
|
||||||
|
let mut stream = futures::stream::iter(config.mastodon.into_values())
|
||||||
|
.map(|mastodon_config| {
|
||||||
|
// calculate Twitter page size
|
||||||
|
let page_size = mastodon_config
|
||||||
|
.twitter_page_size
|
||||||
|
.unwrap_or_else(|| config.twitter.page_size.unwrap_or(DEFAULT_PAGE_SIZE));
|
||||||
|
|
||||||
|
// create temporary value for each task
|
||||||
|
let scootaloo_cache_path = config.scootaloo.cache_path.clone();
|
||||||
|
let token = get_oauth2_token(&config.twitter);
|
||||||
|
let task_conn = conn.clone();
|
||||||
|
|
||||||
|
spawn(async move {
|
||||||
|
info!("Starting treating {}", &mastodon_config.twitter_screen_name);
|
||||||
|
|
||||||
// retrieve the last tweet ID for the username
|
// retrieve the last tweet ID for the username
|
||||||
let last_tweet_id = read_state(&conn, &mastodon_config.twitter_screen_name, None)
|
let lconn = task_conn.lock().await;
|
||||||
.unwrap_or_else(|e| panic!("Cannot retrieve last_tweet_id: {}", e))
|
let last_tweet_id = read_state(&lconn, &mastodon_config.twitter_screen_name, None)?
|
||||||
.map(|s| s.tweet_id);
|
.map(|r| r.tweet_id);
|
||||||
|
drop(lconn);
|
||||||
|
|
||||||
|
// get reversed, curated user timeline
|
||||||
|
let feed = get_user_timeline(
|
||||||
|
&mastodon_config.twitter_screen_name,
|
||||||
|
&token,
|
||||||
|
last_tweet_id,
|
||||||
|
page_size,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
// get Mastodon instance
|
// get Mastodon instance
|
||||||
let mastodon = get_mastodon_token(mastodon_config);
|
let mastodon = get_mastodon_token(&mastodon_config);
|
||||||
|
|
||||||
// get user timeline feed (Vec<tweet>)
|
|
||||||
let mut feed = get_user_timeline(mastodon_config, &token, last_tweet_id)
|
|
||||||
.await
|
|
||||||
.unwrap_or_else(|e| {
|
|
||||||
panic!(
|
|
||||||
"Something went wrong when trying to retrieve {}’s timeline: {}",
|
|
||||||
&mastodon_config.twitter_screen_name, e
|
|
||||||
)
|
|
||||||
});
|
|
||||||
|
|
||||||
// empty feed -> exiting
|
|
||||||
if feed.is_empty() {
|
|
||||||
info!("Nothing to retrieve since last time, exiting…");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// order needs to be chronological
|
|
||||||
feed.reverse();
|
|
||||||
|
|
||||||
for tweet in &feed {
|
for tweet in &feed {
|
||||||
debug!("Treating Tweet {} inside feed", tweet.id);
|
info!("Treating Tweet {} inside feed", tweet.id);
|
||||||
// initiate the toot_reply_id var
|
let lconn = task_conn.lock().await;
|
||||||
let mut toot_reply_id: Option<String> = None;
|
// initiate the toot_reply_id var and retrieve the corresponding toot_id
|
||||||
// determine if the tweet is part of a thread (response to self) or a standard response
|
let toot_reply_id: Option<String> = tweet.in_reply_to_user_id.and_then(|_| {
|
||||||
if let Some(r) = &tweet.in_reply_to_screen_name {
|
read_state(
|
||||||
if r.to_lowercase() != mastodon_config.twitter_screen_name.to_lowercase() {
|
&lconn,
|
||||||
// we are responding not threadin
|
|
||||||
info!("Tweet is a direct response, skipping");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
info!("Tweet is a thread");
|
|
||||||
toot_reply_id = read_state(
|
|
||||||
&conn,
|
|
||||||
&mastodon_config.twitter_screen_name,
|
&mastodon_config.twitter_screen_name,
|
||||||
tweet.in_reply_to_status_id,
|
tweet.in_reply_to_status_id,
|
||||||
)
|
)
|
||||||
.unwrap_or(None)
|
.unwrap_or(None)
|
||||||
.map(|s| s.toot_id);
|
.map(|s| s.toot_id)
|
||||||
};
|
});
|
||||||
|
drop(lconn);
|
||||||
|
|
||||||
// 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_text = build_basic_status(tweet);
|
let mut status_text = build_basic_status(tweet);
|
||||||
|
|
||||||
let mut status_medias: Vec<String> = vec![];
|
// building associative media list
|
||||||
// reupload the attachments if any
|
let (media_url, status_medias) =
|
||||||
if let Some(m) = &tweet.extended_entities {
|
generate_media_ids(tweet, &scootaloo_cache_path, &mastodon).await;
|
||||||
for media in &m.media {
|
|
||||||
let local_tweet_media_path =
|
|
||||||
match get_tweet_media(media, &config.scootaloo.cache_path).await {
|
|
||||||
Ok(m) => m,
|
|
||||||
Err(e) => {
|
|
||||||
error!("Cannot get tweet media for {}: {}", &media.url, e);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let mastodon_media_ids = match mastodon
|
status_text = status_text.replace(&media_url, "");
|
||||||
.media(Cow::from(local_tweet_media_path.to_owned()))
|
|
||||||
{
|
|
||||||
Ok(m) => {
|
|
||||||
remove_file(&local_tweet_media_path)
|
|
||||||
.await
|
|
||||||
.unwrap_or_else(|e|
|
|
||||||
warn!("Attachment for {} has been uploaded, but I’m unable to remove the existing file: {}", &local_tweet_media_path, e)
|
|
||||||
);
|
|
||||||
m.id
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
error!(
|
|
||||||
"Attachment {} cannot be uploaded to Mastodon Instance: {}",
|
|
||||||
&local_tweet_media_path, e
|
|
||||||
);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
status_medias.push(mastodon_media_ids);
|
info!("Building corresponding Mastodon status");
|
||||||
|
|
||||||
// last step, removing the reference to the media from with the toot’s text
|
|
||||||
status_text = status_text.replace(&media.url, "");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// finished reuploading attachments, now let’s do the toot baby!
|
|
||||||
|
|
||||||
debug!("Building corresponding Mastodon status");
|
|
||||||
|
|
||||||
let mut status_builder = StatusBuilder::new();
|
let mut status_builder = StatusBuilder::new();
|
||||||
|
|
||||||
@@ -140,15 +110,14 @@ pub async fn run(config: Config) {
|
|||||||
status_builder.in_reply_to(&i);
|
status_builder.in_reply_to(&i);
|
||||||
}
|
}
|
||||||
|
|
||||||
let status = status_builder
|
// can be activated for test purposes
|
||||||
.build()
|
// status_builder.visibility(elefren::status_builder::Visibility::Private);
|
||||||
.unwrap_or_else(|_| panic!("Cannot build status with text {}", &status_text));
|
|
||||||
|
|
||||||
// publish status
|
let status = status_builder.build()?;
|
||||||
// again unwrap is safe here as we are in the main thread
|
|
||||||
let published_status = mastodon.new_status(status).unwrap();
|
let published_status = mastodon.new_status(status)?;
|
||||||
// this will panic if it cannot publish the status, which is a good thing, it allows the
|
// this will return if it cannot publish the status preventing the last_tweet from
|
||||||
// last_tweet gathered not to be written
|
// being written into db
|
||||||
|
|
||||||
let ttt_towrite = TweetToToot {
|
let ttt_towrite = TweetToToot {
|
||||||
twitter_screen_name: mastodon_config.twitter_screen_name.clone(),
|
twitter_screen_name: mastodon_config.twitter_screen_name.clone(),
|
||||||
@@ -157,8 +126,21 @@ pub async fn run(config: Config) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// write the current state (tweet ID and toot ID) to avoid copying it another time
|
// write the current state (tweet ID and toot ID) to avoid copying it another time
|
||||||
write_state(&conn, ttt_towrite)
|
let lconn = task_conn.lock().await;
|
||||||
.unwrap_or_else(|e| panic!("Can’t write the last tweet retrieved: {}", e));
|
write_state(&lconn, ttt_towrite)?;
|
||||||
|
drop(lconn);
|
||||||
|
}
|
||||||
|
Ok::<(), ScootalooError>(())
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.buffer_unordered(config.scootaloo.rate_limit.unwrap_or(DEFAULT_RATE_LIMIT));
|
||||||
|
|
||||||
|
// launch and wait for every handle
|
||||||
|
while let Some(result) = stream.next().await {
|
||||||
|
match result {
|
||||||
|
Ok(Err(e)) => eprintln!("Error within thread: {}", e),
|
||||||
|
Err(e) => eprintln!("Error with thread: {}", e),
|
||||||
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
95
src/main.rs
95
src/main.rs
@@ -1,5 +1,5 @@
|
|||||||
use clap::{App, Arg, SubCommand};
|
use clap::{Arg, Command};
|
||||||
use log::{error, LevelFilter};
|
use log::LevelFilter;
|
||||||
use scootaloo::*;
|
use scootaloo::*;
|
||||||
use simple_logger::SimpleLogger;
|
use simple_logger::SimpleLogger;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
@@ -7,115 +7,118 @@ use std::str::FromStr;
|
|||||||
const DEFAULT_CONFIG_PATH: &str = "/usr/local/etc/scootaloo.toml";
|
const DEFAULT_CONFIG_PATH: &str = "/usr/local/etc/scootaloo.toml";
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let matches = App::new(env!("CARGO_PKG_NAME"))
|
let matches = Command::new(env!("CARGO_PKG_NAME"))
|
||||||
.version(env!("CARGO_PKG_VERSION"))
|
.version(env!("CARGO_PKG_VERSION"))
|
||||||
.about("A Twitter to Mastodon bot")
|
.about("A Twitter to Mastodon bot")
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("config")
|
Arg::new("config")
|
||||||
.short("c")
|
.short('c')
|
||||||
.long("config")
|
.long("config")
|
||||||
.value_name("CONFIG_FILE")
|
.value_name("CONFIG_FILE")
|
||||||
.help(&format!(
|
.help("TOML config file for scootaloo")
|
||||||
"TOML config file for scootaloo (default {})",
|
.num_args(1)
|
||||||
DEFAULT_CONFIG_PATH
|
.default_value(DEFAULT_CONFIG_PATH)
|
||||||
))
|
|
||||||
.takes_value(true)
|
|
||||||
.display_order(1),
|
.display_order(1),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("log_level")
|
Arg::new("log_level")
|
||||||
.short("l")
|
.short('l')
|
||||||
.long("loglevel")
|
.long("loglevel")
|
||||||
.value_name("LOGLEVEL")
|
.value_name("LOGLEVEL")
|
||||||
.help("Log level. Valid values are: Off, Warn, Error, Info, Debug")
|
.help("Log level")
|
||||||
.takes_value(true)
|
.num_args(1)
|
||||||
|
.value_parser(["Off", "Warn", "Error", "Info", "Debug"])
|
||||||
.display_order(2),
|
.display_order(2),
|
||||||
)
|
)
|
||||||
.subcommand(
|
.subcommand(
|
||||||
SubCommand::with_name("register")
|
Command::new("register")
|
||||||
.version(env!("CARGO_PKG_VERSION"))
|
.version(env!("CARGO_PKG_VERSION"))
|
||||||
.about("Command to register to a Mastodon Instance")
|
.about("Command to register to a Mastodon Instance")
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("host")
|
Arg::new("host")
|
||||||
.short("H")
|
.short('H')
|
||||||
.long("host")
|
.long("host")
|
||||||
.value_name("HOST")
|
.value_name("HOST")
|
||||||
.help("Base URL of the Mastodon instance to register to (no default)")
|
.help("Base URL of the Mastodon instance to register to (no default)")
|
||||||
.takes_value(true)
|
.num_args(1)
|
||||||
.required(true)
|
.required(true)
|
||||||
.display_order(1)
|
.display_order(1)
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("name")
|
Arg::new("name")
|
||||||
.short("n")
|
.short('n')
|
||||||
.long("name")
|
.long("name")
|
||||||
.help("Twitter Screen Name (like https://twitter.com/screen_name, no default)")
|
.help("Twitter Screen Name (like https://twitter.com/screen_name, no default)")
|
||||||
.takes_value(true)
|
.num_args(1)
|
||||||
.required(true)
|
.required(true)
|
||||||
.display_order(2)
|
.display_order(2)
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.subcommand(
|
.subcommand(
|
||||||
SubCommand::with_name("init")
|
Command::new("init")
|
||||||
.version(env!("CARGO_PKG_VERSION"))
|
.version(env!("CARGO_PKG_VERSION"))
|
||||||
.about("Command to init Scootaloo DB")
|
.about("Command to init Scootaloo DB")
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("config")
|
Arg::new("config")
|
||||||
.short("c")
|
.short('c')
|
||||||
.long("config")
|
.long("config")
|
||||||
.value_name("CONFIG_FILE")
|
.value_name("CONFIG_FILE")
|
||||||
.help(&format!(
|
.help(&format!(
|
||||||
"TOML config file for scootaloo (default {})",
|
"TOML config file for scootaloo (default {})",
|
||||||
DEFAULT_CONFIG_PATH
|
DEFAULT_CONFIG_PATH
|
||||||
))
|
))
|
||||||
.takes_value(true)
|
.default_value(DEFAULT_CONFIG_PATH)
|
||||||
|
.num_args(1)
|
||||||
.display_order(1),
|
.display_order(1),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.subcommand(
|
.subcommand(
|
||||||
SubCommand::with_name("migrate")
|
Command::new("migrate")
|
||||||
.version(env!("CARGO_PKG_VERSION"))
|
.version(env!("CARGO_PKG_VERSION"))
|
||||||
.about("Command to migrate Scootaloo DB")
|
.about("Command to migrate Scootaloo DB")
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("config")
|
Arg::new("config")
|
||||||
.short("c")
|
.short('c')
|
||||||
.long("config")
|
.long("config")
|
||||||
.value_name("CONFIG_FILE")
|
.value_name("CONFIG_FILE")
|
||||||
.help(&format!("TOML config file for scootaloo (default {})", DEFAULT_CONFIG_PATH))
|
.help(&format!("TOML config file for scootaloo (default {})", DEFAULT_CONFIG_PATH))
|
||||||
.takes_value(true)
|
.default_value(DEFAULT_CONFIG_PATH)
|
||||||
|
.num_args(1)
|
||||||
.display_order(1),
|
.display_order(1),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("name")
|
Arg::new("name")
|
||||||
.short("n")
|
.short('n')
|
||||||
.long("name")
|
.long("name")
|
||||||
.help("Twitter Screen Name (like https://twitter.com/screen_name, no default)")
|
.help("Twitter Screen Name (like https://twitter.com/screen_name, no default)")
|
||||||
.takes_value(true)
|
.num_args(1)
|
||||||
.display_order(2)
|
.display_order(2)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
match matches.subcommand() {
|
match matches.subcommand() {
|
||||||
("register", Some(sub_m)) => {
|
Some(("register", sub_m)) => {
|
||||||
register(
|
register(
|
||||||
sub_m.value_of("host").unwrap(),
|
sub_m.get_one::<String>("host").unwrap(),
|
||||||
sub_m.value_of("name").unwrap(),
|
sub_m.get_one::<String>("name").unwrap(),
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
("init", Some(sub_m)) => {
|
Some(("init", sub_m)) => {
|
||||||
let config = parse_toml(sub_m.value_of("config").unwrap_or(DEFAULT_CONFIG_PATH));
|
let config = parse_toml(sub_m.get_one::<String>("config").unwrap());
|
||||||
init_db(&config.scootaloo.db_path).unwrap();
|
init_db(&config.scootaloo.db_path).unwrap();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
("migrate", Some(sub_m)) => {
|
Some(("migrate", sub_m)) => {
|
||||||
let config = parse_toml(sub_m.value_of("config").unwrap_or(DEFAULT_CONFIG_PATH));
|
let config = parse_toml(sub_m.get_one::<String>("config").unwrap());
|
||||||
let config_twitter_screen_name =
|
let config_twitter_screen_name =
|
||||||
&config.mastodon.values().next().unwrap().twitter_screen_name;
|
&config.mastodon.values().next().unwrap().twitter_screen_name;
|
||||||
migrate_db(
|
migrate_db(
|
||||||
&config.scootaloo.db_path,
|
&config.scootaloo.db_path,
|
||||||
sub_m.value_of("name").unwrap_or(config_twitter_screen_name),
|
sub_m
|
||||||
|
.get_one::<String>("name")
|
||||||
|
.unwrap_or(config_twitter_screen_name),
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
return;
|
return;
|
||||||
@@ -123,20 +126,14 @@ fn main() {
|
|||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
if matches.is_present("log_level") {
|
if let Some(level) = matches.get_one::<String>("log_level") {
|
||||||
match LevelFilter::from_str(matches.value_of("log_level").unwrap()) {
|
|
||||||
Ok(level) => SimpleLogger::new().with_level(level).init().unwrap(),
|
|
||||||
Err(e) => {
|
|
||||||
SimpleLogger::new()
|
SimpleLogger::new()
|
||||||
.with_level(LevelFilter::Error)
|
.with_level(LevelFilter::from_str(level).unwrap())
|
||||||
.init()
|
.init()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
error!("Unknown log level filter: {}", e);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let config = parse_toml(matches.value_of("config").unwrap_or(DEFAULT_CONFIG_PATH));
|
let config = parse_toml(matches.get_one::<String>("config").unwrap());
|
||||||
|
|
||||||
run(config);
|
run(config);
|
||||||
}
|
}
|
||||||
|
@@ -101,9 +101,9 @@ pub fn register(host: &str, screen_name: &str) {
|
|||||||
|
|
||||||
println!(
|
println!(
|
||||||
"Please insert the following block at the end of your configuration file:
|
"Please insert the following block at the end of your configuration file:
|
||||||
\n[mastodon.{}]
|
[mastodon.{}]
|
||||||
\ntwitter_screen_name = \"{}\"
|
twitter_screen_name = \"{}\"
|
||||||
\n{}",
|
{}",
|
||||||
screen_name.to_lowercase(),
|
screen_name.to_lowercase(),
|
||||||
screen_name,
|
screen_name,
|
||||||
toml
|
toml
|
||||||
|
@@ -87,7 +87,7 @@ pub fn migrate_db(d: &str, s: &str) -> Result<(), Box<dyn Error>> {
|
|||||||
match res {
|
match res {
|
||||||
Err(e) => match e.to_string().as_str() {
|
Err(e) => match e.to_string().as_str() {
|
||||||
"duplicate column name: twitter_screen_name" => Ok(()),
|
"duplicate column name: twitter_screen_name" => Ok(()),
|
||||||
_ => Err(Box::new(e)),
|
_ => Err(e.into()),
|
||||||
},
|
},
|
||||||
_ => Ok(()),
|
_ => Ok(()),
|
||||||
}
|
}
|
||||||
|
@@ -1,4 +1,3 @@
|
|||||||
use crate::config::MastodonConfig;
|
|
||||||
use crate::config::TwitterConfig;
|
use crate::config::TwitterConfig;
|
||||||
use crate::util::cache_media;
|
use crate::util::cache_media;
|
||||||
use crate::ScootalooError;
|
use crate::ScootalooError;
|
||||||
@@ -28,24 +27,31 @@ pub fn get_oauth2_token(config: &TwitterConfig) -> Token {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets Twitter user timeline
|
/// Gets Twitter user timeline, eliminate responses to others and reverse it
|
||||||
pub async fn get_user_timeline(
|
pub async fn get_user_timeline(
|
||||||
config: &MastodonConfig,
|
screen_name: &str,
|
||||||
token: &Token,
|
token: &Token,
|
||||||
lid: Option<u64>,
|
lid: Option<u64>,
|
||||||
|
page_size: i32,
|
||||||
) -> Result<Vec<Tweet>, Box<dyn Error>> {
|
) -> Result<Vec<Tweet>, Box<dyn Error>> {
|
||||||
// fix the page size to 200 as it is the maximum Twitter authorizes
|
// fix the page size to 200 as it is the maximum Twitter authorizes
|
||||||
let (_, feed) = user_timeline(
|
let (_, feed) = user_timeline(UserID::from(screen_name.to_owned()), true, false, token)
|
||||||
UserID::from(config.twitter_screen_name.to_owned()),
|
.with_page_size(page_size)
|
||||||
true,
|
|
||||||
false,
|
|
||||||
token,
|
|
||||||
)
|
|
||||||
.with_page_size(200)
|
|
||||||
.older(lid)
|
.older(lid)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(feed.to_vec())
|
let mut feed: Vec<Tweet> = feed
|
||||||
|
.iter()
|
||||||
|
.cloned()
|
||||||
|
.filter(|t| match &t.in_reply_to_screen_name {
|
||||||
|
Some(r) => r.to_lowercase() == screen_name.to_lowercase(),
|
||||||
|
None => true,
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
feed.reverse();
|
||||||
|
|
||||||
|
Ok(feed)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Retrieves a single media from a tweet and store it in a temporary file
|
/// Retrieves a single media from a tweet and store it in a temporary file
|
||||||
|
75
src/util.rs
75
src/util.rs
@@ -1,11 +1,80 @@
|
|||||||
use crate::ScootalooError;
|
use crate::{twitter::get_tweet_media, ScootalooError};
|
||||||
|
|
||||||
|
use std::{borrow::Cow, error::Error};
|
||||||
|
|
||||||
|
use egg_mode::tweet::Tweet;
|
||||||
|
|
||||||
|
use elefren::prelude::*;
|
||||||
|
|
||||||
|
use log::{error, info, warn};
|
||||||
|
|
||||||
use reqwest::Url;
|
use reqwest::Url;
|
||||||
use std::error::Error;
|
|
||||||
use tokio::{
|
use tokio::{
|
||||||
fs::{create_dir_all, File},
|
fs::{create_dir_all, remove_file, File},
|
||||||
io::copy,
|
io::copy,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use futures::{stream, stream::StreamExt};
|
||||||
|
|
||||||
|
/// Generate associative table between media ids and tweet extended entities
|
||||||
|
pub async fn generate_media_ids(
|
||||||
|
tweet: &Tweet,
|
||||||
|
cache_path: &str,
|
||||||
|
mastodon: &Mastodon,
|
||||||
|
) -> (String, Vec<String>) {
|
||||||
|
let mut media_url = "".to_string();
|
||||||
|
let mut media_ids: Vec<String> = vec![];
|
||||||
|
|
||||||
|
if let Some(m) = &tweet.extended_entities {
|
||||||
|
info!("{} medias in tweet", m.media.len());
|
||||||
|
|
||||||
|
let medias = m.media.clone();
|
||||||
|
|
||||||
|
let mut stream = stream::iter(medias)
|
||||||
|
.map(|media| {
|
||||||
|
// attribute media url
|
||||||
|
media_url = media.url.clone();
|
||||||
|
|
||||||
|
// clone everything we need
|
||||||
|
let cache_path = String::from(cache_path);
|
||||||
|
let mastodon = mastodon.clone();
|
||||||
|
|
||||||
|
tokio::task::spawn(async move {
|
||||||
|
info!("Start treating {}", media.media_url_https);
|
||||||
|
// get the tweet embedded media
|
||||||
|
let local_tweet_media_path = get_tweet_media(&media, &cache_path).await?;
|
||||||
|
|
||||||
|
// upload media to Mastodon
|
||||||
|
let mastodon_media =
|
||||||
|
mastodon.media(Cow::from(local_tweet_media_path.to_owned()))?;
|
||||||
|
// at this point, we can safely erase the original file
|
||||||
|
// it doesn’t matter if we can’t remove, cache_media fn is idempotent
|
||||||
|
remove_file(&local_tweet_media_path).await.ok();
|
||||||
|
|
||||||
|
Ok::<String, ScootalooError>(mastodon_media.id)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.buffered(4); // there are max four medias per tweet and they need to be treated in
|
||||||
|
// order
|
||||||
|
|
||||||
|
while let Some(result) = stream.next().await {
|
||||||
|
match result {
|
||||||
|
Ok(Ok(v)) => media_ids.push(v),
|
||||||
|
Ok(Err(e)) => warn!("Cannot treat media: {}", e),
|
||||||
|
Err(e) => error!("Something went wrong when joining the main thread: {}", e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
info!("No media in tweet");
|
||||||
|
}
|
||||||
|
|
||||||
|
// in case some media_ids slot remained empty due to errors, remove them
|
||||||
|
media_ids.retain(|x| !x.is_empty());
|
||||||
|
|
||||||
|
(media_url, media_ids)
|
||||||
|
}
|
||||||
|
|
||||||
/// Gets and caches Twitter Media inside the determined temp dir
|
/// Gets and caches Twitter Media inside the determined temp dir
|
||||||
pub async fn cache_media(u: &str, t: &str) -> Result<String, Box<dyn Error>> {
|
pub async fn cache_media(u: &str, t: &str) -> Result<String, Box<dyn Error>> {
|
||||||
// create dir
|
// create dir
|
||||||
|
@@ -1,5 +1,57 @@
|
|||||||
use scootaloo::parse_toml;
|
use scootaloo::parse_toml;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_page_size() {
|
||||||
|
const DEFAULT_PAGE_SIZE: i32 = 200;
|
||||||
|
let toml = parse_toml("tests/page_size.toml");
|
||||||
|
|
||||||
|
assert_eq!(toml.twitter.page_size, Some(100));
|
||||||
|
|
||||||
|
assert_eq!(toml.mastodon.get("0").unwrap().twitter_page_size, None);
|
||||||
|
|
||||||
|
assert_eq!(toml.mastodon.get("1").unwrap().twitter_page_size, Some(42));
|
||||||
|
|
||||||
|
// this is the exact line that is used inside fn run() to determine the twitter page size
|
||||||
|
// passed to fn get_user_timeline()
|
||||||
|
let page_size_for_0 = toml
|
||||||
|
.mastodon
|
||||||
|
.get("0")
|
||||||
|
.unwrap()
|
||||||
|
.twitter_page_size
|
||||||
|
.unwrap_or_else(|| toml.twitter.page_size.unwrap_or(DEFAULT_PAGE_SIZE));
|
||||||
|
let page_size_for_1 = toml
|
||||||
|
.mastodon
|
||||||
|
.get("1")
|
||||||
|
.unwrap()
|
||||||
|
.twitter_page_size
|
||||||
|
.unwrap_or_else(|| toml.twitter.page_size.unwrap_or(DEFAULT_PAGE_SIZE));
|
||||||
|
|
||||||
|
assert_eq!(page_size_for_0, 100);
|
||||||
|
assert_eq!(page_size_for_1, 42);
|
||||||
|
|
||||||
|
let toml = parse_toml("tests/no_page_size.toml");
|
||||||
|
|
||||||
|
assert_eq!(toml.twitter.page_size, None);
|
||||||
|
assert_eq!(toml.mastodon.get("0").unwrap().twitter_page_size, None);
|
||||||
|
|
||||||
|
// and same here
|
||||||
|
let page_size_for_0 = toml
|
||||||
|
.mastodon
|
||||||
|
.get("0")
|
||||||
|
.unwrap()
|
||||||
|
.twitter_page_size
|
||||||
|
.unwrap_or_else(|| toml.twitter.page_size.unwrap_or(DEFAULT_PAGE_SIZE));
|
||||||
|
|
||||||
|
assert_eq!(page_size_for_0, 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_good_toml_rate_limit() {
|
||||||
|
let parse_good_toml = parse_toml("tests/good_test_rate_limit.toml");
|
||||||
|
|
||||||
|
assert_eq!(parse_good_toml.scootaloo.rate_limit, Some(69 as usize));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_good_toml() {
|
fn test_parse_good_toml() {
|
||||||
let parse_good_toml = parse_toml("tests/good_test.toml");
|
let parse_good_toml = parse_toml("tests/good_test.toml");
|
||||||
@@ -9,6 +61,7 @@ fn test_parse_good_toml() {
|
|||||||
"/var/random/scootaloo.sqlite"
|
"/var/random/scootaloo.sqlite"
|
||||||
);
|
);
|
||||||
assert_eq!(parse_good_toml.scootaloo.cache_path, "/tmp/scootaloo");
|
assert_eq!(parse_good_toml.scootaloo.cache_path, "/tmp/scootaloo");
|
||||||
|
assert_eq!(parse_good_toml.scootaloo.rate_limit, None);
|
||||||
|
|
||||||
assert_eq!(parse_good_toml.twitter.consumer_key, "rand consumer key");
|
assert_eq!(parse_good_toml.twitter.consumer_key, "rand consumer key");
|
||||||
assert_eq!(parse_good_toml.twitter.consumer_secret, "secret");
|
assert_eq!(parse_good_toml.twitter.consumer_secret, "secret");
|
||||||
|
20
tests/good_test_rate_limit.toml
Normal file
20
tests/good_test_rate_limit.toml
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
[scootaloo]
|
||||||
|
|
||||||
|
db_path="/var/random/scootaloo.sqlite"
|
||||||
|
cache_path="/tmp/scootaloo"
|
||||||
|
rate_limit=69
|
||||||
|
|
||||||
|
[twitter]
|
||||||
|
consumer_key="rand consumer key"
|
||||||
|
consumer_secret="secret"
|
||||||
|
access_key="rand access key"
|
||||||
|
access_secret="super secret"
|
||||||
|
|
||||||
|
[mastodon]
|
||||||
|
[mastodon.tamerelol]
|
||||||
|
twitter_screen_name="tamerelol"
|
||||||
|
base = "https://m.nintendojo.fr"
|
||||||
|
client_id = "rand client id"
|
||||||
|
client_secret = "secret"
|
||||||
|
redirect = "urn:ietf:wg:oauth:2.0:oob"
|
||||||
|
token = "super secret"
|
19
tests/no_page_size.toml
Normal file
19
tests/no_page_size.toml
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
[scootaloo]
|
||||||
|
|
||||||
|
db_path="/var/random/scootaloo.sqlite"
|
||||||
|
cache_path="/tmp/scootaloo"
|
||||||
|
|
||||||
|
[twitter]
|
||||||
|
consumer_key="rand consumer key"
|
||||||
|
consumer_secret="secret"
|
||||||
|
access_key="rand access key"
|
||||||
|
access_secret="super secret"
|
||||||
|
|
||||||
|
[mastodon]
|
||||||
|
[mastodon.0]
|
||||||
|
twitter_screen_name="tamerelol"
|
||||||
|
base = "https://m.nintendojo.fr"
|
||||||
|
client_id = "rand client id"
|
||||||
|
client_secret = "secret"
|
||||||
|
redirect = "urn:ietf:wg:oauth:2.0:oob"
|
||||||
|
token = "super secret"
|
29
tests/page_size.toml
Normal file
29
tests/page_size.toml
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
[scootaloo]
|
||||||
|
|
||||||
|
db_path="/var/random/scootaloo.sqlite"
|
||||||
|
cache_path="/tmp/scootaloo"
|
||||||
|
|
||||||
|
[twitter]
|
||||||
|
consumer_key="rand consumer key"
|
||||||
|
consumer_secret="secret"
|
||||||
|
access_key="rand access key"
|
||||||
|
access_secret="super secret"
|
||||||
|
page_size=100
|
||||||
|
|
||||||
|
[mastodon]
|
||||||
|
[mastodon.0]
|
||||||
|
twitter_screen_name="tamerelol"
|
||||||
|
base = "https://m.nintendojo.fr"
|
||||||
|
client_id = "rand client id"
|
||||||
|
client_secret = "secret"
|
||||||
|
redirect = "urn:ietf:wg:oauth:2.0:oob"
|
||||||
|
token = "super secret"
|
||||||
|
|
||||||
|
[mastodon.1]
|
||||||
|
twitter_screen_name="tonperemdr"
|
||||||
|
twitter_page_size=42
|
||||||
|
base = "https://m.nintendojo.fr"
|
||||||
|
client_id = "rand client id"
|
||||||
|
client_secret = "secret"
|
||||||
|
redirect = "urn:ietf:wg:oauth:2.0:oob"
|
||||||
|
token = "super secret"
|
Reference in New Issue
Block a user