Bug fix and add printing of the name of the extracted EPUB

The fix prevents creating the res directory if it already exists
This commit is contained in:
Kenneth Gitere 2020-11-23 09:01:05 +03:00
parent b0e402d685
commit ab800d0174

View file

@ -2,6 +2,7 @@
extern crate lazy_static; extern crate lazy_static;
use std::fs::File; use std::fs::File;
use std::path::Path;
use async_std::{fs::create_dir, fs::remove_dir_all, task}; use async_std::{fs::create_dir, fs::remove_dir_all, task};
use epub_builder::{EpubBuilder, EpubContent, ZipLibrary}; use epub_builder::{EpubBuilder, EpubContent, ZipLibrary};
@ -49,15 +50,17 @@ fn download(urls: Vec<String>) {
let mut extractor = Extractor::from_html(&html); let mut extractor = Extractor::from_html(&html);
extractor.extract_content(&url); extractor.extract_content(&url);
if extractor.article().is_some() { if extractor.article().is_some() {
create_dir("res/") if !Path::new("res/").exists() {
.await create_dir("res/")
.expect("Unable to create res/ output folder"); .await
.expect("Unable to create res/ output folder");
}
extractor extractor
.download_images(&Url::parse(&url).unwrap()) .download_images(&Url::parse(&url).unwrap())
.await .await
.expect("Unable to download images"); .expect("Unable to download images");
let mut out_file = let file_name = format!("{}.epub", extractor.metadata().title());
File::create(format!("{}.epub", extractor.metadata().title())).unwrap(); let mut out_file = File::create(&file_name).unwrap();
let mut html_buf = Vec::new(); let mut html_buf = Vec::new();
extractor extractor
.article() .article()
@ -84,6 +87,7 @@ fn download(urls: Vec<String>) {
epub.generate(&mut out_file).unwrap(); epub.generate(&mut out_file).unwrap();
println!("Cleaning up"); println!("Cleaning up");
remove_dir_all("res/").await.unwrap(); remove_dir_all("res/").await.unwrap();
println!("Created {:?}", file_name);
} }
} }
}) })