From 1ebd4ffc38d0e9ba73e9cbf81f1dc9f85a08037f Mon Sep 17 00:00:00 2001 From: Alie Date: Wed, 19 Jul 2023 10:17:09 +0200 Subject: [PATCH] changed curl requests to reqwest to not depend on curl being installed --- src/main.rs | 47 ++++++++++++++++++++++------------------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9b949b8..78293b0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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> { } 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::>(); let binding = std::fs::read_to_string(&config.files.urls)?; //.expect("Url file not found"); let urls = binding.lines().collect::>(); - 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?);