Refactor code and moved functionality to a funtion

This commit is contained in:
Alie 2024-01-26 17:43:46 +01:00
parent c2d1b9ee37
commit 0001567b55
1 changed files with 19 additions and 28 deletions

View File

@ -1,5 +1,5 @@
use async_std;
use log;
use log::{self, info};
use mastodon_async::entities::visibility::Visibility;
use mastodon_async::helpers::{cli, toml as masto_toml};
use mastodon_async::prelude::*;
@ -71,13 +71,10 @@ async fn main() -> DynResult<()> {
if retry >= config.errors.retry {
log::error!("Max ammount of retries reached on post_image");
log::info!("Reverting file changes");
while let Err(err) = pop_last_line_of_file(&config.files.posted) {
log::warn!("Failed to revert, retrying: {}", err);
async_std::task::sleep(Duration::new(1, 0)).await;
}
exit(1);
}
}
set_url_as_posted(&config, &url)?;
let mut retry: u8 = 0;
while let Err(err) = update_bio(&mastodon, &config).await {
log::warn!("Cannot update bio, retry: {}, {}", retry, err);
@ -161,15 +158,6 @@ fn parse_config(filename: &str) -> DynResult<Config> {
Ok(toml::from_str(&toml_file)?) //("Malformed config file, check the original one for reference")
}
fn pop_last_line_of_file(filename: &str) -> DynResult<()> {
let binding = std::fs::read_to_string(filename)?;
let mut posted: Vec<_> = binding.lines().collect();
posted.pop();
std::fs::write(filename, posted.concat())?;
log::info!("Success reverting changes");
Ok(())
}
fn get_next_url(config: &Config) -> DynResult<Option<String>> {
let binding = std::fs::read_to_string(&config.files.posted)?; //.expect("Posted file not found");
let posted = binding.lines().collect::<HashSet<&str>>();
@ -179,15 +167,20 @@ fn get_next_url(config: &Config) -> DynResult<Option<String>> {
if urls.is_empty() {
Ok(None)
} else {
let mut file = std::fs::OpenOptions::new()
.write(true)
.append(true) // This is needed to append to file
.open(&config.files.posted)?; //.expect("Cannot open posted file"); // Maybe we should retry just in case
write!(file, "{}\n", urls[0])?; //.expect("Cannot write to posted file"); // maybe we should retry tbh
Ok(Some(urls[0].to_string().clone()))
}
}
fn set_url_as_posted(config: &Config, url: &String) -> DynResult<()> {
let mut file = std::fs::OpenOptions::new()
.write(true)
.append(true) // This is needed to append to file
.open(&config.files.posted)?; //.expect("Cannot open posted file"); // Maybe we should retry just in case
write!(file, "{}\n", url)?; //.expect("Cannot write to posted file"); // maybe we should retry tbh
info!("Set url {} as posted", url);
Ok(())
}
async fn post_image(
account: &Mastodon,
url: &String,
@ -328,17 +321,15 @@ mod tests {
let response = account.get_status(&status.id).await.unwrap();
account.delete_status(&status.id).await.unwrap();
let attachment = &response.media_attachments[0];
let attachment = &response.media_attachments[0];
let response = client
.get(attachment.url.clone().unwrap())
.bearer_auth(dbg!(&account.data.token))
.send()
.await
.unwrap();
let response = client
.get(attachment.url.clone().unwrap())
.bearer_auth(dbg!(&account.data.token))
.send()
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK)
}