auto-call downloader every day
Some checks failed
CI/CD Pipeline / deploy (push) Has been cancelled
CI/CD Pipeline / test (push) Has been cancelled

This commit is contained in:
2025-08-12 20:40:57 +02:00
parent 3d252a2604
commit b21863298b
5 changed files with 74 additions and 7 deletions

47
Cargo.lock generated
View File

@@ -194,6 +194,15 @@ version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]]
name = "croner"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c344b0690c1ad1c7176fe18eb173e0c927008fdaaa256e40dfd43ddd149c0843"
dependencies = [
"chrono",
]
[[package]]
name = "displaydoc"
version = "0.2.5"
@@ -727,6 +736,17 @@ dependencies = [
"windows-sys 0.59.0",
]
[[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"
@@ -804,6 +824,7 @@ dependencies = [
"serde_json",
"stream-download",
"tokio",
"tokio-cron-scheduler",
"tokio-stream",
]
@@ -1323,6 +1344,21 @@ dependencies = [
"windows-sys 0.59.0",
]
[[package]]
name = "tokio-cron-scheduler"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c71ce8f810abc9fabebccc30302a952f9e89c6cf246fafaf170fef164063141"
dependencies = [
"chrono",
"croner",
"num-derive",
"num-traits",
"tokio",
"tracing",
"uuid",
]
[[package]]
name = "tokio-macros"
version = "2.5.0"
@@ -1481,6 +1517,17 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
[[package]]
name = "uuid"
version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f33196643e165781c20a5ead5582283a7dacbb87855d867fbc2df3f81eddc1be"
dependencies = [
"getrandom 0.3.3",
"js-sys",
"wasm-bindgen",
]
[[package]]
name = "want"
version = "0.3.1"

View File

@@ -13,3 +13,4 @@ async-stream = "0.3"
serde_json = "1"
stream-download = "0.22"
chrono = "0.4"
tokio-cron-scheduler = "0.14.0"

View File

@@ -17,15 +17,14 @@ async fn get_newest_morning_journal() -> Result<String, Box<dyn std::error::Erro
if let Some(broadcasts) = day["broadcasts"].as_array() {
for broadcast in broadcasts.iter().rev() {
//if broadcast["title"] == "Ö1 Morgenjournal" {
if broadcast["title"] == "Eröffnungskonzert Allegro Vivo" {
if let Some(href) = broadcast["href"].as_str() {
if broadcast["title"] == "Eröffnungskonzert Allegro Vivo"
&& let Some(href) = broadcast["href"].as_str() {
return Ok(href.into());
}
}
}
}
}
}
Err(String::from("No Ö1 Morgenjournal found").into())
}

View File

@@ -2,8 +2,10 @@ mod state;
mod streamer;
use axum::{routing::get, Router};
use chrono::Utc;
use state::AppState;
use std::sync::Arc;
use tokio_cron_scheduler::{Job, JobScheduler};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -11,7 +13,26 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let app = Router::new()
.route("/", get(streamer::stream_handler))
.with_state(state);
.with_state(state.clone());
let scheduler = JobScheduler::new().await.unwrap();
scheduler
.add(
Job::new_async(
"30 0 7 * * Mon,Tue,Wed,Thu,Fri,Sat",
move |_uuid, _locked| {
let state_for_task = state.clone();
Box::pin(async move {
state_for_task.check_update().await;
println!("Task executed at: {}", Utc::now());
})
},
)
.unwrap(),
)
.await
.unwrap();
scheduler.start().await.unwrap();
println!("Streaming server running on http://localhost:3029");

View File

@@ -25,11 +25,10 @@ impl AppState {
pub async fn check_update(self: Arc<Self>) {
let today = Local::now().date_naive();
if let Some(downloaded_on_day) = *self.downloaded_on_day.read().await {
if today == downloaded_on_day {
if let Some(downloaded_on_day) = *self.downloaded_on_day.read().await
&& today == downloaded_on_day {
return;
}
}
self.reset().await;
*self.downloaded_on_day.write().await = Some(today);