diff --git a/README.md b/README.md index 8a4d989..e3995fc 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,13 @@ Paperoni is a web article downloader written in Rust. The downloaded articles ar paperoni https://en.wikipedia.org/wiki/Pepperoni ``` -Paperoni also supports passing multiple links as arguments. If you are on a Unix-like OS, you can simply do something like this: +Paperoni also supports passing multiple links as arguments. These can be read from a file using the `-f` flag. + +```sh +paperoni -f links.txt +``` + +Alternatively, if you are on a Unix-like OS, you can simply do something like this: ```sh cat links.txt | xargs paperoni diff --git a/src/cli.rs b/src/cli.rs index 474223b..a456f29 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -18,4 +18,11 @@ It takes a url and downloads the article content from it and saves it to an epub .help("Urls of web articles") .multiple(true), ) + .arg( + Arg::with_name("file") + .short("f") + .long("file") + .help("Input file containing links") + .takes_value(true), + ) } diff --git a/src/main.rs b/src/main.rs index 4e403b6..450cf3e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ #[macro_use] extern crate lazy_static; -use std::fs::File; +use std::{fs::File, io::Read}; use async_std::task; use epub_builder::{EpubBuilder, EpubContent, ZipLibrary}; @@ -15,8 +15,27 @@ use extractor::Extractor; fn main() { let app = cli::cli_init(); let arg_matches = app.get_matches(); + let mut urls: Vec = match arg_matches.value_of("file") { + Some(file_name) => { + if let Ok(mut file) = File::open(file_name) { + let mut content = String::new(); + match file.read_to_string(&mut content) { + Ok(_) => content.lines().map(|line| line.to_owned()).collect(), + Err(_) => vec![], + } + } else { + println!("Unable to open file: {}", file_name); + vec![] + } + } + None => vec![], + }; + if let Some(vals) = arg_matches.values_of("urls") { - let urls = vals.map(|val| val.to_string()).collect::>(); + urls.extend(vals.map(|val| val.to_string())); + } + + if !urls.is_empty() { download(urls); } }