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";
@ -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",
&format!(
"note={}\n\n{} new images remaining", "note={}\n\n{} new images remaining",
config.bot.bio, remaining config.bot.bio, remaining
), ))
]) .send()
.spawn()? //.expect("Could not spawn curl") .await?;
.wait()?; //.expect("Curl failed"); log::info!("Bio updated, response status: {}", response.status());
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?);