Persist links to a database

This commit is contained in:
Marc Plano-Lesay 2025-04-30 21:45:08 +10:00
parent 17fb0c1856
commit b157985bf3
Signed by: kernald
GPG key ID: 66A41B08CC62A6CF
10 changed files with 451 additions and 44 deletions

123
src/db.rs Normal file
View file

@ -0,0 +1,123 @@
use crate::models::{Magnet, NewMagnet};
use crate::schema::magnets;
use crate::PostInfo;
use color_eyre::eyre::{eyre, Result, WrapErr};
use diesel::prelude::*;
use diesel::sqlite::SqliteConnection;
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
use std::fs::create_dir_all;
use std::path::Path;
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations");
/// Database for storing magnet links and associated information
pub struct Database {
conn: SqliteConnection,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::PostInfo;
use chrono::Utc;
use tempfile::tempdir;
#[test]
fn test_database_initialization() {
let temp_dir = tempdir().unwrap();
let db_path = temp_dir.path().join("test.db");
let db = Database::new(&db_path);
assert!(db.is_ok());
assert!(db_path.exists());
}
#[test]
fn test_store_and_retrieve_magnet_links() {
let temp_dir = tempdir().unwrap();
let db_path = temp_dir.path().join("test.db");
let mut db = Database::new(&db_path).unwrap();
let post_info = PostInfo {
title: "Test Title".to_string(),
submitter: "test_user".to_string(),
subreddit: "test_subreddit".to_string(),
magnet_links: vec![
"magnet:?xt=urn:btih:test1".to_string(),
"magnet:?xt=urn:btih:test2".to_string(),
],
timestamp: Utc::now(),
};
let expected_timestamp = post_info.timestamp.naive_utc();
let result = db.store_magnets(&post_info);
assert!(result.is_ok());
let magnets = db.get_all_magnets().unwrap();
assert_eq!(magnets.len(), 2);
for magnet in magnets {
assert!(
magnet.link == "magnet:?xt=urn:btih:test1"
|| magnet.link == "magnet:?xt=urn:btih:test2"
);
assert_eq!(magnet.title, "Test Title");
assert_eq!(magnet.submitter, "test_user");
assert_eq!(magnet.subreddit, "test_subreddit");
assert_eq!(magnet.published_at, expected_timestamp);
}
}
}
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();
let links = post
.magnet_links
.iter()
.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")
}
}

View file

@ -8,18 +8,23 @@ use figment::{
Figment,
};
use figment_file_provider_adapter::FileAdapter;
use log::debug;
use log::{debug, warn};
use multimap::MultiMap;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::fs::create_dir_all;
use std::path::{Path, PathBuf};
use crate::db::Database;
use crate::magnet::{extract_magnet_links, Magnet};
use reddit_client::RedditClient;
mod db;
mod magnet;
mod models;
mod reddit_client;
mod schema;
#[derive(Debug, Serialize, Deserialize)]
struct SectionConfig {
@ -36,6 +41,7 @@ struct Config {
#[derive(Debug)]
struct PostInfo {
title: String,
submitter: String,
magnet_links: Vec<Magnet>,
subreddit: String,
timestamp: DateTime<Utc>,
@ -47,6 +53,10 @@ struct Args {
/// Path to the configuration file
#[arg(short, long)]
config: Option<String>,
/// Path to the database file
#[arg(short, long)]
db: Option<String>,
}
/// Filters posts based on a title filter pattern
@ -100,6 +110,23 @@ async fn main() -> Result<()> {
let args = Args::parse();
// Initialize database
let db_path = match args.db {
Some(path) => PathBuf::from(path),
None => ProjectDirs::from("fr", "enoent", "reddit-magnet")
.map(|p| p.data_dir().join("reddit-magnet.db"))
.ok_or_else(|| eyre!("Could not determine data directory"))?,
};
// Create parent directory if it doesn't exist
if let Some(parent) = db_path.parent() {
create_dir_all(parent)
.wrap_err_with(|| format!("Failed to create directory: {:?}", parent))?;
}
let mut db = Database::new(&db_path)
.wrap_err_with(|| format!("Failed to initialize database at {:?}", db_path))?;
let mut conf_extractor = Figment::new();
let config_file_path: Option<PathBuf> = match args.config {
Some(path) => Some(Path::new(&path).to_path_buf()),
@ -166,12 +193,20 @@ async fn main() -> Result<()> {
let magnet_links = extract_magnet_links(body);
if !magnet_links.is_empty() {
filtered_posts.push(PostInfo {
let post_info = PostInfo {
title: title.to_string(),
submitter: username.clone(),
subreddit: subreddit.to_string(),
magnet_links,
timestamp: post.created,
});
};
// Store the post info in the database
if let Err(e) = db.store_magnets(&post_info) {
warn!("Failed to store post info in database: {}", e);
}
filtered_posts.push(post_info);
}
}

26
src/models.rs Normal file
View file

@ -0,0 +1,26 @@
use crate::schema::magnets;
use chrono::NaiveDateTime;
use diesel::prelude::*;
#[derive(Queryable, Selectable)]
#[diesel(table_name = magnets)]
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
pub struct Magnet {
pub id: Option<i32>,
pub title: String,
pub submitter: String,
pub subreddit: String,
pub link: String,
pub published_at: NaiveDateTime,
}
#[derive(Insertable)]
#[diesel(table_name = magnets)]
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
pub struct NewMagnet<'a> {
pub title: &'a str,
pub submitter: &'a str,
pub subreddit: &'a str,
pub link: &'a str,
pub published_at: &'a NaiveDateTime,
}

12
src/schema.rs Normal file
View file

@ -0,0 +1,12 @@
// @generated automatically by Diesel CLI.
diesel::table! {
magnets (id) {
id -> Nullable<Integer>,
title -> Text,
submitter -> Text,
subreddit -> Text,
link -> Text,
published_at -> Timestamp,
}
}