2020-10-12 19:33:01 +01:00
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
|
|
|
|
2021-06-01 10:23:22 +01:00
|
|
|
use std::process::exit;
|
|
|
|
|
2021-04-24 07:00:18 +01:00
|
|
|
use comfy_table::presets::{UTF8_FULL, UTF8_HORIZONTAL_BORDERS_ONLY};
|
2021-04-24 11:58:03 +01:00
|
|
|
use comfy_table::{ContentArrangement, Table};
|
2021-06-08 05:42:30 +01:00
|
|
|
use http::download;
|
2021-04-17 15:27:38 +01:00
|
|
|
use indicatif::{ProgressBar, ProgressStyle};
|
2020-04-30 09:05:53 +01:00
|
|
|
|
2020-05-16 08:09:44 +01:00
|
|
|
mod cli;
|
2021-02-06 09:59:03 +00:00
|
|
|
mod epub;
|
2021-04-17 10:04:06 +01:00
|
|
|
mod errors;
|
2020-05-01 14:17:59 +01:00
|
|
|
mod extractor;
|
2021-02-06 09:59:03 +00:00
|
|
|
/// This module is responsible for async HTTP calls for downloading
|
|
|
|
/// the HTML content and images
|
|
|
|
mod http;
|
2021-04-24 11:58:03 +01:00
|
|
|
mod logs;
|
2020-08-31 17:30:09 +01:00
|
|
|
mod moz_readability;
|
2020-05-01 14:17:59 +01:00
|
|
|
|
2021-02-06 14:03:02 +00:00
|
|
|
use cli::AppConfig;
|
2021-02-11 10:51:21 +00:00
|
|
|
use epub::generate_epubs;
|
2021-04-29 17:58:37 +01:00
|
|
|
use logs::display_summary;
|
2021-02-06 09:59:03 +00:00
|
|
|
|
2020-04-30 09:05:53 +01:00
|
|
|
fn main() {
|
2021-06-01 10:23:22 +01:00
|
|
|
let app_config = match cli::AppConfig::init_with_cli() {
|
|
|
|
Ok(app_config) => app_config,
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("{}", err);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
};
|
2021-02-01 08:28:07 +00:00
|
|
|
|
2021-06-01 10:23:22 +01:00
|
|
|
if !app_config.urls.is_empty() {
|
2021-06-08 05:42:30 +01:00
|
|
|
run(app_config);
|
2020-05-16 08:09:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 05:42:30 +01:00
|
|
|
fn run(app_config: AppConfig) {
|
2021-04-20 19:09:38 +01:00
|
|
|
let mut errors = Vec::new();
|
2021-06-08 05:42:30 +01:00
|
|
|
let mut partial_downloads = Vec::new();
|
2021-06-08 18:35:52 +01:00
|
|
|
|
|
|
|
if let Some(dir_name) = &app_config.output_directory {
|
|
|
|
let noun = if app_config.urls.len() > 1 {
|
|
|
|
"articles"
|
|
|
|
} else {
|
|
|
|
"article"
|
|
|
|
};
|
|
|
|
println!("Downloading {} to {}", noun, dir_name);
|
|
|
|
}
|
|
|
|
|
2021-06-01 10:23:22 +01:00
|
|
|
let bar = if app_config.can_disable_progress_bar {
|
2021-04-29 17:58:37 +01:00
|
|
|
ProgressBar::hidden()
|
|
|
|
} else {
|
2021-06-01 10:23:22 +01:00
|
|
|
let enabled_bar = ProgressBar::new(app_config.urls.len() as u64);
|
2021-04-29 17:58:37 +01:00
|
|
|
let style = ProgressStyle::default_bar().template(
|
2021-06-08 05:42:30 +01:00
|
|
|
"{spinner:.cyan} [{elapsed_precise}] {bar:40.white} {:>8} link {pos}/{len:7} {msg:.yellow/white}",
|
|
|
|
);
|
2021-04-29 17:58:37 +01:00
|
|
|
enabled_bar.set_style(style);
|
|
|
|
enabled_bar.enable_steady_tick(500);
|
|
|
|
enabled_bar
|
|
|
|
};
|
2021-06-08 18:35:52 +01:00
|
|
|
|
2021-06-08 05:42:30 +01:00
|
|
|
let articles = download(&app_config, &bar, &mut partial_downloads, &mut errors);
|
2021-04-17 15:27:38 +01:00
|
|
|
bar.finish_with_message("Downloaded articles");
|
2021-04-24 11:54:47 +01:00
|
|
|
|
2021-04-24 07:00:18 +01:00
|
|
|
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);
|
2021-04-29 17:58:37 +01:00
|
|
|
match generate_epubs(articles, &app_config, &mut succesful_articles_table) {
|
2021-04-17 10:04:06 +01:00
|
|
|
Ok(_) => (),
|
2021-04-20 19:09:38 +01:00
|
|
|
Err(gen_epub_errors) => {
|
|
|
|
errors.extend(gen_epub_errors);
|
|
|
|
}
|
2021-04-17 10:04:06 +01:00
|
|
|
};
|
2021-06-08 05:42:30 +01:00
|
|
|
|
|
|
|
let has_errors = !errors.is_empty() || !partial_downloads.is_empty();
|
2021-04-29 17:58:37 +01:00
|
|
|
display_summary(
|
2021-06-01 10:23:22 +01:00
|
|
|
app_config.urls.len(),
|
2021-04-29 17:58:37 +01:00
|
|
|
succesful_articles_table,
|
2021-06-08 05:42:30 +01:00
|
|
|
partial_downloads,
|
2021-04-29 17:58:37 +01:00
|
|
|
errors,
|
|
|
|
);
|
2021-06-08 05:42:30 +01:00
|
|
|
|
2021-06-01 10:23:22 +01:00
|
|
|
if app_config.is_logging_to_file {
|
2021-04-29 17:58:37 +01:00
|
|
|
println!(
|
|
|
|
"Log written to paperoni_{}.log\n",
|
2021-06-01 10:23:22 +01:00
|
|
|
app_config.start_time.format("%Y-%m-%d_%H-%M-%S")
|
2021-04-29 17:58:37 +01:00
|
|
|
);
|
2021-06-08 05:42:30 +01:00
|
|
|
} else if has_errors && !app_config.is_logging_to_file {
|
|
|
|
println!("\nRun paperoni with the --log-to-file flag to create a log file");
|
2021-04-29 17:58:37 +01:00
|
|
|
}
|
2021-06-08 05:42:30 +01:00
|
|
|
|
2021-04-24 11:58:03 +01:00
|
|
|
if has_errors {
|
2021-04-24 07:00:18 +01:00
|
|
|
std::process::exit(1);
|
2021-04-20 19:09:38 +01:00
|
|
|
}
|
2020-04-30 09:05:53 +01:00
|
|
|
}
|