87 lines
2.5 KiB
Rust
87 lines
2.5 KiB
Rust
#[macro_use]
|
|
extern crate clap;
|
|
extern crate image;
|
|
#[macro_use]
|
|
extern crate log;
|
|
extern crate printpdf;
|
|
extern crate reqwest;
|
|
extern crate scraper;
|
|
extern crate simple_logger;
|
|
extern crate tempfile;
|
|
|
|
use std::path::Path;
|
|
|
|
use clap::{App, AppSettings, Arg};
|
|
|
|
use crate::error::EcloadError;
|
|
use crate::loader::EcLoader;
|
|
|
|
pub mod error;
|
|
pub mod loader;
|
|
|
|
fn main() {
|
|
simple_logger::init_with_level(log::Level::Info).unwrap();
|
|
|
|
let app = App::new("ecload")
|
|
.version("0.1.0")
|
|
.author("Sebastian Hugentobler <sebastian@vanwa.ch>")
|
|
.about("Download books from https://www.e-codices.unifr.ch")
|
|
.arg(
|
|
Arg::with_name("output directory")
|
|
.short("o")
|
|
.long("out-dir")
|
|
.value_name("DIR")
|
|
.help("The directory where the resulting pdf is saved.")
|
|
.takes_value(true)
|
|
.default_value("."),
|
|
)
|
|
.arg(
|
|
Arg::with_name("size")
|
|
.short("s")
|
|
.long("size")
|
|
.value_name("SIZE")
|
|
.help("Sets the size of the downloaded images.")
|
|
.takes_value(true)
|
|
.possible_values(&loader::PageFormat::variants())
|
|
.default_value("medium")
|
|
.case_insensitive(true),
|
|
)
|
|
.arg(
|
|
Arg::with_name("id")
|
|
.short("i")
|
|
.long("id")
|
|
.value_name("ID")
|
|
.help("Id of the book to download. Copy the last two url parts on the overview page to get it (for example bbb/0003).")
|
|
.takes_value(true),
|
|
)
|
|
.setting(AppSettings::ArgRequiredElseHelp);
|
|
|
|
let matches = app.clone().get_matches();
|
|
|
|
let out_path_str = matches.value_of("output directory").unwrap();
|
|
let out_path = Path::new(out_path_str);
|
|
let size = value_t!(matches.value_of("size"), loader::PageFormat).unwrap();
|
|
let id = match matches.value_of("id") {
|
|
Some(id) => id,
|
|
None => {
|
|
println!("ID is a required argument:");
|
|
println!();
|
|
app.clone().print_help().ok();
|
|
println!();
|
|
|
|
return;
|
|
}
|
|
};
|
|
|
|
let title = &id.replace("/", "_");
|
|
|
|
match run(&out_path, size, id, title) {
|
|
Ok(_) => info!("finished!"),
|
|
Err(e) => error!("something went wrong: {}", e),
|
|
};
|
|
}
|
|
|
|
fn run(out_path: &Path, format: loader::PageFormat, id: &str, title: &str) -> Result<(), EcloadError> {
|
|
EcLoader::download_codice(id, title, out_path, format)
|
|
}
|