From 65e6deaabce3d06ae542a178b83ddeed61bc0f65 Mon Sep 17 00:00:00 2001 From: yggverse Date: Tue, 8 Jul 2025 15:11:58 +0300 Subject: [PATCH] use binary api --- Cargo.toml | 2 -- README.md | 10 +++++----- src/api.rs | 44 ++++++++++++++++++++++++++++++++++++++------ src/config.rs | 2 +- test/api/0.json | 0 test/api/1.json | 1 - 6 files changed, 44 insertions(+), 15 deletions(-) delete mode 100644 test/api/0.json delete mode 100644 test/api/1.json diff --git a/Cargo.toml b/Cargo.toml index 0fd220d..7c964f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,8 +17,6 @@ clap = { version = "4.5", features = ["derive"] } hyper-util = "0.1" librqbit = {version = "9.0.0-beta.0", features = ["disable-upload"]} regex = "1.11" -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" tokio = { version = "1.45", features = ["full"] } tracing-subscriber = "0.3" url = "2.5" diff --git a/README.md b/README.md index aa28a5e..c716fcc 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Crawler for [Aquatic](https://github.com/greatest-ape/aquatic) BitTorrent tracke * [x] 1 * [ ] 2 * Import sources - * [x] IPv4 / IPv6 info-hash JSON/API (requires [PR#233](https://github.com/greatest-ape/aquatic/pull/233)) + * [x] IPv4 / IPv6 info-hash binary API (requires [PR#233](https://github.com/greatest-ape/aquatic/pull/233)) * [x] local file path * [ ] remote URL * Export options @@ -40,9 +40,9 @@ Crawler for [Aquatic](https://github.com/greatest-ape/aquatic) BitTorrent tracke ## Usage ``` bash -aquatic-crawler --infohash /path/to/info-hash-ipv4.json\ - --infohash /path/to/info-hash-ipv6.json\ - --infohash /path/to/another-source.json\ +aquatic-crawler --infohash /path/to/info-hash-ipv4.bin\ + --infohash /path/to/info-hash-ipv6.bin\ + --infohash /path/to/another-source.bin\ --tracker udp://host1:port\ --tracker udp://host2:port\ --preload /path/to/directory\ @@ -62,7 +62,7 @@ aquatic-crawler --infohash /path/to/info-hash-ipv4.json\ [default: ei] --infohash - Absolute path(s) or URL(s) to import infohashes from the Aquatic tracker JSON/API + Absolute path(s) or URL(s) to import infohashes from the Aquatic tracker binary API * PR#233 feature diff --git a/src/api.rs b/src/api.rs index 849c765..d0234a5 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1,17 +1,49 @@ /// Parse infohash from the source filepath, /// decode JSON to array on success, return None if the feed file is not reachable pub fn get(path: &str) -> Option> { + use std::io::Read; if path.contains("://") { todo!("URL sources yet not supported") } - let s = std::fs::read_to_string(path).ok()?; // is updating? - let r: Option> = serde_json::from_str(&s).ok(); // is incomplete? - r + const L: usize = 20; // v1 only + let mut r = Vec::new(); + let mut f = std::fs::File::open(path).ok()?; + loop { + let mut b = vec![0; L]; + let l = f.read(&mut b).ok()?; + if l != L { + break; + } + r.push( + b[..l] + .iter() + .map(|i| format!("{i:02x}")) + .collect::(), + ) + } + Some(r) } #[test] fn test() { - assert!(get("test/api/0.json").is_none()); - assert!(get("test/api/1.json").is_some()); - assert!(get("test/api/2.json").is_none()); + use std::fs; + + #[cfg(not(any(target_os = "linux", target_os = "macos",)))] + { + todo!() + } + + const P0: &str = "/tmp/aquatic-crawler-api-test-0.bin"; + const P1: &str = "/tmp/aquatic-crawler-api-test-1.bin"; + const P2: &str = "/tmp/aquatic-crawler-api-test-2.bin"; + + fs::write(P0, vec![]).unwrap(); + fs::write(P1, vec![1; 40]).unwrap(); // 20 + 20 bytes + + assert!(get(P0).is_some_and(|b| b.is_empty())); + assert!(get(P1).is_some_and(|b| b.len() == 2)); + assert!(get(P2).is_none()); + + fs::remove_file(P0).unwrap(); + fs::remove_file(P1).unwrap(); } diff --git a/src/config.rs b/src/config.rs index 425664c..4616801 100644 --- a/src/config.rs +++ b/src/config.rs @@ -11,7 +11,7 @@ pub struct Config { #[arg(short, long, default_value_t = String::from("ei"))] pub debug: String, - /// Absolute path(s) or URL(s) to import infohashes from the Aquatic tracker JSON/API + /// Absolute path(s) or URL(s) to import infohashes from the Aquatic tracker binary API /// /// * PR#233 feature #[arg(long)] diff --git a/test/api/0.json b/test/api/0.json deleted file mode 100644 index e69de29..0000000 diff --git a/test/api/1.json b/test/api/1.json deleted file mode 100644 index 181a695..0000000 --- a/test/api/1.json +++ /dev/null @@ -1 +0,0 @@ -["1","2","3"] \ No newline at end of file