aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMariot Tsitoara <[email protected]>2019-07-27 11:48:41 +0200
committerMariot Tsitoara <[email protected]>2019-07-27 11:48:41 +0200
commit7461bc8352b86b7985a318b3ce3c575c9e306c68 (patch)
treea331945b6d02adad2ad701476cbe3cbdbabc813b
parentMerge pull request #6 from Atul9/cargo-fmt (diff)
downloadchan-downloader-7461bc8352b86b7985a318b3ce3c575c9e306c68.tar.xz
chan-downloader-7461bc8352b86b7985a318b3ce3c575c9e306c68.zip
Add indicatif progress bar
Remove println logs
-rw-r--r--Cargo.toml4
-rw-r--r--src/main.rs37
2 files changed, 29 insertions, 12 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 1572295..1e7d9ae 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,7 +1,7 @@
[package]
name = "chan-downloader"
description = "CLI to download all images/webms of a 4chan thread"
-version = "0.1.0"
+version = "0.1.1"
authors = ["Mariot Tsitoara <[email protected]>"]
edition = "2018"
license = "MIT"
@@ -13,6 +13,8 @@ categories = ["command-line-utilities"]
[dependencies]
clap = {version = "2.32", features = ["yaml"]}
+indicatif = "0.11.0"
+lazy_static = "1.3.0"
regex = "1.1.2"
reqwest = "0.9.12"
tempdir = "0.3.7"
diff --git a/src/main.rs b/src/main.rs
index 52848f2..31c2920 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,10 +1,13 @@
#[macro_use]
extern crate clap;
+#[macro_use]
+extern crate lazy_static;
extern crate regex;
extern crate reqwest;
extern crate tempdir;
use clap::{App, ArgMatches};
+use indicatif::{ProgressBar, ProgressStyle};
use regex::Regex;
use reqwest::{Client, StatusCode};
use tempdir::TempDir;
@@ -43,15 +46,19 @@ fn save_image(url: &str, name: &str, client: &Client) -> Result<(String), String
StatusCode::NOT_FOUND => {
return Err(String::from("File not found"));
}
- s => return Err(String::from(format!("Received response status: {:?}", s))),
+ s => return Err(format!("Received response status: {:?}", s)),
};
Ok(String::from(file_name))
}
fn download_thread(thread_link: &str, matches: &ArgMatches, client: &Client) {
let workpath = env::current_dir().unwrap();
- let re =
- Regex::new(r"(//i(?:s)?\d*\.(?:4cdn|4chan)\.org/\w+/(\d+\.(?:jpg|png|gif|webm)))").unwrap();
+
+ lazy_static! {
+ static ref RE: Regex =
+ Regex::new(r"(//i(?:s)?\d*\.(?:4cdn|4chan)\.org/\w+/(\d+\.(?:jpg|png|gif|webm)))")
+ .unwrap();
+ }
let url_vec: Vec<&str> = thread_link.split('/').collect();
let board = url_vec[3];
@@ -72,18 +79,23 @@ fn download_thread(thread_link: &str, matches: &ArgMatches, client: &Client) {
let directory = workpath.join("downloads").join(board).join(thread);
if !directory.exists() {
match create_dir_all(&directory) {
- Ok(_) => println!("Created new directory: {}", directory.display()),
+ Ok(_) => {}
Err(err) => eprintln!("Failed to create new directory: {}", err),
}
- } else {
- println!("Using existing directory: {}", directory.display())
}
let mut thread_page = load(thread_link, client);
- for cap in re
- .captures_iter(thread_page.text().unwrap().as_str())
- .step_by(2)
- {
+ let page_string = thread_page.text().unwrap();
+ let links_iter = RE.captures_iter(page_string.as_str());
+
+ let number_of_links = RE.captures_iter(page_string.as_str()).count() / 2;
+ let pb = ProgressBar::new(number_of_links as u64);
+ pb.set_style(ProgressStyle::default_bar()
+ .template("{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} {msg} ({eta})")
+ .progress_chars("#>-"));
+
+ pb.tick();
+ for cap in links_iter.step_by(2) {
let img_path = directory.join(&cap[2]);
if !img_path.exists() {
match save_image(
@@ -91,9 +103,12 @@ fn download_thread(thread_link: &str, matches: &ArgMatches, client: &Client) {
img_path.to_str().unwrap(),
client,
) {
- Ok(name) => println!("New file: {}", name),
+ Ok(_) => {}
Err(err) => eprintln!("Error: {}", err),
}
}
+ pb.set_message(&cap[2].to_string());
+ pb.inc(1);
}
+ pb.finish_with_message("Done");
}