Persist links to a database
This commit is contained in:
parent
17fb0c1856
commit
b157985bf3
10 changed files with 451 additions and 44 deletions
41
src/main.rs
41
src/main.rs
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue