From 3e4e3820ff14b1f9910ff573a61660a6650a35e4 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Mon, 11 Mar 2024 11:15:14 +0100 Subject: [PATCH] add tracking for new downloads --- src/db.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++--- src/yt_dlp/mod.rs | 1 + 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/db.rs b/src/db.rs index 4040d71..cb1cce2 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,5 +1,5 @@ use jobdispatcher::{JobDispatcher, JobOrder}; -use rusqlite::Connection; +use rusqlite::{Connection, OptionalExtension}; use std::sync::{mpsc::Receiver, Arc}; pub struct DatabaseBackend { @@ -24,6 +24,18 @@ impl DatabaseBackend { ) .unwrap(); + conn.execute( + "CREATE TABLE IF NOT EXISTS item_log ( + id INTEGER PRIMARY KEY, + module TEXT NOT NULL, + name TEXT NOT NULL, + url TEXT NOT NULL, + timestamp TEXT NOT NULL + )", + [], + ) + .unwrap(); + let dispatcher = Arc::new(dispatcher); Self { file: file.to_string(), @@ -51,13 +63,39 @@ impl DatabaseBackend { job.done(Out::Ok); } Query::CheckForUrl(ref url) => { - let conn = Connection::open(&self.file).unwrap(); - let mut stmt = conn + let mut stmt = self + .conn .prepare("SELECT COUNT(*) FROM urls WHERE url = ?") .unwrap(); let count: i64 = stmt.query_row([url], |row| row.get(0)).unwrap(); job.done(Out::Bool(count > 0)); } + Query::UpdateNewDownloads(ref module, ref name, ref url) => { + let timestamp = chrono::Local::now().to_rfc3339(); + + // Check if the entry exists + let existing_timestamp: Option = self.conn.query_row( + "SELECT timestamp FROM item_log WHERE module = ? AND name = ? AND url = ?", + [module, name, url], + |row| row.get(0) + ).optional().unwrap(); + + if existing_timestamp.is_some() { + // Entry exists, update timestamp + self.conn.execute( + "UPDATE item_log SET timestamp = ? WHERE module = ? AND name = ? AND url = ?", + [×tamp, module, name, url] + ).unwrap(); + } else { + // Entry doesn't exist, insert new row + self.conn.execute( + "INSERT INTO item_log (module, name, url, timestamp) VALUES (?, ?, ?, ?)", + [module, name, url, ×tamp] + ).unwrap(); + } + + job.done(Out::Ok); + } } } } @@ -66,6 +104,7 @@ impl DatabaseBackend { pub enum Query { InsertUrl(String), CheckForUrl(String), + UpdateNewDownloads(String, String, String), } pub enum Out { @@ -94,4 +133,12 @@ impl Database { Out::Bool(b) => b, } } + + pub fn update_new_downloads(&self, module: &str, name: &str, url: &str) { + self.conn.send(Query::UpdateNewDownloads( + module.to_string(), + name.to_string(), + url.to_string(), + )); + } } diff --git a/src/yt_dlp/mod.rs b/src/yt_dlp/mod.rs index eaf68e4..8d2e687 100644 --- a/src/yt_dlp/mod.rs +++ b/src/yt_dlp/mod.rs @@ -99,6 +99,7 @@ impl Module for YtDlpModule { Ok(()) => { // mark as downloaded self.db.insert_url(&video_url); + self.db.update_new_downloads(&self.name(), item, item_url); log::info!("Downloaded \"{video_title}\""); } Err(e) => {