-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathqueue.rs
48 lines (40 loc) · 1.38 KB
/
queue.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//! `cargo run --example queue`
extern crate spider;
use spider::tokio;
use spider::url::Url;
use spider::website::Website;
#[tokio::main]
async fn main() {
let mut website: Website = Website::new("https://rsseau.fr/en");
let mut rx2 = website.subscribe(16).unwrap();
let mut g = website.subscribe_guard().unwrap();
let q = website.queue(100).unwrap();
tokio::spawn(async move {
while let Ok(res) = rx2.recv().await {
let u = res.get_url();
println!("{:?}", u);
let mut url = Url::parse(u).expect("Failed to parse URL");
let mut segments: Vec<_> = url
.path_segments()
.map(|c| c.collect::<Vec<_>>())
.unwrap_or_else(Vec::new);
if segments.len() > 0 && segments[0] == "en" {
segments[0] = "fr";
let new_path = segments.join("/");
url.set_path(&new_path);
// get a new url here or perform an action and queue links
// pre-fetch all fr locales
let _ = q.send(url.into());
}
g.inc();
}
});
let start = std::time::Instant::now();
website.crawl().await;
let duration = start.elapsed();
println!(
"Time elapsed in website.crawl() is: {:?} for total pages: {:?}",
duration,
website.get_size().await
)
}