From: moo Date: Thu, 24 Jul 2025 09:25:16 +0000 (-0700) Subject: optimize fetching X-Git-Url: https://git.taranathan.com/?a=commitdiff_plain;h=f6b04b12cec25e2870f39285e658871aa99285d0;p=scrape.git optimize fetching --- diff --git a/Cargo.lock b/Cargo.lock index 4db2efc..3fec98e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1014,6 +1014,21 @@ dependencies = [ "new_debug_unreachable", ] +[[package]] +name = "futures" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + [[package]] name = "futures-channel" version = "0.3.31" @@ -1021,6 +1036,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10" dependencies = [ "futures-core", + "futures-sink", ] [[package]] @@ -1029,6 +1045,17 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" +[[package]] +name = "futures-executor" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + [[package]] name = "futures-io" version = "0.3.31" @@ -1064,6 +1091,7 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" dependencies = [ + "futures-channel", "futures-core", "futures-io", "futures-macro", @@ -3606,6 +3634,7 @@ name = "scrape" version = "0.1.0" dependencies = [ "csv", + "futures", "kalosm", "rand 0.9.1", "rayon", diff --git a/Cargo.toml b/Cargo.toml index 5010b17..baee083 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ edition = "2024" [dependencies] csv = "1.3.1" +futures = "0.3.31" kalosm = { version = "0.4.0", features = ["language"] } rand = "0.9.1" rayon = "1.10.0" diff --git a/src/fetch.rs b/src/fetch.rs index ee1fae9..f55aecf 100644 --- a/src/fetch.rs +++ b/src/fetch.rs @@ -1,3 +1,4 @@ +use futures::future::join_all; use reqwest; use rss::Channel; @@ -6,16 +7,21 @@ pub async fn fetch(urls: Vec) -> Result, Box) -> Result, Box> { - let mut channels = Vec::new(); - for url in urls { - let channel = fetch_rss_feed(&url).await; - if let Err(e) = channel { - eprintln!("Error fetching {}: {}", url, e); - continue; - } else if let Ok(channell) = channel { - channels.push(channell); - } - } + let futures = urls.iter().map(|url| fetch_rss_feed(url)); + let results: Vec>> = join_all(futures).await; + + let channels: Vec = results + .into_iter() + .zip(urls.iter()) + .filter_map(|(result, url)| match result { + Ok(channel) => Some(channel), + Err(e) => { + eprintln!("Error fetching {}: {}", url, e); + None + } + }) + .collect(); + Ok(channels) }