Skip to content

Commit

Permalink
set transmission configuration at initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
syrflover committed Jul 7, 2024
1 parent 317f90f commit d48bd98
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
22 changes: 22 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,39 @@ where
var.parse().expect("Please set dotenv to valid value")
}

fn env_opt<T>(key: &str) -> Option<T>
where
T: FromStr,
<T as FromStr>::Err: Debug,
{
env::var(key)
.ok()
.map(|var| var.parse().expect("Please set dotenv to valid value"))
}

#[derive(Debug, Deserialize)]
pub struct Config {
pub channels_config_url: String,
pub transmission_url: String,

pub download_dir: Option<String>,
pub speed_limit_up: Option<i32>,
pub speed_limit_down: Option<i32>,
pub download_queue_size: Option<i32>,
pub seed_queue_size: Option<i32>,
}

impl Config {
pub fn new() -> Self {
Self {
channels_config_url: env("CHANNELS_CONFIG_URL"),
transmission_url: env("TRANSMISSION_URL"),

download_dir: env_opt("DOWNLOAD_DIR"),
speed_limit_up: env_opt("SPEED_LIMIT_UP"),
speed_limit_down: env_opt("SPEED_LIMIT_DOWN"),
download_queue_size: env_opt("DOWNLOAD_QUEUE_SIZE"),
seed_queue_size: env_opt("SEED_QUEUE_SIZE"),
}
}
}
Expand Down
26 changes: 22 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use rss::Channel;
use transmission_rpc::{
types::{
Id, Torrent, TorrentAction, TorrentAddArgs, TorrentAddedOrDuplicate, TorrentGetField,
TorrentStatus,
Id, SessionSetArgs, Torrent, TorrentAction, TorrentAddArgs, TorrentAddedOrDuplicate,
TorrentGetField, TorrentStatus,
},
TransClient,
};
Expand Down Expand Up @@ -197,6 +197,26 @@ async fn run() {
.expect("can't parse transmission url"),
);

let transmission_config = SessionSetArgs {
download_dir: config.download_dir,
speed_limit_up_enabled: config.speed_limit_up.is_some().then_some(true),
speed_limit_up: config.speed_limit_up,
speed_limit_down_enabled: config.speed_limit_down.is_some().then_some(true),
speed_limit_down: config.speed_limit_down,
download_queue_enabled: config.download_queue_size.is_some().then_some(true),
download_queue_size: config.download_queue_size,
seed_queue_enabled: config.seed_queue_size.is_some().then_some(true),
seed_queue_size: config.seed_queue_size,
..Default::default()
};

println!("{:#?}", transmission_config);

transmission
.session_set(transmission_config)
.await
.expect("can't set transmission configuration");

let mut channels = Vec::new();

for channel_config in &channels_config {
Expand Down Expand Up @@ -336,8 +356,6 @@ async fn run() {

#[tokio::main]
async fn main() {
println!("Hello, world!");

dotenv::dotenv().ok();

run().await;
Expand Down

0 comments on commit d48bd98

Please sign in to comment.