twatch/twatch_core/src/lib.rs

67 lines
2.0 KiB
Rust
Raw Normal View History

2022-10-28 23:16:00 +02:00
use log::info;
use transmission_rpc::types::{BasicAuth, Id, Result, Torrent, Torrents};
2022-04-04 22:22:59 +02:00
use transmission_rpc::TransClient;
pub struct App {
client: TransClient,
}
impl App {
pub fn new(url: &String, username: &String, password: &String) -> Self {
2022-10-28 23:16:00 +02:00
let auth = BasicAuth {
user: username.clone(),
password: password.clone(),
};
2022-04-04 22:22:59 +02:00
let client = TransClient::with_auth(url.as_str(), auth);
2022-10-28 23:16:00 +02:00
Self { client }
2022-04-04 22:22:59 +02:00
}
2022-10-28 23:16:00 +02:00
pub async fn run(mut self) -> Result<()> {
2022-04-04 22:22:59 +02:00
let response = self.client.torrent_get(None, None).await;
match response {
Ok(res) => self.scan_files(res.arguments).await.unwrap(),
2022-10-28 23:16:00 +02:00
Err(e) => println!("{}", e.to_string()),
2022-04-04 22:22:59 +02:00
}
return Ok(());
}
2022-10-28 23:16:00 +02:00
async fn scan_files(self, torrents: Torrents<Torrent>) -> Result<()> {
2022-04-04 22:22:59 +02:00
let mut torrents_to_remove = Vec::new();
for torrent in torrents.torrents {
match (torrent.id, torrent.is_stalled) {
(Some(id), Some(true)) => {
2022-10-28 23:16:00 +02:00
info!(
"processing: (id: {}, name: {})",
id,
torrent
.name
.unwrap_or("name could not be found from torrent".to_string())
);
2022-04-04 22:22:59 +02:00
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(());
}
2022-10-28 23:16:00 +02:00
async fn clean_up_torrents(mut self, torrent_ids: Vec<i64>) {
self.client
.torrent_remove(torrent_ids.iter().map(|ti| Id::Id(*ti)).collect(), true)
.await
.expect("to remove torrent");
2022-04-04 22:22:59 +02:00
}
2022-10-28 23:16:00 +02:00
}