Clean things up a bit
This commit is contained in:
parent
a91e243ecc
commit
3275f4890d
18 changed files with 572 additions and 249 deletions
220
src/main.rs
220
src/main.rs
|
|
@ -1,23 +1,14 @@
|
|||
use crate::actions::action::Action;
|
||||
use crate::actions::bitmagnet::BitmagnetAction;
|
||||
use crate::actions::transmission::TransmissionAction;
|
||||
use crate::app::App;
|
||||
use crate::args::Args;
|
||||
use crate::config::{get_db_path, load_config};
|
||||
use crate::db::Database;
|
||||
use crate::magnet::{extract_magnet_links, Magnet};
|
||||
use crate::notifications::notification::Notification;
|
||||
use crate::notifications::ntfy::NtfyNotification;
|
||||
use chrono::{DateTime, Utc};
|
||||
use clap::Parser;
|
||||
use color_eyre::eyre::{eyre, Result, WrapErr};
|
||||
use log::{debug, info, warn};
|
||||
use multimap::MultiMap;
|
||||
use reddit_client::RedditClient;
|
||||
use regex::Regex;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use color_eyre::eyre::{Result, WrapErr};
|
||||
use std::fs::create_dir_all;
|
||||
|
||||
mod actions;
|
||||
mod app;
|
||||
mod args;
|
||||
mod config;
|
||||
mod db;
|
||||
|
|
@ -28,30 +19,26 @@ mod reddit_client;
|
|||
mod report;
|
||||
mod schema;
|
||||
|
||||
/// Post information with magnet links
|
||||
#[derive(Debug)]
|
||||
struct PostInfo {
|
||||
title: String,
|
||||
submitter: String,
|
||||
magnet_links: Vec<Magnet>,
|
||||
subreddit: String,
|
||||
timestamp: DateTime<Utc>,
|
||||
imdb_id: Option<String>,
|
||||
}
|
||||
|
||||
/// Filters posts based on a title filter pattern
|
||||
fn filter_posts(title: &str, title_filter: Option<Regex>) -> bool {
|
||||
match title_filter {
|
||||
Some(pattern) => pattern.is_match(title),
|
||||
None => true,
|
||||
}
|
||||
pub struct PostInfo {
|
||||
pub title: String,
|
||||
pub submitter: String,
|
||||
pub magnet_links: Vec<magnet::Magnet>,
|
||||
pub subreddit: String,
|
||||
pub timestamp: DateTime<Utc>,
|
||||
pub imdb_id: Option<String>,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
// Initialize error handling
|
||||
color_eyre::install()?;
|
||||
|
||||
// Parse command-line arguments
|
||||
let args = Args::parse();
|
||||
|
||||
// Initialize logging
|
||||
pretty_env_logger::formatted_timed_builder()
|
||||
.filter_level(args.verbose.log_level_filter())
|
||||
.init();
|
||||
|
|
@ -65,185 +52,14 @@ async fn main() -> Result<()> {
|
|||
.wrap_err_with(|| format!("Failed to create directory: {:?}", parent))?;
|
||||
}
|
||||
|
||||
let mut db = Database::new(&db_path)
|
||||
let db = Database::new(&db_path)
|
||||
.wrap_err_with(|| format!("Failed to initialize database at {:?}", db_path))?;
|
||||
|
||||
// Load configuration
|
||||
let conf = load_config(&args)?;
|
||||
|
||||
if conf.sources.is_empty() {
|
||||
return Err(eyre!("No sources found in configuration. Please add at least one source to your configuration file.").into());
|
||||
}
|
||||
|
||||
let mut unique_usernames = HashSet::new();
|
||||
for (_, source_config) in &conf.sources {
|
||||
unique_usernames.insert(source_config.username.clone());
|
||||
}
|
||||
|
||||
let reddit_client = RedditClient::new();
|
||||
let mut user_posts = MultiMap::new();
|
||||
for username in unique_usernames {
|
||||
let submissions = reddit_client
|
||||
.fetch_user_submissions(&username, args.post_count)
|
||||
.await?;
|
||||
user_posts.insert_many(username, submissions);
|
||||
}
|
||||
|
||||
// Process sources and store magnet links
|
||||
let mut total_new_links = 0;
|
||||
for (source_name, source_config) in conf.sources {
|
||||
info!("Processing source [{}]", source_name);
|
||||
|
||||
let username = source_config.username.clone();
|
||||
let title_filter = match source_config.title_filter {
|
||||
Some(filter) => Some(
|
||||
Regex::new(filter.as_str())
|
||||
.context(format!("Invalid regex pattern: {}", filter))?,
|
||||
),
|
||||
None => None,
|
||||
};
|
||||
|
||||
if let Some(submissions) = user_posts.get_vec(&username) {
|
||||
for post in submissions
|
||||
.iter()
|
||||
.filter(|s| filter_posts(&*s.title, title_filter.clone()))
|
||||
{
|
||||
let title = &post.title;
|
||||
let body = &post.body;
|
||||
let subreddit = &post.subreddit;
|
||||
|
||||
let magnet_links = extract_magnet_links(body);
|
||||
if !magnet_links.is_empty() {
|
||||
let post_info = PostInfo {
|
||||
title: title.to_string(),
|
||||
submitter: username.clone(),
|
||||
subreddit: subreddit.to_string(),
|
||||
magnet_links,
|
||||
timestamp: post.created,
|
||||
imdb_id: source_config.imdb_id.clone(),
|
||||
};
|
||||
|
||||
// Store the post info in the database
|
||||
match db.store_magnets(&post_info) {
|
||||
Ok(count) => {
|
||||
total_new_links += count;
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("Failed to store post info in database: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize actions
|
||||
let mut actions: Vec<Box<dyn Action>> = Vec::new();
|
||||
|
||||
// Add Bitmagnet action if enabled
|
||||
if let Some(bitmagnet_config) = conf.bitmagnet {
|
||||
if bitmagnet_config.enable {
|
||||
info!("Initializing Bitmagnet action");
|
||||
match BitmagnetAction::new(&bitmagnet_config).await {
|
||||
Ok(bitmagnet_action) => {
|
||||
actions.push(Box::new(bitmagnet_action));
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("Failed to initialize Bitmagnet action: {}", e);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
debug!("Bitmagnet action is disabled");
|
||||
}
|
||||
} else {
|
||||
debug!("No Bitmagnet configuration found");
|
||||
}
|
||||
|
||||
// Add Transmission action if enabled
|
||||
if let Some(transmission_config) = conf.transmission {
|
||||
if transmission_config.enable {
|
||||
info!("Initializing Transmission action");
|
||||
match TransmissionAction::new(&transmission_config).await {
|
||||
Ok(transmission_action) => {
|
||||
actions.push(Box::new(transmission_action));
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("Failed to initialize Transmission action: {}", e);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
debug!("Transmission action is disabled");
|
||||
}
|
||||
} else {
|
||||
debug!("No Transmission configuration found");
|
||||
}
|
||||
|
||||
// Initialize notifications
|
||||
let mut notifications: Vec<Box<dyn Notification>> = Vec::new();
|
||||
|
||||
// Add Ntfy notification if enabled
|
||||
if let Some(ntfy_config) = conf.ntfy {
|
||||
if ntfy_config.enable {
|
||||
info!("Initializing Ntfy notification");
|
||||
match NtfyNotification::new(ntfy_config) {
|
||||
Ok(ntfy_notification) => {
|
||||
notifications.push(Box::new(ntfy_notification));
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("Failed to initialize Ntfy notification: {}", e);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
debug!("Ntfy notification is disabled");
|
||||
}
|
||||
} else {
|
||||
debug!("No Ntfy configuration found");
|
||||
}
|
||||
|
||||
// Process all actions and collect results
|
||||
let mut action_results = HashMap::new();
|
||||
|
||||
for action in &mut actions {
|
||||
let action_name = action.name().to_string();
|
||||
debug!("Processing magnet links with {} action", action_name);
|
||||
|
||||
match action.process_unprocessed_magnets(&mut db).await {
|
||||
Ok(processed_magnets) => {
|
||||
let count = processed_magnets.success.len();
|
||||
debug!(
|
||||
"Successfully processed {} magnet links with {}",
|
||||
count, action_name
|
||||
);
|
||||
action_results.insert(action_name, processed_magnets);
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("Failed to process magnet links with {}: {}", action_name, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Send notifications
|
||||
for notification in ¬ifications {
|
||||
match notification
|
||||
.send_notification(&action_results, total_new_links)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {
|
||||
debug!("Successfully sent notification to {}", notification.name());
|
||||
}
|
||||
Err(e) => {
|
||||
warn!(
|
||||
"Failed to send notification to {}: {}",
|
||||
notification.name(),
|
||||
e
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Generate and display report
|
||||
for line in report::generate_report(&action_results, total_new_links, true).lines() {
|
||||
info!("{}", line);
|
||||
}
|
||||
// Create and run the application
|
||||
App::new(db, conf).run(args.post_count).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue