Clean things up a bit
This commit is contained in:
parent
a91e243ecc
commit
3275f4890d
18 changed files with 572 additions and 249 deletions
|
|
@ -12,7 +12,11 @@ pub struct ProcessedMagnets {
|
|||
#[async_trait]
|
||||
pub trait Action {
|
||||
/// Return the name of the action
|
||||
fn name(&self) -> &str;
|
||||
fn name() -> &'static str
|
||||
where
|
||||
Self: Sized;
|
||||
|
||||
fn get_name(&self) -> &'static str;
|
||||
|
||||
/// Process all unprocessed magnet links and return the list of processed magnets
|
||||
async fn process_unprocessed_magnets(
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ pub struct BitmagnetAction {
|
|||
}
|
||||
|
||||
impl BitmagnetAction {
|
||||
pub async fn new(config: &BitmagnetConfig) -> Result<Self> {
|
||||
pub fn new(config: &BitmagnetConfig) -> Result<Self> {
|
||||
let client = BitmagnetClient::new(config)?;
|
||||
|
||||
Ok(BitmagnetAction { client })
|
||||
|
|
@ -21,10 +21,14 @@ impl BitmagnetAction {
|
|||
#[async_trait::async_trait]
|
||||
impl Action for BitmagnetAction {
|
||||
/// Return the name of the action
|
||||
fn name(&self) -> &str {
|
||||
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 =
|
||||
|
|
@ -45,7 +49,8 @@ impl Action for BitmagnetAction {
|
|||
{
|
||||
Ok(_) => {
|
||||
debug!(
|
||||
"Successfully submitted magnet link to Bitmagnet: {}",
|
||||
"Successfully submitted magnet link to {}: {}",
|
||||
Self::name(),
|
||||
magnet.title
|
||||
);
|
||||
debug!("Magnet link: {}", magnet.link);
|
||||
|
|
@ -53,7 +58,7 @@ impl Action for BitmagnetAction {
|
|||
processed_magnets.push(magnet);
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("Failed to submit magnet link to Bitmagnet: {}", e);
|
||||
warn!("Failed to submit magnet link to {}: {}", Self::name(), e);
|
||||
failed_magnets.push(magnet);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
use crate::actions::bitmagnet::config::BitmagnetConfig;
|
||||
use chrono::{DateTime, Utc};
|
||||
use color_eyre::eyre::{eyre, Result, WrapErr};
|
||||
use log::{info, warn};
|
||||
use magnet_url::Magnet;
|
||||
use reqwest::Client;
|
||||
use serde_json::json;
|
||||
|
|
@ -95,35 +94,4 @@ impl BitmagnetClient {
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Extract the info hash from a magnet link
|
||||
fn extract_info_hash(magnet: &str) -> Result<String> {
|
||||
// Magnet links typically have the format: magnet:?xt=urn:btih:<info_hash>&dn=<name>&...
|
||||
let parts: Vec<&str> = magnet.split('&').collect();
|
||||
|
||||
for part in parts {
|
||||
if part.starts_with("magnet:?xt=urn:btih:") {
|
||||
return Ok(part[20..].to_string());
|
||||
}
|
||||
if part.starts_with("xt=urn:btih:") {
|
||||
return Ok(part[12..].to_string());
|
||||
}
|
||||
}
|
||||
|
||||
Err(eyre!("Info hash not found in magnet link"))
|
||||
}
|
||||
|
||||
/// Extract the name from a magnet link
|
||||
fn extract_name(magnet: &str) -> Option<String> {
|
||||
// Magnet links typically have the format: magnet:?xt=urn:btih:<info_hash>&dn=<name>&...
|
||||
let parts: Vec<&str> = magnet.split('&').collect();
|
||||
|
||||
for part in parts {
|
||||
if part.starts_with("dn=") {
|
||||
return Some(part[3..].to_string());
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,15 @@
|
|||
use crate::app::Enableable;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Configuration for the Bitmagnet action
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct BitmagnetConfig {
|
||||
pub enable: bool,
|
||||
pub host: String,
|
||||
}
|
||||
|
||||
impl Enableable for BitmagnetConfig {
|
||||
fn is_enabled(&self) -> bool {
|
||||
self.enable
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,5 +3,4 @@ mod client;
|
|||
mod config;
|
||||
|
||||
pub use action::BitmagnetAction;
|
||||
pub use client::BitmagnetClient;
|
||||
pub use config::BitmagnetConfig;
|
||||
|
|
|
|||
123
src/actions/factory.rs
Normal file
123
src/actions/factory.rs
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
use crate::actions::action::Action;
|
||||
use crate::app::Enableable;
|
||||
use color_eyre::eyre::Result;
|
||||
use log::{debug, info, warn};
|
||||
|
||||
/// Initialize an action from a configuration
|
||||
pub fn init_action<C, A, F>(config_opt: &Option<C>, creator: F) -> Result<Option<Box<dyn Action>>>
|
||||
where
|
||||
C: Enableable + Clone,
|
||||
A: Action + 'static,
|
||||
F: FnOnce(&C) -> Result<A>,
|
||||
{
|
||||
if let Some(config) = config_opt {
|
||||
if config.is_enabled() {
|
||||
info!("Initializing {}", A::name());
|
||||
match creator(config) {
|
||||
Ok(action) => {
|
||||
return Ok(Some(Box::new(action)));
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("Failed to initialize {}: {}", A::name(), e);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
debug!("{} is disabled", A::name());
|
||||
}
|
||||
} else {
|
||||
debug!("No {} configuration found", A::name());
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::actions::action::ProcessedMagnets;
|
||||
use crate::db::Database;
|
||||
use crate::models::Magnet;
|
||||
use async_trait::async_trait;
|
||||
use color_eyre::eyre::{eyre, Result};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
|
||||
#[derive(Clone)]
|
||||
struct FakeConfig {
|
||||
enabled: bool,
|
||||
}
|
||||
|
||||
impl Enableable for FakeConfig {
|
||||
fn is_enabled(&self) -> bool {
|
||||
self.enabled
|
||||
}
|
||||
}
|
||||
|
||||
struct FakeAction {}
|
||||
|
||||
#[async_trait]
|
||||
impl Action for FakeAction {
|
||||
fn name() -> &'static str {
|
||||
"FakeAction"
|
||||
}
|
||||
|
||||
fn get_name(&self) -> &'static str {
|
||||
"FakeAction"
|
||||
}
|
||||
|
||||
async fn process_unprocessed_magnets(
|
||||
&mut self,
|
||||
_db: &mut Database,
|
||||
) -> Result<ProcessedMagnets> {
|
||||
Ok(ProcessedMagnets {
|
||||
success: Vec::<Magnet>::new(),
|
||||
failed: Vec::<Magnet>::new(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_init_action_no_config() {
|
||||
let config: Option<FakeConfig> = None;
|
||||
let result = init_action::<_, FakeAction, _>(&config, |_| {
|
||||
panic!("Creator should not be called when config is None");
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
assert!(result.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_init_action_disabled_config() {
|
||||
let config = Some(FakeConfig { enabled: false });
|
||||
let result = init_action::<_, FakeAction, _>(&config, |_| {
|
||||
panic!("Creator should not be called when config is disabled");
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
assert!(result.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_init_action_creator_fails() {
|
||||
let config = Some(FakeConfig { enabled: true });
|
||||
let result =
|
||||
init_action::<_, FakeAction, _>(&config, |_| Err(eyre!("Creator failed"))).unwrap();
|
||||
|
||||
assert!(result.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_init_action_success() {
|
||||
let config = Some(FakeConfig { enabled: true });
|
||||
let creator_called = AtomicBool::new(false);
|
||||
|
||||
let result = init_action::<_, FakeAction, _>(&config, |_| {
|
||||
creator_called.store(true, Ordering::SeqCst);
|
||||
Ok(FakeAction {})
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
assert!(creator_called.load(Ordering::SeqCst));
|
||||
assert!(result.is_some());
|
||||
}
|
||||
}
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
pub mod action;
|
||||
pub mod bitmagnet;
|
||||
pub mod factory;
|
||||
pub mod transmission;
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ pub struct TransmissionAction {
|
|||
}
|
||||
|
||||
impl TransmissionAction {
|
||||
pub async fn new(config: &TransmissionConfig) -> Result<Self> {
|
||||
pub fn new(config: &TransmissionConfig) -> Result<Self> {
|
||||
let client = TransmissionClient::new(config)?;
|
||||
|
||||
Ok(TransmissionAction { client })
|
||||
|
|
@ -21,10 +21,14 @@ impl TransmissionAction {
|
|||
#[async_trait::async_trait]
|
||||
impl Action for TransmissionAction {
|
||||
/// Return the name of the action
|
||||
fn name(&self) -> &str {
|
||||
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 =
|
||||
|
|
@ -37,7 +41,8 @@ impl Action for TransmissionAction {
|
|||
match self.client.submit_magnet(&magnet.link).await {
|
||||
Ok(_) => {
|
||||
debug!(
|
||||
"Successfully submitted magnet link to Transmission: {}",
|
||||
"Successfully submitted magnet link to {}: {}",
|
||||
Self::name(),
|
||||
magnet.title
|
||||
);
|
||||
debug!("Magnet link: {}", magnet.link);
|
||||
|
|
@ -45,7 +50,7 @@ impl Action for TransmissionAction {
|
|||
processed_magnets.push(magnet);
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("Failed to submit magnet link to Transmission: {}", e);
|
||||
warn!("Failed to submit magnet link to {}: {}", Self::name(), e);
|
||||
failed_magnets.push(magnet);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
use crate::app::Enableable;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Configuration for the Transmission action
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct TransmissionConfig {
|
||||
pub enable: bool,
|
||||
pub host: String,
|
||||
|
|
@ -10,3 +11,9 @@ pub struct TransmissionConfig {
|
|||
pub port: u16,
|
||||
pub download_dir: String,
|
||||
}
|
||||
|
||||
impl Enableable for TransmissionConfig {
|
||||
fn is_enabled(&self) -> bool {
|
||||
self.enable
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue