71 lines
2.2 KiB
Rust
71 lines
2.2 KiB
Rust
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 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() -> &'static str {
|
|
"Bitmagnet"
|
|
}
|
|
|
|
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::<BitmagnetProcessedTable>()?;
|
|
let mut processed_magnets = Vec::new();
|
|
let mut failed_magnets = Vec::new();
|
|
|
|
for magnet in unprocessed_magnets {
|
|
match self
|
|
.client
|
|
.submit_magnet(
|
|
&magnet.link,
|
|
&magnet.published_at.and_utc(),
|
|
&magnet.imdb_id,
|
|
)
|
|
.await
|
|
{
|
|
Ok(_) => {
|
|
debug!(
|
|
"Successfully submitted magnet link to {}: {}",
|
|
Self::name(),
|
|
magnet.title
|
|
);
|
|
debug!("Magnet link: {}", magnet.link);
|
|
db.mark_magnet_processed_for_table::<BitmagnetProcessedTable>(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,
|
|
})
|
|
}
|
|
}
|