Add a send to Transmission action
This commit is contained in:
parent
88419cbf97
commit
3f2b002f52
10 changed files with 952 additions and 83 deletions
182
src/db.rs
182
src/db.rs
|
|
@ -1,5 +1,5 @@
|
|||
use crate::models::{Magnet, NewMagnet};
|
||||
use crate::schema::magnets;
|
||||
use crate::models::{Magnet, NewMagnet, NewTransmissionProcessed, TransmissionProcessed};
|
||||
use crate::schema::{magnets, transmission_processed};
|
||||
use crate::PostInfo;
|
||||
use color_eyre::eyre::{eyre, Result, WrapErr};
|
||||
use diesel::prelude::*;
|
||||
|
|
@ -8,6 +8,40 @@ use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
|
|||
use std::fs::create_dir_all;
|
||||
use std::path::Path;
|
||||
|
||||
pub trait ProcessedTable {
|
||||
/// Get all processed magnet IDs from this table
|
||||
fn get_processed_ids(conn: &mut SqliteConnection) -> Result<Vec<i32>>;
|
||||
|
||||
/// Mark a magnet as processed in this table
|
||||
fn mark_processed(conn: &mut SqliteConnection, magnet_id: i32) -> Result<()>;
|
||||
}
|
||||
|
||||
pub struct TransmissionProcessedTable;
|
||||
|
||||
impl ProcessedTable for TransmissionProcessedTable {
|
||||
fn get_processed_ids(conn: &mut SqliteConnection) -> Result<Vec<i32>> {
|
||||
transmission_processed::table
|
||||
.select(transmission_processed::magnet_id)
|
||||
.load(conn)
|
||||
.wrap_err("Failed to load processed magnet IDs for Transmission")
|
||||
}
|
||||
|
||||
fn mark_processed(conn: &mut SqliteConnection, magnet_id: i32) -> Result<()> {
|
||||
let now = chrono::Utc::now().naive_utc();
|
||||
let new_processed = NewTransmissionProcessed {
|
||||
magnet_id,
|
||||
processed_at: &now,
|
||||
};
|
||||
|
||||
diesel::insert_into(transmission_processed::table)
|
||||
.values(&new_processed)
|
||||
.execute(conn)
|
||||
.wrap_err("Failed to mark magnet as processed by Transmission")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations");
|
||||
|
||||
/// Database for storing magnet links and associated information
|
||||
|
|
@ -15,6 +49,90 @@ pub struct Database {
|
|||
conn: SqliteConnection,
|
||||
}
|
||||
|
||||
impl Database {
|
||||
pub fn new<P: AsRef<Path>>(path: P) -> Result<Self> {
|
||||
let database_url = path
|
||||
.as_ref()
|
||||
.to_str()
|
||||
.ok_or_else(|| eyre!("Database path is not valid UTF-8"))?;
|
||||
|
||||
if let Some(parent) = path.as_ref().parent() {
|
||||
create_dir_all(parent)
|
||||
.wrap_err_with(|| format!("Failed to create directory: {:?}", parent))?;
|
||||
}
|
||||
|
||||
let mut conn = SqliteConnection::establish(database_url)
|
||||
.wrap_err("Failed to open database connection")?;
|
||||
|
||||
conn.run_pending_migrations(MIGRATIONS)
|
||||
.expect("Failed to apply database migrations");
|
||||
|
||||
Ok(Database { conn })
|
||||
}
|
||||
|
||||
pub fn get_all_magnets(&mut self) -> Result<Vec<Magnet>> {
|
||||
let results = magnets::table
|
||||
.select(Magnet::as_select())
|
||||
.load(&mut self.conn)
|
||||
.wrap_err("Failed to load magnets from database")?;
|
||||
|
||||
Ok(results)
|
||||
}
|
||||
|
||||
pub fn store_magnets(&mut self, post: &PostInfo) -> Result<usize> {
|
||||
let published_at = post.timestamp.naive_utc();
|
||||
|
||||
// Filter out magnet links that already exist in the database
|
||||
let existing_links: Vec<String> = magnets::table
|
||||
.select(magnets::link)
|
||||
.filter(magnets::link.eq_any(&post.magnet_links))
|
||||
.load(&mut self.conn)
|
||||
.wrap_err("Failed to query existing magnets")?;
|
||||
|
||||
let links = post
|
||||
.magnet_links
|
||||
.iter()
|
||||
.filter(|link| !existing_links.contains(link))
|
||||
.map(|m| NewMagnet {
|
||||
title: post.title.as_str(),
|
||||
submitter: post.submitter.as_str(),
|
||||
subreddit: post.subreddit.as_str(),
|
||||
link: m,
|
||||
published_at: &published_at,
|
||||
})
|
||||
.collect::<Vec<NewMagnet>>();
|
||||
|
||||
diesel::insert_into(magnets::table)
|
||||
.values(&links)
|
||||
.execute(&mut self.conn)
|
||||
.wrap_err("Failed to save new magnet")
|
||||
}
|
||||
|
||||
/// Get all magnets that have not been processed by a specific table
|
||||
pub fn get_unprocessed_magnets_for_table<T: ProcessedTable>(&mut self) -> Result<Vec<Magnet>> {
|
||||
// Get all magnet IDs that have been processed by the specified table
|
||||
let processed_ids = T::get_processed_ids(&mut self.conn)?;
|
||||
|
||||
// Get all magnets that are not in the processed list
|
||||
let results = magnets::table
|
||||
.select(Magnet::as_select())
|
||||
.filter(magnets::id.is_not_null())
|
||||
.filter(magnets::id.ne_all(processed_ids))
|
||||
.load(&mut self.conn)
|
||||
.wrap_err("Failed to load unprocessed magnets from database")?;
|
||||
|
||||
Ok(results)
|
||||
}
|
||||
|
||||
/// Mark a magnet as processed by a specific table
|
||||
pub fn mark_magnet_processed_for_table<T: ProcessedTable>(
|
||||
&mut self,
|
||||
magnet_id: i32,
|
||||
) -> Result<()> {
|
||||
T::mark_processed(&mut self.conn, magnet_id)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
@ -137,63 +255,3 @@ mod tests {
|
|||
assert_eq!(magnets.len(), 3);
|
||||
}
|
||||
}
|
||||
|
||||
impl Database {
|
||||
pub fn new<P: AsRef<Path>>(path: P) -> Result<Self> {
|
||||
let database_url = path
|
||||
.as_ref()
|
||||
.to_str()
|
||||
.ok_or_else(|| eyre!("Database path is not valid UTF-8"))?;
|
||||
|
||||
if let Some(parent) = path.as_ref().parent() {
|
||||
create_dir_all(parent)
|
||||
.wrap_err_with(|| format!("Failed to create directory: {:?}", parent))?;
|
||||
}
|
||||
|
||||
let mut conn = SqliteConnection::establish(database_url)
|
||||
.wrap_err("Failed to open database connection")?;
|
||||
|
||||
conn.run_pending_migrations(MIGRATIONS)
|
||||
.expect("Failed to apply database migrations");
|
||||
|
||||
Ok(Database { conn })
|
||||
}
|
||||
|
||||
pub fn get_all_magnets(&mut self) -> Result<Vec<Magnet>> {
|
||||
let results = magnets::table
|
||||
.select(Magnet::as_select())
|
||||
.load(&mut self.conn)
|
||||
.wrap_err("Failed to load magnets from database")?;
|
||||
|
||||
Ok(results)
|
||||
}
|
||||
|
||||
pub fn store_magnets(&mut self, post: &PostInfo) -> Result<usize> {
|
||||
let published_at = post.timestamp.naive_utc();
|
||||
|
||||
// Filter out magnet links that already exist in the database
|
||||
let existing_links: Vec<String> = magnets::table
|
||||
.select(magnets::link)
|
||||
.filter(magnets::link.eq_any(&post.magnet_links))
|
||||
.load(&mut self.conn)
|
||||
.wrap_err("Failed to query existing magnets")?;
|
||||
|
||||
let links = post
|
||||
.magnet_links
|
||||
.iter()
|
||||
.filter(|link| !existing_links.contains(link))
|
||||
.map(|m| NewMagnet {
|
||||
title: post.title.as_str(),
|
||||
submitter: post.submitter.as_str(),
|
||||
subreddit: post.subreddit.as_str(),
|
||||
link: m,
|
||||
published_at: &published_at,
|
||||
})
|
||||
.collect::<Vec<NewMagnet>>();
|
||||
|
||||
diesel::insert_into(magnets::table)
|
||||
.values(&links)
|
||||
.execute(&mut self.conn)
|
||||
.wrap_err("Failed to save new magnet")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue