feat: async treatment of all accounts

This commit is contained in:
VC
2022-11-04 10:23:05 +01:00
parent 73244f9ecc
commit df75520175
4 changed files with 148 additions and 126 deletions

2
Cargo.lock generated
View File

@@ -2016,7 +2016,7 @@ dependencies = [
[[package]] [[package]]
name = "scootaloo" name = "scootaloo"
version = "0.7.0" version = "0.7.1"
dependencies = [ dependencies = [
"chrono", "chrono",
"clap", "clap",

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "scootaloo" name = "scootaloo"
version = "0.7.0" version = "0.7.1"
authors = ["VC <veretcle+framagit@mateu.be>"] authors = ["VC <veretcle+framagit@mateu.be>"]
edition = "2021" edition = "2021"

View File

@@ -21,33 +21,47 @@ 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::{debug, error, info, warn};
use rusqlite::Connection; use rusqlite::Connection;
use std::borrow::Cow; use std::{borrow::Cow, sync::Arc};
use tokio::fs::remove_file; use tokio::{fs::remove_file, spawn, sync::Mutex, task::JoinHandle};
/// 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 // create the task vector for handling multiple accounts
let token = get_oauth2_token(&config.twitter); let mut mtask: Vec<JoinHandle<()>> = vec![];
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
) )
}); }),
));
for mastodon_config in config.mastodon.into_values() {
// 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();
let task = spawn(async move {
debug!("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;
let last_tweet_id = read_state(&lconn, &mastodon_config.twitter_screen_name, None)
.unwrap_or_else(|e| panic!("Cannot retrieve last_tweet_id: {}", e)) .unwrap_or_else(|e| panic!("Cannot retrieve last_tweet_id: {}", e))
.map(|s| s.tweet_id); .map(|s| s.tweet_id);
drop(lconn);
// 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>) // get user timeline feed (Vec<tweet>)
let mut feed = get_user_timeline(mastodon_config, &token, last_tweet_id) let mut feed =
get_user_timeline(&mastodon_config.twitter_screen_name, &token, last_tweet_id)
.await .await
.unwrap_or_else(|e| { .unwrap_or_else(|e| {
panic!( panic!(
@@ -72,18 +86,21 @@ pub async fn run(config: Config) {
// determine if the tweet is part of a thread (response to self) or a standard response // 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 let Some(r) = &tweet.in_reply_to_screen_name {
if r.to_lowercase() != mastodon_config.twitter_screen_name.to_lowercase() { if r.to_lowercase() != mastodon_config.twitter_screen_name.to_lowercase() {
// we are responding not threadin // we are responding not threading
info!("Tweet is a direct response, skipping"); info!("Tweet is a direct response, skipping");
continue; continue;
} }
info!("Tweet is a thread"); info!("Tweet is a thread");
// get the corresponding toot id
let lconn = task_conn.lock().await;
toot_reply_id = read_state( toot_reply_id = read_state(
&conn, &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);
}; };
// build basic status by just yielding text and dereferencing contained urls // build basic status by just yielding text and dereferencing contained urls
@@ -94,7 +111,7 @@ pub async fn run(config: Config) {
if let Some(m) = &tweet.extended_entities { if let Some(m) = &tweet.extended_entities {
for media in &m.media { for media in &m.media {
let local_tweet_media_path = let local_tweet_media_path =
match get_tweet_media(media, &config.scootaloo.cache_path).await { match get_tweet_media(media, &scootaloo_cache_path).await {
Ok(m) => m, Ok(m) => m,
Err(e) => { Err(e) => {
error!("Cannot get tweet media for {}: {}", &media.url, e); error!("Cannot get tweet media for {}: {}", &media.url, e);
@@ -157,8 +174,19 @@ 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;
write_state(&lconn, ttt_towrite)
.unwrap_or_else(|e| panic!("Cant write the last tweet retrieved: {}", e)); .unwrap_or_else(|e| panic!("Cant write the last tweet retrieved: {}", e));
} drop(lconn);
}
});
// push each task into the vec task
mtask.push(task);
}
// launch and wait for every handle
for handle in mtask {
handle.await.unwrap();
} }
} }

View File

@@ -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;
@@ -30,18 +29,13 @@ pub fn get_oauth2_token(config: &TwitterConfig) -> Token {
/// Gets Twitter user timeline /// Gets Twitter user timeline
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>,
) -> 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(20)
true,
false,
token,
)
.with_page_size(200)
.older(lid) .older(lid)
.await?; .await?;