-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathsubscribe_download.rs
60 lines (46 loc) · 1.65 KB
/
subscribe_download.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
49
50
51
52
53
54
55
56
57
58
59
60
//! `cargo run --example subscribe_download`
extern crate env_logger;
extern crate spider;
use spider::percent_encoding::{percent_encode, NON_ALPHANUMERIC};
use spider::utils::log;
use spider::website::Website;
use env_logger::Env;
use spider::tokio;
use tokio::fs::OpenOptions;
use tokio::io::AsyncWriteExt;
#[tokio::main]
async fn main() {
// view the target dist for the downloads
std::fs::create_dir_all("./target/downloads").unwrap_or_default();
let env = Env::default()
.filter_or("RUST_LOG", "info")
.write_style_or("RUST_LOG_STYLE", "always");
env_logger::init_from_env(env);
let website_name = "https://rsseau.fr/en";
let mut website: Website = Website::new(website_name);
let mut rx2 = website.subscribe(888).unwrap();
tokio::spawn(async move {
while let Ok(page) = rx2.recv().await {
let download_file =
percent_encode(page.get_url().as_bytes(), NON_ALPHANUMERIC).to_string();
let download_file = if download_file.is_empty() {
"index"
} else {
&download_file
};
let download_file = format!("./target/downloads/{}.html", download_file);
let mut file = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(&download_file)
.await
.expect("Unable to open file");
if let Some(b) = page.get_bytes() {
file.write_all(b).await.unwrap_or_default();
}
log("downloaded", download_file)
}
});
website.crawl().await;
}