Check torrent type

This commit is contained in:
2025-06-13 17:45:10 +03:00
parent 5cfdf1f173
commit f101dd7539
4 changed files with 76 additions and 6 deletions
Generated
+37
View File
@@ -52,6 +52,16 @@ dependencies = [
"windows-sys", "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]] [[package]]
name = "clap" name = "clap"
version = "4.5.39" version = "4.5.39"
@@ -110,6 +120,12 @@ version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]]
name = "itoa"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"
[[package]] [[package]]
name = "once_cell_polyfill" name = "once_cell_polyfill"
version = "1.70.1" version = "1.70.1"
@@ -143,6 +159,26 @@ dependencies = [
"winapi-util", "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]] [[package]]
name = "strsim" name = "strsim"
version = "0.11.1" version = "0.11.1"
@@ -164,6 +200,7 @@ dependencies = [
name = "transmission-torrent-loader" name = "transmission-torrent-loader"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bt_bencode",
"clap", "clap",
"walkdir", "walkdir",
] ]
+1
View File
@@ -4,5 +4,6 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
bt_bencode = "0.8.2"
clap = { version = "4.5.39", features = ["derive"] } clap = { version = "4.5.39", features = ["derive"] }
walkdir = "2.5.0" walkdir = "2.5.0"
+35 -3
View File
@@ -1,4 +1,7 @@
use bt_bencode::Value;
use clap::Parser; use clap::Parser;
use std::error::Error;
use std::fs::File;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use walkdir::WalkDir; use walkdir::WalkDir;
@@ -28,11 +31,39 @@ fn find_torrent_files(start_path: &Path) -> Vec<PathBuf> {
.into_iter() .into_iter()
.filter_map(|e| e.ok()) .filter_map(|e| e.ok())
.filter(|e| e.file_type().is_file()) .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()) .map(|e| e.into_path())
.collect() .collect()
} }
#[derive(Debug)]
enum TorrentType {
SingleFile,
Directory(String),
}
fn get_torrent_type(torrent: &Path) -> Result<TorrentType, Box<dyn Error>> {
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() { fn main() {
let args = Args::parse(); let args = Args::parse();
@@ -40,8 +71,9 @@ fn main() {
println!("Source path: {}", args.source); println!("Source path: {}", args.source);
println!("Destination path: {}", args.destination); println!("Destination path: {}", args.destination);
println!("Rename: {}", args.rename); 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 { for file in torrent_files {
println!("{}", file.display()); println!("{:?}", get_torrent_type(&file));
} }
} }
+3 -3
View File
@@ -10,9 +10,6 @@ https://chat.deepseek.com/a/chat/s/70331337-52dd-4f53-9344-c680977f73dc
- Создать привязку к systemd: service и timer - Создать привязку к systemd: service и timer
- https://documentation.suse.com/smart/systems-management/html/systemd-working-with-timers/index.html - https://documentation.suse.com/smart/systems-management/html/systemd-working-with-timers/index.html
- Создавать новую директорию в нужном месте - Создавать новую директорию в нужном месте
- Вскрывать торрент-файл, чтобы понять нужно ли переименование
- Если файл один, то не нужно, но нужно создавать директорию
- Если файлов много, то нужно переименовывать, создавать директорию не нужно
- Добавлять торрент в transmission через transmission-remote - Добавлять торрент в transmission через transmission-remote
- Переименовывать в transmission - Переименовывать в transmission
@@ -22,3 +19,6 @@ https://chat.deepseek.com/a/chat/s/70331337-52dd-4f53-9344-c680977f73dc
- destination - destination
- rename - rename
- Обходить дерево директорий - Обходить дерево директорий
- Вскрывать торрент-файл, чтобы понять нужно ли переименование
- Если файл один, то не нужно, но нужно создавать директорию
- Если файлов много, то нужно переименовывать, создавать директорию не нужно