mirror of
https://framagit.org/veretcle/scootaloo.git
synced 2025-07-21 09:31:19 +02:00
Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
ffbe98f838 | ||
![]() |
822f4044c6 | ||
![]() |
78924f6eeb | ||
![]() |
9c14636735 | ||
![]() |
01bac63fb9 | ||
![]() |
4f5663b450 | ||
![]() |
9a9c4b4809 | ||
![]() |
9970968b47 | ||
![]() |
291c86677e |
1203
Cargo.lock
generated
1203
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.8.0"
|
version = "0.8.2"
|
||||||
authors = ["VC <veretcle+framagit@mateu.be>"]
|
authors = ["VC <veretcle+framagit@mateu.be>"]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
43
src/lib.rs
43
src/lib.rs
@@ -52,31 +52,24 @@ pub async fn run(config: Config) {
|
|||||||
|
|
||||||
// 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 =
|
||||||
.unwrap_or_else(|e| panic!("Cannot retrieve last_tweet_id: {}", e))
|
read_state(&lconn, &mastodon_config.twitter_screen_name, None)?.map(|r| r.tweet_id);
|
||||||
.map(|s| s.tweet_id);
|
|
||||||
drop(lconn);
|
drop(lconn);
|
||||||
|
|
||||||
// get Mastodon instance
|
|
||||||
let mastodon = get_mastodon_token(&mastodon_config);
|
|
||||||
|
|
||||||
// get user timeline feed (Vec<tweet>)
|
// get user timeline feed (Vec<tweet>)
|
||||||
let mut feed =
|
let mut feed =
|
||||||
get_user_timeline(&mastodon_config.twitter_screen_name, &token, last_tweet_id)
|
get_user_timeline(&mastodon_config.twitter_screen_name, &token, last_tweet_id)
|
||||||
.await
|
.await?;
|
||||||
.unwrap_or_else(|e| {
|
|
||||||
panic!(
|
|
||||||
"Something went wrong when trying to retrieve {}’s timeline: {}",
|
|
||||||
&mastodon_config.twitter_screen_name, e
|
|
||||||
)
|
|
||||||
});
|
|
||||||
|
|
||||||
// empty feed -> exiting
|
// empty feed -> exiting
|
||||||
if feed.is_empty() {
|
if feed.is_empty() {
|
||||||
info!("Nothing to retrieve since last time, exiting…");
|
info!("Nothing to retrieve since last time, exiting…");
|
||||||
return;
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get Mastodon instance
|
||||||
|
let mastodon = get_mastodon_token(&mastodon_config);
|
||||||
|
|
||||||
// order needs to be chronological
|
// order needs to be chronological
|
||||||
feed.reverse();
|
feed.reverse();
|
||||||
|
|
||||||
@@ -126,15 +119,11 @@ pub async fn run(config: Config) {
|
|||||||
// 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);
|
||||||
|
|
||||||
let status = status_builder
|
let status = status_builder.build()?;
|
||||||
.build()
|
|
||||||
.unwrap_or_else(|_| panic!("Cannot build status with text {}", &status_text));
|
|
||||||
|
|
||||||
// publish status
|
let published_status = mastodon.new_status(status)?;
|
||||||
// again unwrap is safe here as we are in the main thread
|
// this will return if it cannot publish the status preventing the last_tweet from
|
||||||
let published_status = mastodon.new_status(status).unwrap();
|
// being written into db
|
||||||
// this will panic if it cannot publish the status, which is a good thing, it allows the
|
|
||||||
// last_tweet gathered not to be written
|
|
||||||
|
|
||||||
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(),
|
||||||
@@ -144,10 +133,10 @@ 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
|
||||||
let lconn = task_conn.lock().await;
|
let lconn = task_conn.lock().await;
|
||||||
write_state(&lconn, ttt_towrite)
|
write_state(&lconn, ttt_towrite)?;
|
||||||
.unwrap_or_else(|e| panic!("Can’t write the last tweet retrieved: {}", e));
|
|
||||||
drop(lconn);
|
drop(lconn);
|
||||||
}
|
}
|
||||||
|
Ok::<(), ScootalooError>(())
|
||||||
});
|
});
|
||||||
|
|
||||||
// push each task into the vec task
|
// push each task into the vec task
|
||||||
@@ -156,6 +145,10 @@ pub async fn run(config: Config) {
|
|||||||
|
|
||||||
// launch and wait for every handle
|
// launch and wait for every handle
|
||||||
for handle in mtask {
|
for handle in mtask {
|
||||||
handle.await.unwrap();
|
match handle.await {
|
||||||
|
Ok(Err(e)) => eprintln!("Error within thread: {}", e),
|
||||||
|
Err(e) => eprintln!("Error with thread: {}", e),
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -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(()),
|
||||||
}
|
}
|
||||||
|
27
src/util.rs
27
src/util.rs
@@ -45,33 +45,16 @@ pub async fn generate_media_ids(
|
|||||||
let task = tokio::task::spawn(async move {
|
let task = tokio::task::spawn(async move {
|
||||||
info!("Start treating {}", media.media_url_https);
|
info!("Start treating {}", media.media_url_https);
|
||||||
// get the tweet embedded media
|
// get the tweet embedded media
|
||||||
let local_tweet_media_path = match get_tweet_media(&media, &cache_path).await {
|
let local_tweet_media_path = get_tweet_media(&media, &cache_path).await?;
|
||||||
Ok(l) => l,
|
|
||||||
Err(e) => {
|
|
||||||
return Err(ScootalooError::new(&format!(
|
|
||||||
"Cannot get tweet media for {}: {}",
|
|
||||||
&media.url, e
|
|
||||||
)))
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// upload media to Mastodon
|
// upload media to Mastodon
|
||||||
let mastodon_media = mastodon.media(Cow::from(local_tweet_media_path.to_owned()));
|
let mastodon_media =
|
||||||
|
mastodon.media(Cow::from(local_tweet_media_path.to_owned()))?;
|
||||||
// at this point, we can safely erase the original file
|
// at this point, we can safely erase the original file
|
||||||
// it doesn’t matter if we can’t remove, cache_media fn is idempotent
|
// it doesn’t matter if we can’t remove, cache_media fn is idempotent
|
||||||
remove_file(&local_tweet_media_path).await.ok();
|
remove_file(&local_tweet_media_path).await.ok();
|
||||||
|
|
||||||
let mastodon_media = match mastodon_media {
|
Ok::<(usize, String), ScootalooError>((i, mastodon_media.id))
|
||||||
Ok(m) => m,
|
|
||||||
Err(e) => {
|
|
||||||
return Err(ScootalooError::new(&format!(
|
|
||||||
"Attachment {} cannot be uploaded to Mastodon Instance: {}",
|
|
||||||
&local_tweet_media_path, e
|
|
||||||
)))
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok((i, mastodon_media.id))
|
|
||||||
});
|
});
|
||||||
|
|
||||||
tasks.push(task);
|
tasks.push(task);
|
||||||
@@ -81,7 +64,7 @@ pub async fn generate_media_ids(
|
|||||||
match task.await {
|
match task.await {
|
||||||
// insert the media at the right place
|
// insert the media at the right place
|
||||||
Ok(Ok((i, v))) => media_ids[i] = v,
|
Ok(Ok((i, v))) => media_ids[i] = v,
|
||||||
Ok(Err(e)) => warn!("{}", e),
|
Ok(Err(e)) => warn!("Cannot treat media: {}", e),
|
||||||
Err(e) => error!("Something went wrong when joining the main thread: {}", e),
|
Err(e) => error!("Something went wrong when joining the main thread: {}", e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user