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

116 lines
4.0 KiB
Rust
Raw Normal View History

2023-02-22 18:33:39 +00:00
use std::collections::HashSet;
2023-03-09 19:23:56 +00:00
use std::process::Command;
use mastodon_async::helpers::toml; use mastodon_async::scopes;
2023-07-03 18:42:04 +00:00
use mastodon_async::{prelude::*};
2023-02-22 18:33:39 +00:00
use mastodon_async::{entities::visibility::Visibility};
2023-03-09 21:34:14 +00:00
use mastodon_async::{helpers::cli, Result};
2023-02-22 18:33:39 +00:00
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?
};
2023-07-03 18:42:04 +00:00
let data = Data::default();
let client = Mastodon::from(data);
let statuses = client.statuses(&AccountId::new("user-id"), Default::default()).await.unwrap();
dbg!(statuses);
/*
2023-02-22 18:33:39 +00:00
match get_next_url() {
2023-03-09 19:23:56 +00:00
Some(url) => {
post_image(&mastodon, &url).await;
update_bio(&mastodon).await;
},
2023-02-22 18:33:39 +00:00
None => post(&mastodon, "@Sugui@awoo.fai.st @MeDueleLaTeta@awoo.fai.st me quedé sin chicas").await
2023-07-03 18:42:04 +00:00
};*/
2023-02-22 18:33:39 +00:00
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();
2023-03-09 19:23:56 +00:00
}
async fn update_bio(account: &Mastodon) {
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 remaining = urls.difference(&posted).count();
Command::new("curl")
.args([
"-X", "PATCH",
"https://awoo.fai.st/api/v1/accounts/update_credentials",
"-H", &format!("Authorization:Bearer {}", account.data.token),
"-d", &format!("note=Bot who posts images of sleeping girls every 6 hours.\n\n{} new images remaining", remaining),
]).spawn().unwrap().wait().unwrap();
2023-02-22 18:33:39 +00:00
}
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")
2023-07-03 18:42:04 +00:00
.scopes(Scopes::all())
2023-02-22 18:33:39 +00:00
.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)
}