63 lines
2.3 KiB
Rust
63 lines
2.3 KiB
Rust
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, warn};
|
|
|
|
/// Action for submitting magnet links to Transmission
|
|
pub struct TransmissionAction {
|
|
client: TransmissionClient,
|
|
}
|
|
|
|
impl TransmissionAction {
|
|
pub async fn new(config: &TransmissionConfig) -> Result<Self> {
|
|
let client = TransmissionClient::new(config)?;
|
|
|
|
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 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(_) => {
|
|
debug!(
|
|
"Successfully submitted magnet link to Transmission: {}",
|
|
magnet.title
|
|
);
|
|
debug!("Magnet link: {}", magnet.link);
|
|
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(ProcessedMagnets {
|
|
success: processed_magnets,
|
|
failed: failed_magnets,
|
|
})
|
|
}
|
|
}
|