reddit-magnet/src/actions/transmission/client.rs
Marc Plano-Lesay c3764c125a
feat: add the concept of tags
Tags are attached to all magnet links provided by a given source, and
passed to actions. This allows for e.g. better categorization in
Bitmagnet.
2025-05-08 20:26:04 +10:00

54 lines
1.6 KiB
Rust

use crate::actions::transmission::config::TransmissionConfig;
use color_eyre::eyre::{eyre, Result, WrapErr};
use transmission_rpc::{
types::{BasicAuth, TorrentAddArgs},
TransClient,
};
use url::Url;
/// High-level Transmission client
pub struct TransmissionClient {
client: TransClient,
download_dir: String,
}
impl TransmissionClient {
/// Create a new Transmission client from configuration
pub fn new(config: &TransmissionConfig) -> 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(TransmissionClient {
client,
download_dir: config.download_dir.clone(),
})
}
/// Submit a magnet link to Transmission
pub async fn submit_magnet(&mut self, magnet: &str, tags: Vec<&str>) -> Result<()> {
let args = TorrentAddArgs {
filename: Some(magnet.to_string()),
download_dir: Some(self.download_dir.clone()),
labels: Some(tags.iter().map(|t| t.to_string()).collect()),
..Default::default()
};
self.client
.torrent_add(args)
.await
.map_err(|e| eyre!("Failed to add torrent to Transmission: {}", e))?;
Ok(())
}
}