diff --git a/Cargo.lock b/Cargo.lock index e948a02..acb5148 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -52,6 +52,16 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "bt_bencode" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6398febbe0d54acd58256692c7d0608c0f49741ec830af6918129d6348f5c4bd" +dependencies = [ + "itoa", + "serde", +] + [[package]] name = "clap" version = "4.5.39" @@ -110,6 +120,12 @@ version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" +[[package]] +name = "itoa" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" + [[package]] name = "once_cell_polyfill" version = "1.70.1" @@ -143,6 +159,26 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "serde" +version = "1.0.219" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.219" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "strsim" version = "0.11.1" @@ -164,6 +200,7 @@ dependencies = [ name = "transmission-torrent-loader" version = "0.1.0" dependencies = [ + "bt_bencode", "clap", "walkdir", ] diff --git a/Cargo.toml b/Cargo.toml index 661ebef..7f524f4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,5 +4,6 @@ version = "0.1.0" edition = "2024" [dependencies] +bt_bencode = "0.8.2" clap = { version = "4.5.39", features = ["derive"] } walkdir = "2.5.0" diff --git a/src/main.rs b/src/main.rs index 72590a7..f005bdb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,7 @@ +use bt_bencode::Value; use clap::Parser; +use std::error::Error; +use std::fs::File; use std::path::{Path, PathBuf}; use walkdir::WalkDir; @@ -28,11 +31,39 @@ fn find_torrent_files(start_path: &Path) -> Vec { .into_iter() .filter_map(|e| e.ok()) .filter(|e| e.file_type().is_file()) - .filter(|e| e.path().extension().unwrap_or_default().to_str() == Some("torrent")) + .filter(|e| { + e.path() + .extension() + .map(|a| a == "torrent") + .unwrap_or(false) + }) .map(|e| e.into_path()) .collect() } +#[derive(Debug)] +enum TorrentType { + SingleFile, + Directory(String), +} + +fn get_torrent_type(torrent: &Path) -> Result> { + let f = File::open(torrent)?; + let value: Value = bt_bencode::from_reader(f)?; + let torrenttype = match value.get("info").unwrap().get("files") { + Some(_) => TorrentType::Directory( + value + .get("info") + .and_then(|v| v.get("name")) + .and_then(|v| v.as_str()) + .unwrap() + .to_owned(), + ), + _ => TorrentType::SingleFile, + }; + Ok(torrenttype) +} + fn main() { let args = Args::parse(); @@ -40,8 +71,9 @@ fn main() { println!("Source path: {}", args.source); println!("Destination path: {}", args.destination); println!("Rename: {}", args.rename); - let torrent_files = find_torrent_files(Path::new(&args.source)); + let source = Path::new(&args.source); + let torrent_files = find_torrent_files(source); for file in torrent_files { - println!("{}", file.display()); + println!("{:?}", get_torrent_type(&file)); } } diff --git a/tasks.md b/tasks.md index 57cb967..69e5af2 100755 --- a/tasks.md +++ b/tasks.md @@ -10,9 +10,6 @@ https://chat.deepseek.com/a/chat/s/70331337-52dd-4f53-9344-c680977f73dc - Создать привязку к systemd: service и timer - https://documentation.suse.com/smart/systems-management/html/systemd-working-with-timers/index.html - Создавать новую директорию в нужном месте -- Вскрывать торрент-файл, чтобы понять нужно ли переименование - - Если файл один, то не нужно, но нужно создавать директорию - - Если файлов много, то нужно переименовывать, создавать директорию не нужно - Добавлять торрент в transmission через transmission-remote - Переименовывать в transmission @@ -22,3 +19,6 @@ https://chat.deepseek.com/a/chat/s/70331337-52dd-4f53-9344-c680977f73dc - destination - rename - Обходить дерево директорий +- Вскрывать торрент-файл, чтобы понять нужно ли переименование + - Если файл один, то не нужно, но нужно создавать директорию + - Если файлов много, то нужно переименовывать, создавать директорию не нужно