Skip to content

Commit

Permalink
feat(improved code): threaded downloading and writing to file
Browse files Browse the repository at this point in the history
  • Loading branch information
XDream8 committed Jan 13, 2023
1 parent b7d201c commit 5ea5251
Showing 1 changed file with 40 additions and 29 deletions.
69 changes: 40 additions & 29 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,36 +77,47 @@ fn action(c: &Context) {
format!("{}", rm_duplicate_lines).yellow()
);

// fetch urls and write to file
let mut n: u8 = 1;

// create file
let mut file = File::create(&filename).expect("Error encountered while creating a file");

// this is where we store fetched content
let mut body: String = String::new();

for uri in urls {
println!("{}) {}", format!("{}", n).cyan().bold(), uri.yellow());

// Fetch url
match fetch(&uri, &mut body) {
Ok(f) => f,
Err(e) => {
println!("Couldn't fetch url!\n{:?}", e);
}
std::thread::scope(|s| {
// create file
let _file = File::create(&filename).expect("Error encountered while creating a file");

let fname = &filename;

// info
println!("{}", "Starting downloads(threaded)".blue().bold());

for uri in urls.iter() {
s.spawn(move || {
// this is where we store fetched content
let mut body: String = String::new();

// Fetch url
match fetch(&uri, &mut body) {
Ok(f) => f,
Err(e) => {
println!("Couldn't fetch url!\n{:?}", e);
}
}

println!(
"{} ({}) {}",
"fetched".green().bold(),
uri.yellow(),
"successfully".green().bold()
);

let f = File::options().append(true).open(&fname);

// write to file
match f.expect("error").write_all(body.as_bytes()) {
Ok(f) => f,
Err(e) => {
println!("Couldn't write fetched content to file!\n{:?}", e);
}
}
});
}

// write to file
match file.write_all(body.as_bytes()) {
Ok(f) => f,
Err(e) => {
println!("Couldn't write fetched content to file!\n{:?}", e);
}
}

n += 1;
}
});

// remove duplicates if -rmd flag is used
if rm_duplicate_lines == true {
Expand Down

0 comments on commit 5ea5251

Please sign in to comment.