|
|
@ -2,11 +2,27 @@ 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::*;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::collections::HashSet;
|
|
|
|
use std::io::Cursor;
|
|
|
|
use std::io::{Cursor, Write};
|
|
|
|
|
|
|
|
use std::process::exit;
|
|
|
|
use std::process::exit;
|
|
|
|
use std::time::Duration;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
|
|
|
struct ImagesWrap {
|
|
|
|
|
|
|
|
images: Vec<Image>,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize, Clone, Eq, PartialEq, Debug)]
|
|
|
|
|
|
|
|
struct Image {
|
|
|
|
|
|
|
|
url: String,
|
|
|
|
|
|
|
|
_id: String,
|
|
|
|
|
|
|
|
status: String,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
|
|
|
|
struct ImageStatus {
|
|
|
|
|
|
|
|
status: String,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[derive(Serialize)]
|
|
|
|
struct AccountUpdate {
|
|
|
|
struct AccountUpdate {
|
|
|
|
note: String,
|
|
|
|
note: String,
|
|
|
@ -19,8 +35,14 @@ type DynResult<T> = Result<T, Box<dyn std::error::Error>>;
|
|
|
|
#[derive(Deserialize, Serialize, Default)]
|
|
|
|
#[derive(Deserialize, Serialize, Default)]
|
|
|
|
struct Config {
|
|
|
|
struct Config {
|
|
|
|
bot: Bot,
|
|
|
|
bot: Bot,
|
|
|
|
files: Files,
|
|
|
|
backend: Backend,
|
|
|
|
errors: Errors,
|
|
|
|
errors: Errors,
|
|
|
|
|
|
|
|
files: Files,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize, Default)]
|
|
|
|
|
|
|
|
struct Files {
|
|
|
|
|
|
|
|
tempfile: String,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize, Default)]
|
|
|
|
#[derive(Deserialize, Serialize, Default)]
|
|
|
@ -31,10 +53,10 @@ struct Bot {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize, Default)]
|
|
|
|
#[derive(Deserialize, Serialize, Default)]
|
|
|
|
struct Files {
|
|
|
|
struct Backend {
|
|
|
|
posted: String,
|
|
|
|
url: String,
|
|
|
|
urls: String,
|
|
|
|
app: String,
|
|
|
|
tempfile: String,
|
|
|
|
secret: String,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize, Default)]
|
|
|
|
#[derive(Deserialize, Serialize, Default)]
|
|
|
@ -56,11 +78,12 @@ async fn main() -> DynResult<()> {
|
|
|
|
let config: Config = get_config();
|
|
|
|
let config: Config = get_config();
|
|
|
|
let mastodon = get_account(&config).await;
|
|
|
|
let mastodon = get_account(&config).await;
|
|
|
|
|
|
|
|
|
|
|
|
match get_next_url(&config) {
|
|
|
|
match get_next_url(&config).await {
|
|
|
|
Ok(url) => match url {
|
|
|
|
Ok(image) => match image {
|
|
|
|
Some(url) => {
|
|
|
|
Some(image) => {
|
|
|
|
let mut retry: u8 = 0;
|
|
|
|
let mut retry: u8 = 0;
|
|
|
|
while let Err(err) = post_image(&mastodon, &url, &config, Visibility::Unlisted).await {
|
|
|
|
while let Err(err) = post_image(&mastodon, &image.url, &config, Visibility::Unlisted).await
|
|
|
|
|
|
|
|
{
|
|
|
|
log::warn!("Cannot post image, retry: {}, {}", retry, err);
|
|
|
|
log::warn!("Cannot post image, 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;
|
|
|
@ -70,7 +93,7 @@ async fn main() -> DynResult<()> {
|
|
|
|
exit(1);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set_url_as_posted(&config, &url)?;
|
|
|
|
set_url_as_posted(&config, &image).await?;
|
|
|
|
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);
|
|
|
@ -139,7 +162,7 @@ fn get_config() -> Config {
|
|
|
|
fn generate_config() -> DynResult<()> {
|
|
|
|
fn generate_config() -> DynResult<()> {
|
|
|
|
let config = Config::default();
|
|
|
|
let config = Config::default();
|
|
|
|
let config = toml::to_string(&config)?;
|
|
|
|
let config = toml::to_string(&config)?;
|
|
|
|
std::fs::write(CONFIG_FILENAME, &config)?;
|
|
|
|
std::fs::write(CONFIG_FILENAME, config)?;
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -163,28 +186,67 @@ async fn get_account(config: &Config) -> Mastodon {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async fn get_next_url(config: &Config) -> DynResult<Option<Image>> {
|
|
|
|
|
|
|
|
let images: ImagesWrap = reqwest::get(format!(
|
|
|
|
|
|
|
|
"{}/images?status=available&limit=1",
|
|
|
|
|
|
|
|
config.backend.url
|
|
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
.await?
|
|
|
|
|
|
|
|
.json()
|
|
|
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
|
|
fn get_next_url(config: &Config) -> DynResult<Option<String>> {
|
|
|
|
let images = images.images;
|
|
|
|
let binding = std::fs::read_to_string(&config.files.posted)?; //.expect("Posted file not found");
|
|
|
|
|
|
|
|
let posted = binding.lines().collect::<HashSet<&str>>();
|
|
|
|
if images.is_empty() {
|
|
|
|
let binding = std::fs::read_to_string(&config.files.urls)?; //.expect("Urls file not found");
|
|
|
|
|
|
|
|
let urls = binding.lines().collect::<HashSet<&str>>();
|
|
|
|
|
|
|
|
let urls = urls.difference(&posted).collect::<Vec<_>>();
|
|
|
|
|
|
|
|
if urls.is_empty() {
|
|
|
|
|
|
|
|
Ok(None)
|
|
|
|
Ok(None)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
Ok(Some(urls[0].to_string().clone()))
|
|
|
|
Ok(Some(images[0].clone()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn set_url_as_posted(config: &Config, url: &String) -> DynResult<()> {
|
|
|
|
#[derive(Serialize)]
|
|
|
|
let mut file = std::fs::OpenOptions::new()
|
|
|
|
struct Auth {
|
|
|
|
.write(true)
|
|
|
|
app: String,
|
|
|
|
.append(true) // This is needed to append to file
|
|
|
|
secret: String,
|
|
|
|
.open(&config.files.posted)?; //.expect("Cannot open posted file"); // Maybe we should retry just in case
|
|
|
|
}
|
|
|
|
writeln!(file, "{}", url)?; //.expect("Cannot write to posted file"); // maybe we should retry tbh
|
|
|
|
|
|
|
|
log::info!("Set url {} as posted", url);
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
|
|
|
struct Token {
|
|
|
|
|
|
|
|
token: String,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async fn set_url_as_posted(config: &Config, image: &Image) -> DynResult<()> {
|
|
|
|
|
|
|
|
let client = reqwest::Client::new();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let auth = &Auth {
|
|
|
|
|
|
|
|
app: config.backend.app.to_string(),
|
|
|
|
|
|
|
|
secret: config.backend.secret.to_string(),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let token: Token = client
|
|
|
|
|
|
|
|
.post(&format!("{}/login", config.backend.url))
|
|
|
|
|
|
|
|
.json(auth)
|
|
|
|
|
|
|
|
.send()
|
|
|
|
|
|
|
|
.await?
|
|
|
|
|
|
|
|
.json()
|
|
|
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let response = client
|
|
|
|
|
|
|
|
.put(&format!("{}/images/{}", config.backend.url, image._id))
|
|
|
|
|
|
|
|
.bearer_auth(token.token)
|
|
|
|
|
|
|
|
.json(&ImageStatus {
|
|
|
|
|
|
|
|
status: "consumed".to_string(),
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.send()
|
|
|
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
if response.status().is_success() {
|
|
|
|
|
|
|
|
log::info!("Set url {} as posted", image.url);
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
let error = response.text().await?;
|
|
|
|
|
|
|
|
log::error!("Set url response: {}", error);
|
|
|
|
|
|
|
|
Err(error.into())
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn post_image(
|
|
|
|
async fn post_image(
|
|
|
@ -211,12 +273,12 @@ async fn post_image(
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn update_bio(account: &Mastodon, config: &Config) -> DynResult<()> {
|
|
|
|
async fn update_bio(account: &Mastodon, config: &Config) -> DynResult<()> {
|
|
|
|
let binding = std::fs::read_to_string(&config.files.posted)?; //.expect("Posted file not found");
|
|
|
|
let images: ImagesWrap = reqwest::get(format!("{}/images?status=available", config.backend.url))
|
|
|
|
let posted = binding.lines().collect::<HashSet<&str>>();
|
|
|
|
.await?
|
|
|
|
let binding = std::fs::read_to_string(&config.files.urls)?; //.expect("Url file not found");
|
|
|
|
.json()
|
|
|
|
let urls = binding.lines().collect::<HashSet<&str>>();
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
|
|
let remaining = urls.difference(&posted).count();
|
|
|
|
let remaining = images.images.len();
|
|
|
|
let client = reqwest::Client::new();
|
|
|
|
let client = reqwest::Client::new();
|
|
|
|
|
|
|
|
|
|
|
|
let account_update = AccountUpdate {
|
|
|
|
let account_update = AccountUpdate {
|
|
|
@ -333,35 +395,93 @@ mod tests {
|
|
|
|
assert_eq!(response.status(), StatusCode::OK)
|
|
|
|
assert_eq!(response.status(), StatusCode::OK)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[tokio::test]
|
|
|
|
fn set_as_posted_works() {
|
|
|
|
async fn set_as_posted_works() {
|
|
|
|
let config = get_config();
|
|
|
|
let config = get_config();
|
|
|
|
|
|
|
|
const IMAGE: &str = "https://picsum.photos/id/2";
|
|
|
|
|
|
|
|
let expected = insert_image(&config, IMAGE).await.unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
std::fs::write(&config.files.posted, "").unwrap();
|
|
|
|
set_url_as_posted(&config, &expected).await.unwrap();
|
|
|
|
|
|
|
|
let image: ImageWrap = reqwest::get(format!("{}/images/{}", config.backend.url, expected._id))
|
|
|
|
set_url_as_posted(&config, &TEST_URL.to_string()).unwrap();
|
|
|
|
.await
|
|
|
|
let file = std::fs::read_to_string(&config.files.posted).unwrap();
|
|
|
|
.unwrap()
|
|
|
|
let url = file.lines().next_back().unwrap();
|
|
|
|
.json()
|
|
|
|
assert_eq!(url, TEST_URL)
|
|
|
|
.await
|
|
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let image = image.image;
|
|
|
|
|
|
|
|
assert_eq!(image.status, "consumed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[tokio::test]
|
|
|
|
fn get_next_url_works() {
|
|
|
|
async fn get_next_url_works() {
|
|
|
|
let config = get_config();
|
|
|
|
let config = get_config();
|
|
|
|
// Reset file
|
|
|
|
|
|
|
|
std::fs::write(&config.files.posted, "").unwrap();
|
|
|
|
const IMAGE: &str = "https://picsum.photos/id/1";
|
|
|
|
|
|
|
|
let expected = insert_image(&config, IMAGE).await.unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
// Get test url
|
|
|
|
// Get test url
|
|
|
|
let url = get_next_url(&config).unwrap().unwrap();
|
|
|
|
let image = get_next_url(&config).await.unwrap().unwrap();
|
|
|
|
assert_eq!(url, TEST_URL);
|
|
|
|
assert_eq!(image.url, IMAGE);
|
|
|
|
|
|
|
|
|
|
|
|
set_url_as_posted(&config, &TEST_URL.to_string()).unwrap();
|
|
|
|
set_url_as_posted(&config, &expected).await.unwrap();
|
|
|
|
let file = std::fs::read_to_string(&config.files.posted).unwrap();
|
|
|
|
let image: ImageWrap = reqwest::get(format!("{}/images/{}", config.backend.url, expected._id))
|
|
|
|
let url = file.lines().next_back().unwrap();
|
|
|
|
.await
|
|
|
|
assert_eq!(url, TEST_URL);
|
|
|
|
.unwrap()
|
|
|
|
|
|
|
|
.json()
|
|
|
|
|
|
|
|
.await
|
|
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let image = image.image;
|
|
|
|
|
|
|
|
assert_eq!(image.status, "consumed");
|
|
|
|
|
|
|
|
|
|
|
|
// Test that now it does not get it
|
|
|
|
// Test that now it does not get it
|
|
|
|
let url = get_next_url(&config).unwrap();
|
|
|
|
let image = get_next_url(&config).await.unwrap();
|
|
|
|
assert_eq!(url, None);
|
|
|
|
assert_eq!(image, None);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async fn insert_image(config: &Config, url: &str) -> DynResult<Image> {
|
|
|
|
|
|
|
|
let client = reqwest::Client::new();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let auth = &Auth {
|
|
|
|
|
|
|
|
app: config.backend.app.to_string(),
|
|
|
|
|
|
|
|
secret: config.backend.secret.to_string(),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let token: Token = client
|
|
|
|
|
|
|
|
.post(&format!("{}/login", config.backend.url))
|
|
|
|
|
|
|
|
.json(auth)
|
|
|
|
|
|
|
|
.send()
|
|
|
|
|
|
|
|
.await?
|
|
|
|
|
|
|
|
.json()
|
|
|
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let response = client
|
|
|
|
|
|
|
|
.post(&format!("{}/images", config.backend.url))
|
|
|
|
|
|
|
|
.bearer_auth(&token.token)
|
|
|
|
|
|
|
|
.json(&Insert {
|
|
|
|
|
|
|
|
url: url.to_string(),
|
|
|
|
|
|
|
|
status: "available".to_string(),
|
|
|
|
|
|
|
|
tags: vec![],
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.send()
|
|
|
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if response.status().is_success() {
|
|
|
|
|
|
|
|
let response: ImageWrap = response.json().await?;
|
|
|
|
|
|
|
|
return Ok(response.image);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
dbg!(response.text().await?);
|
|
|
|
|
|
|
|
panic!();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
|
|
|
|
struct Insert {
|
|
|
|
|
|
|
|
url: String,
|
|
|
|
|
|
|
|
status: String,
|
|
|
|
|
|
|
|
tags: Vec<String>,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
|
|
|
struct ImageWrap {
|
|
|
|
|
|
|
|
image: Image,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|