Add a Bitmagnet import action

This commit is contained in:
Marc Plano-Lesay 2025-05-01 19:32:42 +10:00
parent 8eda807ead
commit d26931f4c6
Signed by: kernald
GPG key ID: 66A41B08CC62A6CF
15 changed files with 402 additions and 10 deletions

View file

@ -0,0 +1,70 @@
use crate::actions::action::{Action, ProcessedMagnets};
use crate::actions::bitmagnet::client::BitmagnetClient;
use crate::actions::bitmagnet::config::BitmagnetConfig;
use crate::db::{BitmagnetProcessedTable, Database};
use color_eyre::eyre::Result;
use log::{debug, warn};
/// Action for submitting magnet links to Bitmagnet
pub struct BitmagnetAction {
client: BitmagnetClient,
}
impl BitmagnetAction {
pub async fn new(config: &BitmagnetConfig) -> Result<Self> {
let client = BitmagnetClient::new(config)?;
Ok(BitmagnetAction { client })
}
}
#[async_trait::async_trait]
impl Action for BitmagnetAction {
/// Return the name of the action
fn name(&self) -> &str {
"Bitmagnet"
}
/// 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::<BitmagnetProcessedTable>()?;
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,
&magnet.published_at.and_utc(),
&magnet.imdb_id,
)
.await
{
Ok(_) => {
debug!(
"Successfully submitted magnet link to Bitmagnet: {}",
magnet.title
);
debug!("Magnet link: {}", magnet.link);
db.mark_magnet_processed_for_table::<BitmagnetProcessedTable>(id)?;
processed_magnets.push(magnet);
}
Err(e) => {
warn!("Failed to submit magnet link to Bitmagnet: {}", e);
failed_magnets.push(magnet);
}
}
} else {
warn!("Skipping magnet with null ID: {}", magnet.link);
}
}
Ok(ProcessedMagnets {
success: processed_magnets,
failed: failed_magnets,
})
}
}