add tracking for new downloads

This commit is contained in:
JMARyA 2024-03-11 11:15:14 +01:00
parent 3d85455906
commit 3e4e3820ff
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
2 changed files with 51 additions and 3 deletions

View file

@ -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<String> = 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 = ?",
[&timestamp, 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, &timestamp]
).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(),
));
}
}

View file

@ -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) => {