DailyWikiHuman/src/main.rs
2024-12-28 17:02:22 -05:00

48 lines
1.4 KiB
Rust

use std::path::Path;
mod file;
mod wikipedia;
mod mastodon;
mod server;
use file::{FileError, LinksFile};
use mastodon::{Mastodon, Visibility};
use wikipedia::post_body;
async fn post_link_from_file(queue: LinksFile<'_>, backup: LinksFile<'_>, mastodon: Mastodon) {
// get link to post
let link_to_post = match queue.remove_first_line() {
Ok(a) => a,
Err(FileError::EmptyLine) => match backup.remove_first_line() {
Ok(b) => b,
Err(c) => panic!("{c:?}")
},
Err(d) => panic!("{d:?}")
};
// convert said link into a post status
let post_status = post_body(link_to_post).await.unwrap();
// post it on mastodon
mastodon.post_text_status(post_status, Visibility::Direct).await.unwrap();
}
#[tokio::main]
async fn main() {
dotenv::dotenv().ok();
// let app_code = std::env::var("APP_CODE").expect("APP_CODE required.");
let app_token = std::env::var("APP_TOKEN").expect("APP_TOKEN required.");
let app_instance = std::env::var("APP_INSTANCE").expect("APP_INSTANCE required.");
let queue = LinksFile::new(Path::new("queue.txt"));
let backup = LinksFile::new(Path::new("backup.txt"));
let mastodon = Mastodon::new(app_instance, app_token);
let mut interval_timer = tokio::time::interval(
chrono::Duration::minutes(10).to_std().unwrap()
);
loop {
interval_timer.tick().await;
post_link_from_file(queue, backup, mastodon.clone()).await;
}
}