diff --git a/src/args/mod.rs b/src/args/mod.rs index 5e78d06..1056a42 100644 --- a/src/args/mod.rs +++ b/src/args/mod.rs @@ -1,6 +1,6 @@ use clap::Parser; -#[derive(Parser)] +#[derive(Debug, Parser)] #[command(version)] pub struct Args { /// User Agent to use for requests @@ -17,4 +17,13 @@ pub struct Args { /// Async jobs to use for fetching #[arg(short, long, default_value = "4")] pub jobs: usize, + + /// Delay for rate-limits (ms) + #[arg(short, long, default_value = "1000", value_parser = parse_duration)] + pub delay: std::time::Duration, +} + +fn parse_duration(arg: &str) -> Result { + let seconds = arg.parse()?; + Ok(std::time::Duration::from_millis(seconds)) } diff --git a/src/main.rs b/src/main.rs index b14ae51..5449b39 100644 --- a/src/main.rs +++ b/src/main.rs @@ -89,25 +89,28 @@ async fn main() -> ExitCode { async move { // "thread" loop { - this_prog.set_message(format!("\x1b[30m[{i}/{urls_ammount}] \x1b[36mscraping {url:?}\x1b[0m")); + this_prog.set_message(format!("\x1b[30m[{i: >4}/{urls_ammount}] \x1b[36mscraping {url:?}\x1b[0m")); let resp = client.get(&url).send().await.unwrap(); match extract_img_url(&resp.text().await.unwrap()) { Ok(img_url) => { if img_url.is_empty() { this_prog.abandon_with_message(format!( - "\x1b[30m[{i}/{urls_ammount}] \x1b[1;31mimage url not found\x1b[0m" + "\x1b[30m[{i: >4}/{urls_ammount}] \x1b[1;31mimage url not found\x1b[0m" )); } else { this_prog.finish_with_message(format!( - "\x1b[30m[{i}/{urls_ammount}] \x1b[32mfound image url: {img_url}\x1b[0m" + "\x1b[30m[{i: >4}/{urls_ammount}] \x1b[32mfound image url: {img_url}\x1b[0m" )); } break img_url; } Err(_) => { this_prog - .set_message(format!("\x1b[30m[{i}/{urls_ammount}] \x1b[31mratelimited, retrying after 1 second\x1b[0m")); - tokio::time::sleep(std::time::Duration::from_millis(1000)).await; + .set_message(format!( + "\x1b[30m[{i: >4}/{urls_ammount}] \x1b[31mratelimited, retrying after {}ms\x1b[0m", + args.delay.as_millis()) + ); + tokio::time::sleep(args.delay).await; continue; } }