4 Commits

Author SHA1 Message Date
VC
31aea7e1a6 Merge branch 'fix_french_inclusive_writing' into 'master'
feat: allow user to remove some links, replace some links by others

See merge request veretcle/scootaloo!38
2022-11-23 12:49:46 +00:00
Clément VERET
851f95d516 doc: regexp + alt services 2022-11-23 13:17:00 +01:00
Clément VERET
ffb9522ce2 feat: main logic for regex + url filtering 2022-11-23 13:16:56 +01:00
Clément VERET
b6df8c6230 test: add tests for scootaloo alt services + regexp 2022-11-23 13:09:03 +01:00
8 changed files with 141 additions and 1 deletions

View File

@@ -19,6 +19,19 @@ First up, create a configuration file (default path is `/usr/local/etc/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 rate_limiting = 4 ## optional, default 4, number of accounts handled simultaneously
## optional, this should be omitted the majority of the time
## sometimes, twitter try to use french inclusive writting, but instead of using `·` (median point), theyre using `.`
## this makes twitter interpret it as a URL, which is wrong
## this parameter allows you to catch such URLs and apply the `display_url` (i.e. `tout.es`) instead of the `expanded_url` (i.e. `http://tout.es`)
## in those particular cases
## (!) use with caution, it might have some undesired effects
show_url_as_display_url_for = "^http(s)://(.+)\\.es$"
## optional, this allows you to replace the host for popular services such as YouTube of Twitter, or any other
## with their more freely accessible equivalent
[scootaloo.alternative_services_for]
"tamere.lol" = "tonpere.mdr" ## quotes are necessary for both parameters
"you.pi" = "you.pla"
"www.you.pi" = "you.pla" ## this is an exact match, so youll need to lay out all the possibilities
[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)

View File

@@ -36,6 +36,8 @@ pub struct ScootalooConfig {
pub db_path: String, pub db_path: String,
pub cache_path: String, pub cache_path: String,
pub rate_limit: Option<usize>, pub rate_limit: Option<usize>,
pub show_url_as_display_url_for: Option<String>,
pub alternative_services_for: Option<HashMap<String, String>>,
} }
/// Parses the TOML file into a Config Struct /// Parses the TOML file into a Config Struct

View File

@@ -22,6 +22,7 @@ use state::{read_state, write_state, TweetToToot};
use elefren::{prelude::*, status_builder::StatusBuilder, Language}; use elefren::{prelude::*, status_builder::StatusBuilder, Language};
use futures::StreamExt; use futures::StreamExt;
use log::info; use log::info;
use regex::Regex;
use rusqlite::Connection; use rusqlite::Connection;
use std::{collections::HashMap, sync::Arc}; use std::{collections::HashMap, sync::Arc};
use tokio::{spawn, sync::Mutex}; use tokio::{spawn, sync::Mutex};
@@ -58,6 +59,14 @@ pub async fn run(config: Config) {
}) })
.collect(); .collect();
let display_url_re = config
.scootaloo
.show_url_as_display_url_for
.as_ref()
.map(|r|
// we want to panic in case the RE is not valid
Regex::new(r).unwrap());
let mut stream = futures::stream::iter(config.mastodon.into_values()) let mut stream = futures::stream::iter(config.mastodon.into_values())
.map(|mastodon_config| { .map(|mastodon_config| {
// calculate Twitter page size // calculate Twitter page size
@@ -68,6 +77,8 @@ pub async fn run(config: Config) {
// create temporary value for each task // create temporary value for each task
let scootaloo_cache_path = config.scootaloo.cache_path.clone(); let scootaloo_cache_path = config.scootaloo.cache_path.clone();
let scootaloo_mentions = scootaloo_mentions.clone(); let scootaloo_mentions = scootaloo_mentions.clone();
let scootaloo_alt_services = config.scootaloo.alternative_services_for.clone();
let display_url_re = display_url_re.clone();
let token = get_oauth2_token(&config.twitter); let token = get_oauth2_token(&config.twitter);
let task_conn = conn.clone(); let task_conn = conn.clone();
@@ -108,7 +119,12 @@ pub async fn run(config: Config) {
drop(lconn); 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, &scootaloo_mentions); let mut status_text = build_basic_status(
tweet,
&scootaloo_mentions,
&display_url_re,
&scootaloo_alt_services,
);
// building associative media list // building associative media list
let (media_url, status_medias) = let (media_url, status_medias) =

View File

@@ -1,4 +1,33 @@
use scootaloo::parse_toml; use scootaloo::parse_toml;
use std::collections::HashMap;
#[test]
fn test_alt_services() {
let toml = parse_toml("tests/no_test_alt_services.toml");
assert_eq!(toml.scootaloo.alternative_services_for, None);
let toml = parse_toml("tests/test_alt_services.toml");
assert_eq!(
toml.scootaloo.alternative_services_for,
Some(HashMap::from([
("tamere.lol".to_string(), "tonpere.mdr".to_string()),
("you.pi".to_string(), "you.pla".to_string())
]))
);
}
#[test]
fn test_re_display() {
let toml = parse_toml("tests/no_show_url_as_display_url_for.toml");
assert_eq!(toml.scootaloo.show_url_as_display_url_for, None);
let toml = parse_toml("tests/show_url_as_display_url_for.toml");
assert_eq!(
toml.scootaloo.show_url_as_display_url_for,
Some("^(.+)\\.es$".to_string())
);
}
#[test] #[test]
fn test_page_size() { fn test_page_size() {

View 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.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"

View 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.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"

View File

@@ -0,0 +1,20 @@
[scootaloo]
db_path="/var/random/scootaloo.sqlite"
cache_path="/tmp/scootaloo"
show_url_as_display_url_for = "^(.+)\\.es$"
[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"

View File

@@ -0,0 +1,22 @@
[scootaloo]
db_path="/var/random/scootaloo.sqlite"
cache_path="/tmp/scootaloo"
[scootaloo.alternative_services_for]
"tamere.lol" = "tonpere.mdr"
"you.pi" = "you.pla"
[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"