Clean up output

This commit is contained in:
Marc Plano-Lesay 2025-05-01 16:08:49 +10:00
parent 774a5ed4ac
commit 1a11a18d05
Signed by: kernald
GPG key ID: 66A41B08CC62A6CF
7 changed files with 224 additions and 77 deletions

View file

@ -1,3 +1,4 @@
use crate::actions::action::Action;
use crate::actions::transmission::TransmissionAction;
use crate::args::Args;
use crate::config::{get_db_path, load_config};
@ -10,7 +11,7 @@ use log::{debug, info, warn};
use multimap::MultiMap;
use reddit_client::RedditClient;
use regex::Regex;
use std::collections::HashSet;
use std::collections::{HashMap, HashSet};
use std::fs::create_dir_all;
mod actions;
@ -20,6 +21,7 @@ mod db;
mod magnet;
mod models;
mod reddit_client;
mod report;
mod schema;
#[derive(Debug)]
@ -39,43 +41,6 @@ fn filter_posts(title: &str, title_filter: Option<Regex>) -> bool {
}
}
/// Prints the posts with their magnet links
fn print_posts(posts: &[PostInfo], username: &str, title_filter: Option<Regex>) -> usize {
println!("Magnet links from u/{}:", username);
if let Some(pattern) = title_filter {
println!("Filtering titles by pattern: {}", pattern);
}
println!("----------------------------");
let mut post_count = 0;
for post in posts {
// Only display posts with magnet links
if !post.magnet_links.is_empty() {
post_count += 1;
println!(
"{}. [r/{}] {} (Posted: {})",
post_count,
post.subreddit,
post.title,
post.timestamp.format("%Y-%m-%d %H:%M:%S")
);
for (i, link) in post.magnet_links.iter().enumerate() {
println!(" Link {}: {}", i + 1, link);
}
println!();
}
}
if post_count == 0 {
println!("No posts with magnet links found.");
}
post_count
}
#[tokio::main]
async fn main() -> Result<()> {
color_eyre::install()?;
@ -117,8 +82,9 @@ async fn main() -> Result<()> {
}
// Process sources and store magnet links
let mut total_new_links = 0;
for (source_name, source_config) in conf.sources {
println!("\nProcessing source [{}]", source_name);
info!("Processing source [{}]", source_name);
let username = source_config.username.clone();
let title_filter = match source_config.title_filter {
@ -130,8 +96,6 @@ async fn main() -> Result<()> {
};
if let Some(submissions) = user_posts.get_vec(&username) {
let mut filtered_posts = Vec::new();
for post in submissions
.iter()
.filter(|s| filter_posts(&*s.title, title_filter.clone()))
@ -151,35 +115,29 @@ async fn main() -> Result<()> {
};
// 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);
match db.store_magnets(&post_info) {
Ok(count) => {
total_new_links += count;
}
Err(e) => {
warn!("Failed to store post info in database: {}", e);
}
}
filtered_posts.push(post_info);
}
}
print_posts(&filtered_posts, &username, title_filter);
}
}
// Process magnet links with Transmission if enabled
// Initialize actions
let mut actions: Vec<Box<dyn Action>> = Vec::new();
// Add Transmission action if enabled
if let Some(transmission_config) = conf.transmission {
if transmission_config.enable {
info!("Processing magnet links with Transmission");
match TransmissionAction::new(&transmission_config, db).await {
Ok(mut transmission_action) => {
match transmission_action.process_unprocessed_magnets().await {
Ok(count) => {
info!(
"Successfully processed {} magnet links with Transmission",
count
);
}
Err(e) => {
warn!("Failed to process magnet links with Transmission: {}", e);
}
}
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);
@ -192,5 +150,32 @@ async fn main() -> Result<()> {
debug!("No Transmission 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);
}
}
}
// Generate report
if let Err(e) = report::generate_report(&action_results, total_new_links) {
warn!("Failed to generate report: {}", e);
}
Ok(())
}