70 lines
2.4 KiB
Rust
70 lines
2.4 KiB
Rust
use crate::actions::action::{Action, ProcessedMagnets};
|
|
use crate::actions::transmission::client::TransmissionClient;
|
|
use crate::actions::transmission::config::TransmissionConfig;
|
|
use crate::actions::transmission::db::TransmissionProcessedTable;
|
|
use crate::db::Database;
|
|
use color_eyre::eyre::Result;
|
|
use log::{debug, warn};
|
|
|
|
/// Action for submitting magnet links to Transmission
|
|
pub struct TransmissionAction {
|
|
client: TransmissionClient,
|
|
}
|
|
|
|
impl TransmissionAction {
|
|
pub 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() -> &'static str {
|
|
"Transmission"
|
|
}
|
|
|
|
fn get_name(&self) -> &'static str {
|
|
Self::name()
|
|
}
|
|
|
|
/// 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 {
|
|
let tags = db.get_tags_for_magnet(magnet.id)?;
|
|
let tag_refs: Vec<&str> = tags.iter().map(|s| s.as_str()).collect();
|
|
|
|
match self.client.submit_magnet(&magnet.link, tag_refs).await {
|
|
Ok(_) => {
|
|
debug!(
|
|
"Successfully submitted magnet link to {}: {}",
|
|
Self::name(),
|
|
magnet.title
|
|
);
|
|
debug!("Magnet link: {}", magnet.link);
|
|
if !tags.is_empty() {
|
|
debug!("Tags: {:?}", tags);
|
|
}
|
|
db.mark_magnet_processed_for_table::<TransmissionProcessedTable>(magnet.id)?;
|
|
processed_magnets.push(magnet);
|
|
}
|
|
Err(e) => {
|
|
warn!("Failed to submit magnet link to {}: {}", Self::name(), e);
|
|
failed_magnets.push(magnet);
|
|
}
|
|
}
|
|
}
|
|
|
|
Ok(ProcessedMagnets {
|
|
success: processed_magnets,
|
|
failed: failed_magnets,
|
|
})
|
|
}
|
|
}
|