mirror of
https://github.com/lifegpc/pixiv_downloader.git
synced 2026-07-08 01:32:41 +08:00
add support for ugoira
add progress bar
This commit is contained in:
@@ -4,7 +4,10 @@ use crate::cookies::Cookie;
|
||||
use crate::cookies::CookieJar;
|
||||
use crate::gettext;
|
||||
use crate::list::NonTailList;
|
||||
use crate::opthelper::OptHelper;
|
||||
use futures_util::StreamExt;
|
||||
use indicatif::ProgressBar;
|
||||
use indicatif::ProgressStyle;
|
||||
use json::JsonValue;
|
||||
use reqwest::{Client, IntoUrl, RequestBuilder, Response};
|
||||
use spin_on::spin_on;
|
||||
@@ -275,7 +278,23 @@ impl WebClient {
|
||||
/// * `file_name` - File name
|
||||
/// * `r` - Response
|
||||
/// Note: If file already exists, will remove existing file first.
|
||||
pub fn download_stream<S: AsRef<OsStr> + ?Sized>(file_name: &S, r: Response) -> Result<(), ()> {
|
||||
pub fn download_stream<S: AsRef<OsStr> + ?Sized>(file_name: &S, r: Response, opt: &OptHelper) -> Result<(), ()> {
|
||||
let content_length = r.content_length();
|
||||
let use_progress_bar = match &content_length {
|
||||
Some(_) => { opt.use_progress_bar() }
|
||||
None => { false }
|
||||
};
|
||||
let mut bar = if use_progress_bar {
|
||||
Some(ProgressBar::new(content_length.unwrap()))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if bar.is_some() {
|
||||
bar.as_mut().unwrap().set_style(ProgressStyle::default_bar()
|
||||
.template("[{elapsed_precise}] [{wide_bar:.green/yellow}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta}) {msg:40}")
|
||||
.progress_chars("#>-"));
|
||||
}
|
||||
let mut downloaded = 0usize;
|
||||
let p = Path::new(file_name);
|
||||
if p.exists() {
|
||||
let re = remove_file(p);
|
||||
@@ -284,6 +303,10 @@ impl WebClient {
|
||||
return Err(());
|
||||
}
|
||||
}
|
||||
if bar.is_some() {
|
||||
let tmp = p.file_name().unwrap_or(p.as_os_str());
|
||||
bar.as_mut().unwrap().set_message(gettext("Downloading \"<loc>\".").replace("<loc>", tmp.to_str().unwrap_or("<NULL>")));
|
||||
}
|
||||
let f = File::create(p);
|
||||
if f.is_err() {
|
||||
println!("{} {}", gettext("Failed to create file:"), f.unwrap_err());
|
||||
@@ -297,6 +320,10 @@ impl WebClient {
|
||||
return Err(());
|
||||
}
|
||||
let data = data.unwrap();
|
||||
downloaded += data.len();
|
||||
if bar.is_some() {
|
||||
bar.as_mut().unwrap().set_position(downloaded as u64);
|
||||
}
|
||||
let r = f.write(&data);
|
||||
if r.is_err() {
|
||||
println!("{} {}", gettext("Failed to write file:"), r.unwrap_err());
|
||||
|
||||
Reference in New Issue
Block a user