aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/epub.rs6
-rw-r--r--src/main.rs13
2 files changed, 13 insertions, 6 deletions
diff --git a/src/epub.rs b/src/epub.rs
index 1e7402e..050e9cf 100644
--- a/src/epub.rs
+++ b/src/epub.rs
@@ -16,9 +16,6 @@ use tokio::fs::{self, File};
use tokio_util::io::StreamReader;
use zip::{CompressionMethod, ZipWriter, write::FileOptions};
-// TODO: make configurable.
-const MAX_CONCURRENT: usize = 4;
-
/// Creates and writes container.xml.
fn write_container_xml<W: Write>(out: &mut W, opf_full_path: &RelativePathBuf) -> Result<()> {
// Prepare file contents.
@@ -41,12 +38,13 @@ pub async fn download_all_files(
client: &Client,
file_entries: &[FileEntry],
dest_root: &Path,
+ max_concurrent: usize,
) -> Result<()> {
let mut downloading = FuturesUnordered::new();
let mut files_iter = file_entries.iter();
// Start downloading the first n files.
- for entry in files_iter.by_ref().take(MAX_CONCURRENT) {
+ for entry in files_iter.by_ref().take(max_concurrent) {
downloading.push(download_one_file(client, entry, dest_root));
}
diff --git a/src/main.rs b/src/main.rs
index 8712d4f..b4775d6 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -11,7 +11,7 @@ use crate::epub::{create_epub_archive, download_all_files};
use crate::http_client::build_authenticated_client;
use crate::models::{Chapter, EpubResponse, FileEntry, Paginated};
use anyhow::{Context, Result, anyhow};
-use clap::Parser;
+use clap::{Parser, value_parser};
use directories::{BaseDirs, UserDirs};
use reqwest::Client;
@@ -28,6 +28,9 @@ struct Args {
/// Do not download files. Use if they were already downloaded in a previous run.
#[arg(long = "skip-download")]
skip_download: bool,
+ /// Number of files to download in parallel. Limit is 8 (be polite).
+ #[arg(long, value_parser=value_parser!(u32).range(1..=8), default_value_t = 4)]
+ parallel: u32,
}
/// Fetches EPUB structural data (like the chapters URL).
@@ -132,7 +135,13 @@ async fn main() -> Result<()> {
let epub_root = data_root.join("files").join(&args.bookid);
if !args.skip_download {
println!("Downloading files from the server...");
- download_all_files(&client, &file_entries, &epub_root).await?;
+ download_all_files(
+ &client,
+ &file_entries,
+ &epub_root,
+ args.parallel.try_into()?, // Will work as 1..=8 will fit into any usize.
+ )
+ .await?;
}
println!("Generating the EPUB file...");
create_epub_archive(&epub_data, &epub_root, &epub_path, &file_entries, &chapters)?;