Add a send to Transmission action
This commit is contained in:
parent
88419cbf97
commit
3f2b002f52
10 changed files with 952 additions and 83 deletions
1
src/actions/mod.rs
Normal file
1
src/actions/mod.rs
Normal file
|
|
@ -0,0 +1 @@
|
|||
pub mod transmission;
|
||||
99
src/actions/transmission.rs
Normal file
99
src/actions/transmission.rs
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
use crate::db::{Database, TransmissionProcessedTable};
|
||||
use color_eyre::eyre::{eyre, Result, WrapErr};
|
||||
use log::{debug, info, warn};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use transmission_rpc::{
|
||||
types::{BasicAuth, TorrentAddArgs},
|
||||
TransClient,
|
||||
};
|
||||
use url::Url;
|
||||
|
||||
/// Configuration for the Transmission action
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct TransmissionConfig {
|
||||
pub enable: bool,
|
||||
pub host: String,
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
pub port: u16,
|
||||
pub download_dir: String,
|
||||
}
|
||||
|
||||
/// Action for submitting magnet links to Transmission
|
||||
pub struct TransmissionAction {
|
||||
client: TransClient,
|
||||
download_dir: String,
|
||||
db: Database,
|
||||
}
|
||||
|
||||
impl TransmissionAction {
|
||||
pub async fn new(config: &TransmissionConfig, db: Database) -> Result<Self> {
|
||||
if !config.enable {
|
||||
return Err(eyre!("Transmission action is disabled"));
|
||||
}
|
||||
|
||||
let url_str = format!("{}:{}/transmission/rpc", config.host, config.port);
|
||||
let url = Url::parse(&url_str).wrap_err_with(|| format!("Invalid URL: {}", url_str))?;
|
||||
|
||||
let auth = BasicAuth {
|
||||
user: config.username.clone(),
|
||||
password: config.password.clone(),
|
||||
};
|
||||
|
||||
let client = TransClient::with_auth(url, auth);
|
||||
|
||||
Ok(TransmissionAction {
|
||||
client,
|
||||
download_dir: config.download_dir.clone(),
|
||||
db,
|
||||
})
|
||||
}
|
||||
|
||||
/// Process all unprocessed magnet links
|
||||
pub async fn process_unprocessed_magnets(&mut self) -> Result<usize> {
|
||||
let unprocessed_magnets = self
|
||||
.db
|
||||
.get_unprocessed_magnets_for_table::<TransmissionProcessedTable>()?;
|
||||
let mut processed_count = 0;
|
||||
|
||||
for magnet in unprocessed_magnets {
|
||||
if let Some(id) = magnet.id {
|
||||
match self.submit_magnet(&magnet.link).await {
|
||||
Ok(_) => {
|
||||
info!(
|
||||
"Successfully submitted magnet link to Transmission: {}",
|
||||
magnet.title
|
||||
);
|
||||
debug!("Magnet link: {}", magnet.link);
|
||||
self.db
|
||||
.mark_magnet_processed_for_table::<TransmissionProcessedTable>(id)?;
|
||||
processed_count += 1;
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("Failed to submit magnet link to Transmission: {}", e);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
warn!("Skipping magnet with null ID: {}", magnet.link);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(processed_count)
|
||||
}
|
||||
|
||||
/// Submit a magnet link to Transmission
|
||||
async fn submit_magnet(&mut self, magnet: &str) -> Result<()> {
|
||||
let args = TorrentAddArgs {
|
||||
filename: Some(magnet.to_string()),
|
||||
download_dir: Some(self.download_dir.clone()),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
self.client
|
||||
.torrent_add(args)
|
||||
.await
|
||||
.map_err(|e| eyre!("Failed to add torrent to Transmission: {}", e))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue