changed curl requests to reqwest

to not depend on curl being installed
This commit is contained in:
Alie 2023-07-19 10:17:09 +02:00
parent 5e175bfcd8
commit 1ebd4ffc38
1 changed files with 22 additions and 25 deletions

View File

@ -1,4 +1,5 @@
use async_std;
use log;
use mastodon_async::entities::visibility::Visibility;
use mastodon_async::helpers::{cli, toml as masto_toml};
use mastodon_async::prelude::*;
@ -6,10 +7,9 @@ use reqwest;
use serde::Deserialize;
use std::collections::HashSet;
use std::io::{Cursor, Write};
use std::process::{exit, Command};
use std::process::exit;
use std::time::Duration;
use toml;
use log;
const CONFIG_FILENAME: &str = "config.toml";
@ -82,7 +82,7 @@ async fn main() -> DynResult<()> {
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);
log::warn!("Failed to revert, retrying: {}", err);
async_std::task::sleep(Duration::new(1, 0)).await;
}
exit(1);
@ -103,7 +103,7 @@ async fn main() -> DynResult<()> {
let mut retry: u8 = 0;
while let Err(err) = post(&mastodon, &config.errors.out_of_images).await {
log::warn!("Cannot post, retry: {}, {}", retry, err);
async_std::task::sleep(Duration::new(1, 0)).await;
async_std::task::sleep(Duration::new(1, 0)).await;
retry += 1;
if retry >= config.errors.retry {
log::error!("Max ammount of retries reached on post");
@ -160,7 +160,7 @@ fn get_next_url(config: &Config) -> DynResult<Option<String>> {
}
async fn post_image(account: &Mastodon, url: &String, config: &Config) -> DynResult<()> {
fetch_url(url.to_string(), &config.files.tempfile).await?; //.expect("Error fetching url");
fetch_url(url, &config.files.tempfile).await?; //.expect("Error fetching url");
let attachment = account
.media(&config.files.tempfile, Some(url.to_string()))
.await?; //.expect("Attachment upload error");
@ -182,26 +182,23 @@ async fn update_bio(account: &Mastodon, config: &Config) -> DynResult<()> {
let posted = binding.lines().collect::<HashSet<&str>>();
let binding = std::fs::read_to_string(&config.files.urls)?; //.expect("Url file not found");
let urls = binding.lines().collect::<HashSet<&str>>();
let remaining = urls.difference(&posted).count();
Command::new("curl")
.args([
"--fail",
"--silent",
"--show-error",
"-X",
"PATCH",
&format!("{}/api/v1/accounts/update_credentials", config.bot.instance),
"-H",
&format!("Authorization:Bearer {}", account.data.token),
"-d",
&format!(
"note={}\n\n{} new images remaining",
config.bot.bio, remaining
),
])
.spawn()? //.expect("Could not spawn curl")
.wait()?; //.expect("Curl failed");
let remaining = urls.difference(&posted).count();
let client = reqwest::Client::new();
let response = client
.patch(&format!(
"{}/api/v1/accounts/update_credentials",
config.bot.instance
))
.bearer_auth(&account.data.token)
.body(format!(
"note={}\n\n{} new images remaining",
config.bot.bio, remaining
))
.send()
.await?;
log::info!("Bio updated, response status: {}", response.status());
Ok(())
}
@ -215,7 +212,7 @@ async fn post(account: &Mastodon, msg: &str) -> DynResult<()> {
Ok(())
}
async fn fetch_url(url: String, file_name: &String) -> DynResult<()> {
async fn fetch_url(url: &String, file_name: &String) -> DynResult<()> {
let response = reqwest::get(url);
let mut file = std::fs::File::create(file_name)?;
let mut content = Cursor::new(response.await?.bytes().await?);