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