mastodon-image-uploader-bot/src/main.rs

88 lines
2.9 KiB
Rust

use std::collections::HashSet;
use mastodon_async::helpers::toml; // requires `features = ["toml"]`
use mastodon_async::prelude::*;
use mastodon_async::{entities::visibility::Visibility};
use mastodon_async::{helpers::cli, Result};
use reqwest;
use std::io::Cursor;
use std::io::Write;
#[tokio::main] // requires `features = ["mt"]
async fn main() -> Result<()> {
let mastodon = if let Ok(data) = toml::from_file("mastodon-data.toml") {
Mastodon::from(data)
} else {
register().await?
};
match get_next_url() {
Some(url) => post_image(&mastodon, &url).await,
None => post(&mastodon, "@Sugui@awoo.fai.st @MeDueleLaTeta@awoo.fai.st me quedé sin chicas").await
};
Ok(())
}
fn get_next_url() -> Option<String> {
let binding = std::fs::read("./posted.csv").expect("File not found").iter().map(|c| *c as char).collect::<String>();
let posted = binding.lines().collect::<HashSet<_>>();
let binding = std::fs::read("./urls.csv").expect("File not found").iter().map(|c| *c as char).collect::<String>();
let urls = binding.lines().collect::<HashSet<_>>();
let urls = urls.difference(&posted).collect::<Vec<_>>();
if urls.is_empty() {
None
} else {
let mut file = std::fs::OpenOptions::new()
.write(true)
.append(true) // This is needed to append to file
.open("./posted.csv")
.unwrap();
write!(file, "{}\n", urls[0]).unwrap();
Some(urls[0].to_string().clone())
}
}
async fn post_image(account: &Mastodon, url: &String) {
fetch_url(url.to_string(), ".tmp".to_string()).await.unwrap();
let attachment = account.media("./.tmp", Some(url.to_string())).await.expect("upload");
let attachment = account.wait_for_processing(attachment, Default::default()).await.expect("processing");
let status = StatusBuilder::new()
.media_ids(&[attachment.id])
.visibility(Visibility::Unlisted)
.sensitive(true)
.build()
.unwrap();
account.new_status(status).await.unwrap();
}
async fn post(account: &Mastodon, msg: &str) {
let status = StatusBuilder::new()
.visibility(Visibility::Direct)
.status(msg)
.build()
.unwrap();
account.new_status(status).await.unwrap();
}
async fn fetch_url(url: String, file_name: String) -> Result<()> {
let response = reqwest::get(url).await?;
let mut file = std::fs::File::create(file_name)?;
let mut content = Cursor::new(response.bytes().await?);
std::io::copy(&mut content, &mut file)?;
Ok(())
}
async fn register() -> Result<Mastodon> {
let registration = Registration::new("https://awoo.fai.st")
.client_name("sleeping-girls-bot")
.scopes(Scopes::all())
.build()
.await?;
let mastodon = cli::authenticate(registration).await?;
// Save app data for using on the next run.
toml::to_file(&mastodon.data, "mastodon-data.toml")?;
Ok(mastodon)
}