implement permanent database to skip handle processed entries

This commit is contained in:
yggverse 2026-04-06 22:35:36 +03:00
parent c160c30ee5
commit 2ea58cc9c3
7 changed files with 99 additions and 13 deletions

View file

@ -1,10 +1,12 @@
mod argument;
mod config;
mod database;
use argument::Argument;
use chrono::Local;
use clap::Parser;
use config::Config;
use database::Database;
use log::*;
use rustypipe::client::RustyPipe;
use std::{env::var, process::Command, time::Duration};
@ -29,18 +31,44 @@ async fn main() {
let config: Config =
toml::from_str(&std::fs::read_to_string(&argument.config).unwrap()).unwrap();
let update = config.update.map(Duration::from_secs);
let mut database = Database::new(&config.database).unwrap();
let rp = RustyPipe::new();
loop {
for (c, channel) in &config.channel {
debug!("handle channel `{c}` ({})...", channel.id);
match rp.query().channel_videos(&channel.id).await {
Ok(result) => {
for item in result.content.items.iter().filter(|i| {
i.is_live == channel.is_live
&& i.is_short == channel.is_short
&& i.is_upcoming == channel.is_upcoming
}) {
for item in result.content.items {
if database.is_processed(&item.id).is_ok_and(|r| r) {
debug!(
"item `{}` for channel `{c}` already processed; skip handle.",
item.id
);
continue;
}
if item.is_live != channel.is_live {
debug!(
"skip item `{}` for channel `{c}` by `is_live` filter (item: {:?}/config:{:?})",
item.id, item.is_live, channel.is_live
);
continue;
}
if item.is_short != channel.is_short {
debug!(
"skip item `{}` for channel `{c}` by `is_short` filter (item: {:?}/config:{:?})",
item.id, item.is_short, channel.is_short
);
continue;
}
if item.is_upcoming != channel.is_upcoming {
debug!(
"skip item `{}` for channel `{c}` by `is_upcoming` filter (item: {:?}/config:{:?})",
item.id, item.is_upcoming, channel.is_upcoming
);
continue;
}
for command in &channel.command {
let cmd = command.exec.replace("{ID}", &item.id);
match Command::new("sh").arg("-c").arg(&cmd).output() {
@ -49,10 +77,16 @@ async fn main() {
if command.stdout_contains.as_ref().is_none_or(|s| {
String::from_utf8_lossy(&response.stdout).contains(s)
}) {
debug!(
"command `{cmd}` for channel `{c}` successful: `{:?}`",
response
)
match database.process(&item.id) {
Ok(()) => debug!(
"command `{cmd}` for channel `{c}` successful: `{:?}`",
response
),
Err(e) => error!(
"can't persist processed item `{}` for channel `{c}` by `{cmd}`: `{e}`",
item.id
),
}
} else {
warn!(
"unexpected command `{cmd}` for channel `{c}`: `{:?}`",