Full project done.

This commit is contained in:
vorboyvo 2024-12-30 14:26:53 -05:00
parent 82e67a3c79
commit 71da65576b
6 changed files with 111 additions and 21 deletions

57
Cargo.lock generated
View file

@ -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"

View file

@ -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"

View file

@ -1,2 +1,3 @@
https://en.wikipedia.org/wiki/GNU/Linux_naming_controversy
https://en.wikipedia.org/wiki/Buck-a-beer?wprov=sfla1

View file

@ -35,8 +35,11 @@ impl<'a> LinksFile<'a> {
pub fn remove_first_line(&self) -> Result<String, FileError> {
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::<Vec<&str>>()
.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<String, FileError> {
// 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::<Vec<&str>>()
// .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(())
}

View file

@ -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,11 +34,17 @@ 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 {
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, app_token);
let mastodon = Mastodon::new(app_instance_cloned, app_token_cloned);
let mut interval_timer = tokio::time::interval(
chrono::Duration::minutes(10).to_std().unwrap()
@ -45,10 +52,15 @@ async fn main() {
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;
}

View file

@ -1,8 +1,7 @@
use std::path::Path;
use axum::{
routing::{get, post},
extract::State,
routing::post,
http::StatusCode,
Json, Router
};