Minor refactor and add non zero exit upon failure to download any article
- Move printing of the successfully downloaded articles into main.rs - Add summary text
This commit is contained in:
parent
b496abb576
commit
c0323a6ae4
2 changed files with 34 additions and 20 deletions
25
src/epub.rs
25
src/epub.rs
|
@ -1,6 +1,5 @@
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
|
||||||
use comfy_table::presets::{UTF8_FULL, UTF8_HORIZONTAL_BORDERS_ONLY};
|
|
||||||
use comfy_table::{Attribute, Cell, CellAlignment, Color, ContentArrangement, Table};
|
use comfy_table::{Attribute, Cell, CellAlignment, Color, ContentArrangement, Table};
|
||||||
use epub_builder::{EpubBuilder, EpubContent, ZipLibrary};
|
use epub_builder::{EpubBuilder, EpubContent, ZipLibrary};
|
||||||
use indicatif::{ProgressBar, ProgressStyle};
|
use indicatif::{ProgressBar, ProgressStyle};
|
||||||
|
@ -13,6 +12,7 @@ use crate::{
|
||||||
pub fn generate_epubs(
|
pub fn generate_epubs(
|
||||||
articles: Vec<Extractor>,
|
articles: Vec<Extractor>,
|
||||||
merged: Option<&String>,
|
merged: Option<&String>,
|
||||||
|
successful_articles_table: &mut Table,
|
||||||
) -> Result<(), Vec<PaperoniError>> {
|
) -> Result<(), Vec<PaperoniError>> {
|
||||||
let bar = ProgressBar::new(articles.len() as u64);
|
let bar = ProgressBar::new(articles.len() as u64);
|
||||||
let style = ProgressStyle::default_bar().template(
|
let style = ProgressStyle::default_bar().template(
|
||||||
|
@ -21,18 +21,11 @@ pub fn generate_epubs(
|
||||||
bar.set_style(style);
|
bar.set_style(style);
|
||||||
bar.set_message("Generating epubs");
|
bar.set_message("Generating epubs");
|
||||||
|
|
||||||
let mut base_table = Table::new();
|
|
||||||
base_table
|
|
||||||
.load_preset(UTF8_FULL)
|
|
||||||
.load_preset(UTF8_HORIZONTAL_BORDERS_ONLY)
|
|
||||||
.set_content_arrangement(ContentArrangement::Dynamic);
|
|
||||||
|
|
||||||
let mut errors: Vec<PaperoniError> = Vec::new();
|
let mut errors: Vec<PaperoniError> = Vec::new();
|
||||||
let mut can_print_table = false;
|
|
||||||
|
|
||||||
match merged {
|
match merged {
|
||||||
Some(name) => {
|
Some(name) => {
|
||||||
base_table.set_header(vec![Cell::new("Table of Contents")
|
successful_articles_table.set_header(vec![Cell::new("Table of Contents")
|
||||||
.add_attribute(Attribute::Bold)
|
.add_attribute(Attribute::Bold)
|
||||||
.set_alignment(CellAlignment::Center)
|
.set_alignment(CellAlignment::Center)
|
||||||
.fg(Color::Green)]);
|
.fg(Color::Green)]);
|
||||||
|
@ -90,7 +83,7 @@ pub fn generate_epubs(
|
||||||
errors.push(error);
|
errors.push(error);
|
||||||
}
|
}
|
||||||
bar.inc(1);
|
bar.inc(1);
|
||||||
base_table.add_row(vec![article.metadata().title()]);
|
successful_articles_table.add_row(vec![article.metadata().title()]);
|
||||||
epub
|
epub
|
||||||
});
|
});
|
||||||
let mut out_file = File::create(&name).unwrap();
|
let mut out_file = File::create(&name).unwrap();
|
||||||
|
@ -106,17 +99,16 @@ pub fn generate_epubs(
|
||||||
|
|
||||||
bar.finish_with_message("Generated epub\n");
|
bar.finish_with_message("Generated epub\n");
|
||||||
println!("Created {:?}", name);
|
println!("Created {:?}", name);
|
||||||
can_print_table = true;
|
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
base_table
|
successful_articles_table
|
||||||
.set_header(vec![Cell::new("Downloaded articles")
|
.set_header(vec![Cell::new("Downloaded articles")
|
||||||
.add_attribute(Attribute::Bold)
|
.add_attribute(Attribute::Bold)
|
||||||
.set_alignment(CellAlignment::Center)
|
.set_alignment(CellAlignment::Center)
|
||||||
.fg(Color::Green)])
|
.fg(Color::Green)])
|
||||||
.set_content_arrangement(ContentArrangement::Dynamic);
|
.set_content_arrangement(ContentArrangement::Dynamic);
|
||||||
|
|
||||||
for article in articles {
|
for article in &articles {
|
||||||
let mut result = || -> Result<(), PaperoniError> {
|
let mut result = || -> Result<(), PaperoniError> {
|
||||||
let mut epub = EpubBuilder::new(ZipLibrary::new()?)?;
|
let mut epub = EpubBuilder::new(ZipLibrary::new()?)?;
|
||||||
let file_name = format!(
|
let file_name = format!(
|
||||||
|
@ -151,10 +143,9 @@ pub fn generate_epubs(
|
||||||
epub.generate(&mut out_file)?;
|
epub.generate(&mut out_file)?;
|
||||||
bar.inc(1);
|
bar.inc(1);
|
||||||
|
|
||||||
base_table.add_row(vec![article.metadata().title()]);
|
successful_articles_table.add_row(vec![article.metadata().title()]);
|
||||||
|
|
||||||
// println!("Created {:?}", file_name);
|
// println!("Created {:?}", file_name);
|
||||||
can_print_table = true;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
};
|
};
|
||||||
if let Err(mut error) = result() {
|
if let Err(mut error) = result() {
|
||||||
|
@ -165,9 +156,7 @@ pub fn generate_epubs(
|
||||||
bar.finish_with_message("Generated epubs\n");
|
bar.finish_with_message("Generated epubs\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if can_print_table {
|
|
||||||
println!("{}", base_table);
|
|
||||||
}
|
|
||||||
if errors.is_empty() {
|
if errors.is_empty() {
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
|
|
29
src/main.rs
29
src/main.rs
|
@ -3,7 +3,7 @@ extern crate lazy_static;
|
||||||
|
|
||||||
use async_std::stream;
|
use async_std::stream;
|
||||||
use async_std::task;
|
use async_std::task;
|
||||||
use comfy_table::presets::UTF8_HORIZONTAL_BORDERS_ONLY;
|
use comfy_table::presets::{UTF8_FULL, UTF8_HORIZONTAL_BORDERS_ONLY};
|
||||||
use comfy_table::{Attribute, Cell, CellAlignment, ContentArrangement, Table};
|
use comfy_table::{Attribute, Cell, CellAlignment, ContentArrangement, Table};
|
||||||
use futures::stream::StreamExt;
|
use futures::stream::StreamExt;
|
||||||
use indicatif::{ProgressBar, ProgressStyle};
|
use indicatif::{ProgressBar, ProgressStyle};
|
||||||
|
@ -78,12 +78,36 @@ fn download(app_config: AppConfig) {
|
||||||
articles
|
articles
|
||||||
});
|
});
|
||||||
bar.finish_with_message("Downloaded articles");
|
bar.finish_with_message("Downloaded articles");
|
||||||
match generate_epubs(articles, app_config.merged()) {
|
let mut succesful_articles_table = Table::new();
|
||||||
|
succesful_articles_table
|
||||||
|
.load_preset(UTF8_FULL)
|
||||||
|
.load_preset(UTF8_HORIZONTAL_BORDERS_ONLY)
|
||||||
|
.set_content_arrangement(ContentArrangement::Dynamic);
|
||||||
|
match generate_epubs(articles, app_config.merged(), &mut succesful_articles_table) {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(gen_epub_errors) => {
|
Err(gen_epub_errors) => {
|
||||||
errors.extend(gen_epub_errors);
|
errors.extend(gen_epub_errors);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
let successfully_downloaded_count = app_config.urls().len() - errors.len();
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"{} articles downloaded successfully. {}",
|
||||||
|
if successfully_downloaded_count == app_config.urls().len() {
|
||||||
|
"All".to_string()
|
||||||
|
} else {
|
||||||
|
successfully_downloaded_count.to_string()
|
||||||
|
},
|
||||||
|
if errors.len() > 0 {
|
||||||
|
errors.len().to_string() + " failed"
|
||||||
|
} else {
|
||||||
|
"".to_string()
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if successfully_downloaded_count > 0 {
|
||||||
|
println!("{}", succesful_articles_table);
|
||||||
|
}
|
||||||
if !errors.is_empty() {
|
if !errors.is_empty() {
|
||||||
println!(
|
println!(
|
||||||
"{}Failed article downloads{}",
|
"{}Failed article downloads{}",
|
||||||
|
@ -109,5 +133,6 @@ fn download(app_config: AppConfig) {
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
println!("{}", table_failed);
|
println!("{}", table_failed);
|
||||||
|
std::process::exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue