|
|
|
@ -2,11 +2,27 @@ use mastodon_async::entities::visibility::Visibility;
|
|
|
|
|
use mastodon_async::helpers::{cli, toml as masto_toml};
|
|
|
|
|
use mastodon_async::prelude::*;
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
use std::collections::HashSet;
|
|
|
|
|
use std::io::{Cursor, Write};
|
|
|
|
|
use std::io::Cursor;
|
|
|
|
|
use std::process::exit;
|
|
|
|
|
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)]
|
|
|
|
|
struct AccountUpdate {
|
|
|
|
|
note: String,
|
|
|
|
@ -19,8 +35,14 @@ type DynResult<T> = Result<T, Box<dyn std::error::Error>>;
|
|
|
|
|
#[derive(Deserialize, Serialize, Default)]
|
|
|
|
|
struct Config {
|
|
|
|
|
bot: Bot,
|
|
|
|
|
files: Files,
|
|
|
|
|
backend: Backend,
|
|
|
|
|
errors: Errors,
|
|
|
|
|
files: Files,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize, Default)]
|
|
|
|
|
struct Files {
|
|
|
|
|
tempfile: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize, Default)]
|
|
|
|
@ -31,10 +53,10 @@ struct Bot {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize, Default)]
|
|
|
|
|
struct Files {
|
|
|
|
|
posted: String,
|
|
|
|
|
urls: String,
|
|
|
|
|
tempfile: String,
|
|
|
|
|
struct Backend {
|
|
|
|
|
url: String,
|
|
|
|
|
app: String,
|
|
|
|
|
secret: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize, Default)]
|
|
|
|
@ -56,11 +78,12 @@ async fn main() -> DynResult<()> {
|
|
|
|
|
let config: Config = get_config();
|
|
|
|
|
let mastodon = get_account(&config).await;
|
|
|
|
|
|
|
|
|
|
match get_next_url(&config) {
|
|
|
|
|
Ok(url) => match url {
|
|
|
|
|
Some(url) => {
|
|
|
|
|
match get_next_url(&config).await {
|
|
|
|
|
Ok(image) => match image {
|
|
|
|
|
Some(image) => {
|
|
|
|
|
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);
|
|
|
|
|
async_std::task::sleep(Duration::new(1, 0)).await;
|
|
|
|
|
retry += 1;
|
|
|
|
@ -70,7 +93,7 @@ async fn main() -> DynResult<()> {
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
set_url_as_posted(&config, &url)?;
|
|
|
|
|
set_url_as_posted(&config, &image).await?;
|
|
|
|
|
let mut retry: u8 = 0;
|
|
|
|
|
while let Err(err) = update_bio(&mastodon, &config).await {
|
|
|
|
|
log::warn!("Cannot update bio, retry: {}, {}", retry, err);
|
|
|
|
@ -139,7 +162,7 @@ fn get_config() -> Config {
|
|
|
|
|
fn generate_config() -> DynResult<()> {
|
|
|
|
|
let config = Config::default();
|
|
|
|
|
let config = toml::to_string(&config)?;
|
|
|
|
|
std::fs::write(CONFIG_FILENAME, &config)?;
|
|
|
|
|
std::fs::write(CONFIG_FILENAME, config)?;
|
|
|
|
|
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 binding = std::fs::read_to_string(&config.files.posted)?; //.expect("Posted file not found");
|
|
|
|
|
let posted = binding.lines().collect::<HashSet<&str>>();
|
|
|
|
|
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() {
|
|
|
|
|
let images = images.images;
|
|
|
|
|
|
|
|
|
|
if images.is_empty() {
|
|
|
|
|
Ok(None)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(Some(urls[0].to_string().clone()))
|
|
|
|
|
Ok(Some(images[0].clone()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn set_url_as_posted(config: &Config, url: &String) -> DynResult<()> {
|
|
|
|
|
let mut file = std::fs::OpenOptions::new()
|
|
|
|
|
.write(true)
|
|
|
|
|
.append(true) // This is needed to append to file
|
|
|
|
|
.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(Serialize)]
|
|
|
|
|
struct Auth {
|
|
|
|
|
app: String,
|
|
|
|
|
secret: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[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(())
|
|
|
|
|
} else {
|
|
|
|
|
let error = response.text().await?;
|
|
|
|
|
log::error!("Set url response: {}", error);
|
|
|
|
|
Err(error.into())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn post_image(
|
|
|
|
@ -211,12 +273,12 @@ async fn post_image(
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 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 images: ImagesWrap = reqwest::get(format!("{}/images?status=available", config.backend.url))
|
|
|
|
|
.await?
|
|
|
|
|
.json()
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
let remaining = urls.difference(&posted).count();
|
|
|
|
|
let remaining = images.images.len();
|
|
|
|
|
let client = reqwest::Client::new();
|
|
|
|
|
|
|
|
|
|
let account_update = AccountUpdate {
|
|
|
|
@ -333,35 +395,93 @@ mod tests {
|
|
|
|
|
assert_eq!(response.status(), StatusCode::OK)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn set_as_posted_works() {
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn set_as_posted_works() {
|
|
|
|
|
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, &TEST_URL.to_string()).unwrap();
|
|
|
|
|
let file = std::fs::read_to_string(&config.files.posted).unwrap();
|
|
|
|
|
let url = file.lines().next_back().unwrap();
|
|
|
|
|
assert_eq!(url, TEST_URL)
|
|
|
|
|
set_url_as_posted(&config, &expected).await.unwrap();
|
|
|
|
|
let image: ImageWrap = reqwest::get(format!("{}/images/{}", config.backend.url, expected._id))
|
|
|
|
|
.await
|
|
|
|
|
.unwrap()
|
|
|
|
|
.json()
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
let image = image.image;
|
|
|
|
|
assert_eq!(image.status, "consumed");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn get_next_url_works() {
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn get_next_url_works() {
|
|
|
|
|
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
|
|
|
|
|
let url = get_next_url(&config).unwrap().unwrap();
|
|
|
|
|
assert_eq!(url, TEST_URL);
|
|
|
|
|
let image = get_next_url(&config).await.unwrap().unwrap();
|
|
|
|
|
assert_eq!(image.url, IMAGE);
|
|
|
|
|
|
|
|
|
|
set_url_as_posted(&config, &TEST_URL.to_string()).unwrap();
|
|
|
|
|
let file = std::fs::read_to_string(&config.files.posted).unwrap();
|
|
|
|
|
let url = file.lines().next_back().unwrap();
|
|
|
|
|
assert_eq!(url, TEST_URL);
|
|
|
|
|
set_url_as_posted(&config, &expected).await.unwrap();
|
|
|
|
|
let image: ImageWrap = reqwest::get(format!("{}/images/{}", config.backend.url, expected._id))
|
|
|
|
|
.await
|
|
|
|
|
.unwrap()
|
|
|
|
|
.json()
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
let image = image.image;
|
|
|
|
|
assert_eq!(image.status, "consumed");
|
|
|
|
|
|
|
|
|
|
// Test that now it does not get it
|
|
|
|
|
let url = get_next_url(&config).unwrap();
|
|
|
|
|
assert_eq!(url, None);
|
|
|
|
|
let image = get_next_url(&config).await.unwrap();
|
|
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|