Clean things up a bit
This commit is contained in:
parent
3f2b002f52
commit
774a5ed4ac
9 changed files with 223 additions and 172 deletions
53
src/actions/transmission/client.rs
Normal file
53
src/actions/transmission/client.rs
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
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) -> 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