first commit

This commit is contained in:
Suguivy 2023-02-22 19:33:39 +01:00
commit 950fe918b8
4 changed files with 1647 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
/target
mastodon-data.toml
*.csv
.tmp

1541
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

15
Cargo.toml Normal file
View File

@ -0,0 +1,15 @@
[package]
name = "mastodon-image-uploader-bot"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
tokio-test = "0.4.2"
reqwest = "0.11.14"
[dependencies.mastodon-async]
version = "1.0"
features = ["toml", "mt"]

87
src/main.rs Normal file
View File

@ -0,0 +1,87 @@
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)
}