This commit is contained in:
2022-04-04 22:22:59 +02:00
commit 0a35246672
14 changed files with 1245 additions and 0 deletions

11
twatch_core/Cargo.toml Normal file
View File

@@ -0,0 +1,11 @@
[package]
name = "twatch_core"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
transmission-rpc = "0.3.6"
env_logger="0.9.0"
log = "0.4.16"

55
twatch_core/src/lib.rs Normal file
View File

@@ -0,0 +1,55 @@
use transmission_rpc::TransClient;
use transmission_rpc::types::{Result, BasicAuth, Torrent, Torrents, Id};
use log::{info};
pub struct App {
client: TransClient,
}
impl App {
pub fn new(url: &String, username: &String, password: &String) -> Self {
let auth = BasicAuth { user: username.clone(), password: password.clone() };
let client = TransClient::with_auth(url.as_str(), auth);
Self {
client
}
}
pub async fn run(&self) -> Result<()> {
let response = self.client.torrent_get(None, None).await;
match response {
Ok(res) => self.scan_files(res.arguments).await.unwrap(),
Err(e) => println!("{}", e.to_string())
}
return Ok(());
}
async fn scan_files(&self, torrents: Torrents<Torrent>) -> Result<()> {
let mut torrents_to_remove = Vec::new();
for torrent in torrents.torrents {
match (torrent.id, torrent.is_stalled) {
(Some(id), Some(true)) => {
info!("processing: (id: {}, name: {})", id, torrent.name.unwrap_or("name could not be found from torrent".to_string()));
torrents_to_remove.push(id);
}
_ => {
info!("did not fit criteria (everything is good for this torrent)");
continue;
}
}
}
if torrents_to_remove.len() != 0 {
info!("cleaning up torrents");
self.clean_up_torrents(torrents_to_remove).await;
} else {
info!("nothing to cleanup")
}
return Ok(());
}
async fn clean_up_torrents(&self, torrent_ids: Vec<i64>) {
self.client.torrent_remove(torrent_ids.iter().map(|ti| { Id::Id(*ti) }).collect(), true).await;
}
}