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

3
.dockerignore Normal file
View File

@ -0,0 +1,3 @@
.git/
target/
.env

3
.env Normal file
View File

@ -0,0 +1,3 @@
T_URL=http://192.168.1.150:9091/transmission/rpc
T_USER=kjuulh
T_PASSWORD=Z7TmqcUTkS3aNy

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.env
target/

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/twatch.iml" filepath="$PROJECT_DIR$/.idea/twatch.iml" />
</modules>
</component>
</project>

12
.idea/twatch.iml Normal file
View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="CPP_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/twatch/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/twatch_core/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

1081
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

6
Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[workspace]
members = [
"twatch_core",
"twatch"
]

14
Dockerfile Normal file
View File

@ -0,0 +1,14 @@
FROM rust:slim-bullseye AS builder
COPY . /app/builder/
WORKDIR /app/builder/
RUN cargo build --release
FROM debian:bullseye-slim
COPY --from=builder /app/builder/target/release/ /app/
WORKDIR /app/
CMD ["./twatch"]

13
twatch/Cargo.toml Normal file
View File

@ -0,0 +1,13 @@
[package]
name = "twatch"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
twatch_core = {path="../twatch_core"}
tokio = {version="1.17.0", features=["full"]}
env_logger="0.9.0"
log = "0.4.16"
dotenv = "0.15.0"

23
twatch/src/main.rs Normal file
View File

@ -0,0 +1,23 @@
use std::env;
use std::env::VarError;
use std::time::Duration;
use log::info;
use twatch_core::App;
use tokio::time::sleep;
#[tokio::main]
async fn main() -> Result<(), VarError> {
env_logger::init();
dotenv::dotenv();
let url = env::var("T_URL")?;
let user = env::var("T_USER")?;
let password = env::var("T_PASSWORD")?;
let cycle_seconds = env::var("T_CYCLE").unwrap_or("30".to_string()).parse::<u64>().unwrap_or(30);
loop {
info!("running clean up cycle");
let _ = App::new(&url, &user, &password).run().await;
sleep(Duration::from_secs(cycle_seconds)).await
}
}

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;
}
}