Refactor code and moved functionality to a funtion
This commit is contained in:
parent
c2d1b9ee37
commit
0001567b55
47
src/main.rs
47
src/main.rs
|
@ -1,5 +1,5 @@
|
||||||
use async_std;
|
use async_std;
|
||||||
use log;
|
use log::{self, info};
|
||||||
use mastodon_async::entities::visibility::Visibility;
|
use mastodon_async::entities::visibility::Visibility;
|
||||||
use mastodon_async::helpers::{cli, toml as masto_toml};
|
use mastodon_async::helpers::{cli, toml as masto_toml};
|
||||||
use mastodon_async::prelude::*;
|
use mastodon_async::prelude::*;
|
||||||
|
@ -71,13 +71,10 @@ async fn main() -> DynResult<()> {
|
||||||
if retry >= config.errors.retry {
|
if retry >= config.errors.retry {
|
||||||
log::error!("Max ammount of retries reached on post_image");
|
log::error!("Max ammount of retries reached on post_image");
|
||||||
log::info!("Reverting file changes");
|
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);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
set_url_as_posted(&config, &url)?;
|
||||||
let mut retry: u8 = 0;
|
let mut retry: u8 = 0;
|
||||||
while let Err(err) = update_bio(&mastodon, &config).await {
|
while let Err(err) = update_bio(&mastodon, &config).await {
|
||||||
log::warn!("Cannot update bio, retry: {}, {}", retry, err);
|
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")
|
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>> {
|
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 binding = std::fs::read_to_string(&config.files.posted)?; //.expect("Posted file not found");
|
||||||
let posted = binding.lines().collect::<HashSet<&str>>();
|
let posted = binding.lines().collect::<HashSet<&str>>();
|
||||||
|
@ -179,15 +167,20 @@ fn get_next_url(config: &Config) -> DynResult<Option<String>> {
|
||||||
if urls.is_empty() {
|
if urls.is_empty() {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
} else {
|
} 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()))
|
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(
|
async fn post_image(
|
||||||
account: &Mastodon,
|
account: &Mastodon,
|
||||||
url: &String,
|
url: &String,
|
||||||
|
@ -328,17 +321,15 @@ mod tests {
|
||||||
|
|
||||||
let response = account.get_status(&status.id).await.unwrap();
|
let response = account.get_status(&status.id).await.unwrap();
|
||||||
|
|
||||||
|
|
||||||
account.delete_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
|
||||||
let response = client
|
.get(attachment.url.clone().unwrap())
|
||||||
.get(attachment.url.clone().unwrap())
|
.bearer_auth(dbg!(&account.data.token))
|
||||||
.bearer_auth(dbg!(&account.data.token))
|
.send()
|
||||||
.send()
|
.await
|
||||||
.await
|
.unwrap();
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(response.status(), StatusCode::OK)
|
assert_eq!(response.status(), StatusCode::OK)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue