Clean up output
This commit is contained in:
parent
774a5ed4ac
commit
1a11a18d05
7 changed files with 224 additions and 77 deletions
22
src/actions/action.rs
Normal file
22
src/actions/action.rs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
use crate::models::Magnet;
|
||||
use async_trait::async_trait;
|
||||
use color_eyre::eyre::Result;
|
||||
|
||||
/// Struct to hold the list of processed and failed magnets
|
||||
pub struct ProcessedMagnets {
|
||||
pub success: Vec<Magnet>,
|
||||
pub failed: Vec<Magnet>,
|
||||
}
|
||||
|
||||
/// Trait for actions that process magnet links
|
||||
#[async_trait]
|
||||
pub trait Action {
|
||||
/// Return the name of the action
|
||||
fn name(&self) -> &str;
|
||||
|
||||
/// Process all unprocessed magnet links and return the list of processed magnets
|
||||
async fn process_unprocessed_magnets(
|
||||
&mut self,
|
||||
db: &mut crate::db::Database,
|
||||
) -> Result<ProcessedMagnets>;
|
||||
}
|
||||
|
|
@ -1 +1,2 @@
|
|||
pub mod action;
|
||||
pub mod transmission;
|
||||
|
|
|
|||
|
|
@ -1,51 +1,63 @@
|
|||
use crate::actions::action::{Action, ProcessedMagnets};
|
||||
use crate::actions::transmission::client::TransmissionClient;
|
||||
use crate::actions::transmission::config::TransmissionConfig;
|
||||
use crate::db::{Database, TransmissionProcessedTable};
|
||||
use color_eyre::eyre::Result;
|
||||
use log::{debug, info, warn};
|
||||
use log::{debug, warn};
|
||||
|
||||
/// Action for submitting magnet links to Transmission
|
||||
pub struct TransmissionAction {
|
||||
client: TransmissionClient,
|
||||
db: Database,
|
||||
}
|
||||
|
||||
impl TransmissionAction {
|
||||
pub async fn new(config: &TransmissionConfig, db: Database) -> Result<Self> {
|
||||
pub async fn new(config: &TransmissionConfig) -> Result<Self> {
|
||||
let client = TransmissionClient::new(config)?;
|
||||
|
||||
Ok(TransmissionAction { client, db })
|
||||
Ok(TransmissionAction { client })
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Action for TransmissionAction {
|
||||
/// Return the name of the action
|
||||
fn name(&self) -> &str {
|
||||
"Transmission"
|
||||
}
|
||||
|
||||
/// Process all unprocessed magnet links
|
||||
pub async fn process_unprocessed_magnets(&mut self) -> Result<usize> {
|
||||
let unprocessed_magnets = self
|
||||
.db
|
||||
.get_unprocessed_magnets_for_table::<TransmissionProcessedTable>()?;
|
||||
let mut processed_count = 0;
|
||||
/// Process all unprocessed magnet links and return the list of processed magnets
|
||||
async fn process_unprocessed_magnets(&mut self, db: &mut Database) -> Result<ProcessedMagnets> {
|
||||
let unprocessed_magnets =
|
||||
db.get_unprocessed_magnets_for_table::<TransmissionProcessedTable>()?;
|
||||
let mut processed_magnets = Vec::new();
|
||||
let mut failed_magnets = Vec::new();
|
||||
|
||||
for magnet in unprocessed_magnets {
|
||||
if let Some(id) = magnet.id {
|
||||
match self.client.submit_magnet(&magnet.link).await {
|
||||
Ok(_) => {
|
||||
info!(
|
||||
debug!(
|
||||
"Successfully submitted magnet link to Transmission: {}",
|
||||
magnet.title
|
||||
);
|
||||
debug!("Magnet link: {}", magnet.link);
|
||||
self.db
|
||||
.mark_magnet_processed_for_table::<TransmissionProcessedTable>(id)?;
|
||||
processed_count += 1;
|
||||
db.mark_magnet_processed_for_table::<TransmissionProcessedTable>(id)?;
|
||||
processed_magnets.push(magnet);
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("Failed to submit magnet link to Transmission: {}", e);
|
||||
failed_magnets.push(magnet);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
warn!("Skipping magnet with null ID: {}", magnet.link);
|
||||
// Consider adding to failed_magnets if we want to report these as well
|
||||
}
|
||||
}
|
||||
|
||||
Ok(processed_count)
|
||||
Ok(ProcessedMagnets {
|
||||
success: processed_magnets,
|
||||
failed: failed_magnets,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue