mirror of
https://framagit.org/veretcle/scootaloo.git
synced 2025-07-21 17:34:37 +02:00
Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
92d5fdffad | ||
![]() |
331adec60f | ||
![]() |
9a341310da | ||
![]() |
2c77a0e5fc | ||
![]() |
032e3cf8dd | ||
![]() |
a854243cf6 | ||
![]() |
b33ffa4401 | ||
![]() |
77941e0b9a | ||
![]() |
1489f89bdb | ||
![]() |
93a27deae8 | ||
![]() |
fe3745d91f | ||
![]() |
9a1e4c8e6c | ||
![]() |
8b12f83c5d | ||
![]() |
f93bb5158b | ||
![]() |
d5db8b0d85 | ||
![]() |
fe8e81b54d | ||
![]() |
636ea8c85e |
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -2103,7 +2103,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "scootaloo"
|
name = "scootaloo"
|
||||||
version = "0.9.0"
|
version = "0.9.4"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
"clap",
|
"clap",
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "scootaloo"
|
name = "scootaloo"
|
||||||
version = "0.9.0"
|
version = "0.9.4"
|
||||||
authors = ["VC <veretcle+framagit@mateu.be>"]
|
authors = ["VC <veretcle+framagit@mateu.be>"]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
|
@@ -22,6 +22,7 @@ rate_limiting = 4 ## optional, default 4, number of accounts handled simultaneou
|
|||||||
|
|
||||||
[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"
|
||||||
@@ -56,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,
|
||||||
|
59
src/lib.rs
59
src/lib.rs
@@ -19,15 +19,15 @@ 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, Language};
|
||||||
|
use futures::StreamExt;
|
||||||
use log::info;
|
use log::info;
|
||||||
use rusqlite::Connection;
|
use rusqlite::Connection;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio::{spawn, sync::Mutex};
|
use tokio::{spawn, sync::Mutex};
|
||||||
|
|
||||||
use futures::StreamExt;
|
|
||||||
|
|
||||||
const DEFAULT_RATE_LIMIT: usize = 4;
|
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]
|
||||||
@@ -44,6 +44,11 @@ pub async fn run(config: Config) {
|
|||||||
|
|
||||||
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
|
||||||
|
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
|
// 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 token = get_oauth2_token(&config.twitter);
|
let token = get_oauth2_token(&config.twitter);
|
||||||
@@ -51,53 +56,39 @@ pub async fn run(config: Config) {
|
|||||||
|
|
||||||
spawn(async move {
|
spawn(async move {
|
||||||
info!("Starting treating {}", &mastodon_config.twitter_screen_name);
|
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 lconn = task_conn.lock().await;
|
let lconn = task_conn.lock().await;
|
||||||
let last_tweet_id = read_state(&lconn, &mastodon_config.twitter_screen_name, None)?
|
let last_tweet_id = read_state(&lconn, &mastodon_config.twitter_screen_name, None)?
|
||||||
.map(|r| r.tweet_id);
|
.map(|r| r.tweet_id);
|
||||||
drop(lconn);
|
drop(lconn);
|
||||||
|
|
||||||
// get user timeline feed (Vec<tweet>)
|
// get reversed, curated user timeline
|
||||||
let mut feed =
|
let feed = get_user_timeline(
|
||||||
get_user_timeline(&mastodon_config.twitter_screen_name, &token, last_tweet_id)
|
&mastodon_config.twitter_screen_name,
|
||||||
|
&token,
|
||||||
|
last_tweet_id,
|
||||||
|
page_size,
|
||||||
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// empty feed -> exiting
|
|
||||||
if feed.is_empty() {
|
|
||||||
info!("Nothing to retrieve since last time, exiting…");
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
// get Mastodon instance
|
// get Mastodon instance
|
||||||
let mastodon = get_mastodon_token(&mastodon_config);
|
let mastodon = get_mastodon_token(&mastodon_config);
|
||||||
|
|
||||||
// order needs to be chronological
|
|
||||||
feed.reverse();
|
|
||||||
|
|
||||||
for tweet in &feed {
|
for tweet in &feed {
|
||||||
info!("Treating Tweet {} inside feed", tweet.id);
|
info!("Treating Tweet {} inside feed", tweet.id);
|
||||||
// initiate the toot_reply_id var
|
|
||||||
let mut toot_reply_id: Option<String> = None;
|
|
||||||
// determine if the tweet is part of a thread (response to self) or a standard response
|
|
||||||
if let Some(r) = &tweet.in_reply_to_screen_name {
|
|
||||||
if r.to_lowercase() != mastodon_config.twitter_screen_name.to_lowercase() {
|
|
||||||
// we are responding not threading
|
|
||||||
info!("Tweet is a direct response, skipping");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
info!("Tweet is a thread");
|
|
||||||
// get the corresponding toot id
|
|
||||||
let lconn = task_conn.lock().await;
|
let lconn = task_conn.lock().await;
|
||||||
toot_reply_id = read_state(
|
// initiate the toot_reply_id var and retrieve the corresponding toot_id
|
||||||
|
let toot_reply_id: Option<String> = tweet.in_reply_to_user_id.and_then(|_| {
|
||||||
|
read_state(
|
||||||
&lconn,
|
&lconn,
|
||||||
&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);
|
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);
|
||||||
@@ -114,10 +105,18 @@ pub async fn run(config: Config) {
|
|||||||
|
|
||||||
status_builder.status(&status_text).media_ids(status_medias);
|
status_builder.status(&status_text).media_ids(status_medias);
|
||||||
|
|
||||||
|
// theard if necessary
|
||||||
if let Some(i) = toot_reply_id {
|
if let Some(i) = toot_reply_id {
|
||||||
status_builder.in_reply_to(&i);
|
status_builder.in_reply_to(&i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// language if any
|
||||||
|
if let Some(l) = &tweet.lang {
|
||||||
|
if let Some(r) = Language::from_639_1(l) {
|
||||||
|
status_builder.language(r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// can be activated for test purposes
|
// can be activated for test purposes
|
||||||
// status_builder.visibility(elefren::status_builder::Visibility::Private);
|
// status_builder.visibility(elefren::status_builder::Visibility::Private);
|
||||||
|
|
||||||
|
@@ -15,10 +15,7 @@ fn main() {
|
|||||||
.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 {})",
|
|
||||||
DEFAULT_CONFIG_PATH
|
|
||||||
))
|
|
||||||
.num_args(1)
|
.num_args(1)
|
||||||
.default_value(DEFAULT_CONFIG_PATH)
|
.default_value(DEFAULT_CONFIG_PATH)
|
||||||
.display_order(1),
|
.display_order(1),
|
||||||
@@ -28,7 +25,7 @@ fn main() {
|
|||||||
.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")
|
||||||
.num_args(1)
|
.num_args(1)
|
||||||
.value_parser(["Off", "Warn", "Error", "Info", "Debug"])
|
.value_parser(["Off", "Warn", "Error", "Info", "Debug"])
|
||||||
.display_order(2),
|
.display_order(2),
|
||||||
|
@@ -27,19 +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(
|
||||||
screen_name: &str,
|
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(UserID::from(screen_name.to_owned()), true, false, token)
|
let (_, feed) = user_timeline(UserID::from(screen_name.to_owned()), true, false, token)
|
||||||
.with_page_size(200)
|
.with_page_size(page_size)
|
||||||
.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
|
||||||
|
@@ -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