From 71da65576bd81626d53736fe410ebb336ef576dc Mon Sep 17 00:00:00 2001 From: vorboyvo Date: Mon, 30 Dec 2024 14:26:53 -0500 Subject: [PATCH] Full project done. --- Cargo.lock | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + queue.txt | 1 + src/file.rs | 28 +++++++++++++++++++++---- src/main.rs | 42 +++++++++++++++++++++++-------------- src/server.rs | 3 +-- 6 files changed, 111 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 238defb..4b30fae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -215,6 +215,15 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" +[[package]] +name = "croner" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38fd53511eaf0b00a185613875fee58b208dfce016577d0ad4bb548e1c4fb3ee" +dependencies = [ + "chrono", +] + [[package]] name = "daily_wiki_human" version = "0.1.0" @@ -227,6 +236,7 @@ dependencies = [ "reqwest", "serde", "tokio", + "tokio-cron-scheduler", ] [[package]] @@ -817,6 +827,17 @@ dependencies = [ "tempfile", ] +[[package]] +name = "num-derive" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -1362,6 +1383,21 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "tokio-cron-scheduler" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a5597b569b4712cf78aa0c9ae29742461b7bda1e49c2a5fdad1d79bf022f8f0" +dependencies = [ + "chrono", + "croner", + "num-derive", + "num-traits", + "tokio", + "tracing", + "uuid", +] + [[package]] name = "tokio-macros" version = "2.4.0" @@ -1442,9 +1478,21 @@ checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" dependencies = [ "log", "pin-project-lite", + "tracing-attributes", "tracing-core", ] +[[package]] +name = "tracing-attributes" +version = "0.1.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "395ae124c09f9e6918a2310af6038fba074bcf474ac352496d5910dd59a2226d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tracing-core" version = "0.1.33" @@ -1501,6 +1549,15 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" +[[package]] +name = "uuid" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8c5f0a0af699448548ad1a2fbf920fb4bee257eae39953ba95cb84891a0446a" +dependencies = [ + "getrandom", +] + [[package]] name = "vcpkg" version = "0.2.15" diff --git a/Cargo.toml b/Cargo.toml index ef77ea6..42a64ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,4 @@ regex = "1.11.1" reqwest = { version = "0.12.11", features = ['blocking', 'multipart'] } serde = { version = "1.0.217", features = ["derive"] } tokio = { version = "1", features = ["full"] } +tokio-cron-scheduler = "0.13.0" diff --git a/queue.txt b/queue.txt index e02dd40..2589a05 100644 --- a/queue.txt +++ b/queue.txt @@ -1,2 +1,3 @@ https://en.wikipedia.org/wiki/GNU/Linux_naming_controversy https://en.wikipedia.org/wiki/Buck-a-beer?wprov=sfla1 + diff --git a/src/file.rs b/src/file.rs index 78f5aab..ffdfa60 100644 --- a/src/file.rs +++ b/src/file.rs @@ -35,8 +35,11 @@ impl<'a> LinksFile<'a> { pub fn remove_first_line(&self) -> Result { let file_contents = read_to_string(self.0)?; let mut file_lines = file_contents.split("\n"); - let first_line = file_lines.next(); - let first_line_clean = match first_line { + let mut line = file_lines.next(); + while let Some("") = line { + line = file_lines.next(); + }; + let line_clean = match line { Some("") => return Err(FileError::EmptyLine), Some(a) => a, None => panic!("Iterator returns None, something wrong happened") @@ -45,15 +48,32 @@ impl<'a> LinksFile<'a> { .collect::>() .join("\n"); write(self.0, rest_of_file)?; - Ok(first_line_clean.to_owned()) + Ok(line_clean.to_owned()) } + // pub fn remove_first_line(&self) -> Result { + // let file_contents = read_to_string(self.0)?; + // let mut file_lines = file_contents.split("\n"); + // let first_line = file_lines.next(); + // let first_line_clean = match first_line { + // Some("") => return Err(FileError::EmptyLine), + // Some(a) => a, + // None => panic!("Iterator returns None, something wrong happened") + // }; + // let rest_of_file = file_lines + // .collect::>() + // .join("\n"); + // write(self.0, rest_of_file)?; + // Ok(first_line_clean.to_owned()) + // } + pub fn add_line_to_end(&self, line: String) -> Result<(), FileError> { let old_contents = read_to_string(self.0)?; let new_contents = old_contents .trim() .to_owned() - + "\n" + &line; + + (if old_contents.is_empty() {""} else {"\n"}) + + &line; write(self.0, new_contents)?; Ok(()) } diff --git a/src/main.rs b/src/main.rs index a0150c1..42727e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use std::path::Path; use tokio::{spawn}; +use tokio_cron_scheduler::{Job, JobScheduler}; mod file; mod wikipedia; @@ -23,7 +24,7 @@ async fn post_link_from_file(queue: LinksFile<'_>, backup: LinksFile<'_>, mastod // 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(); + mastodon.post_text_status(post_status, Visibility::Public).await.unwrap(); } #[tokio::main] @@ -33,22 +34,33 @@ async fn main() { 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 sched = JobScheduler::new().await.unwrap(); - let poster = spawn(async { - let queue = LinksFile::new(Path::new("queue.txt")); - let backup = LinksFile::new(Path::new("backup.txt")); - let mastodon = Mastodon::new(app_instance, app_token); + sched.add( + Job::new_async("0 0 8 * * *", move |_uuid, _l| { + Box::pin({ + let app_instance_cloned = app_instance.clone(); + let app_token_cloned = app_token.clone(); + async move { + let queue = LinksFile::new(Path::new("queue.txt")); + let backup = LinksFile::new(Path::new("backup.txt")); + let mastodon = Mastodon::new(app_instance_cloned, app_token_cloned); - 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; - } - }); + 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; + }; + } + }) + }).unwrap() + ).await.unwrap(); - let server = spawn(server::serve()); + let scheduler = sched.start(); + let server = server::serve(); - let _ = poster.await; + let _ = scheduler.await; + let _ = server.await; } diff --git a/src/server.rs b/src/server.rs index 3f0acda..27d93c3 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,8 +1,7 @@ use std::path::Path; use axum::{ - routing::{get, post}, - extract::State, + routing::post, http::StatusCode, Json, Router };