Skip to content

Commit

Permalink
feat(args): Option for defining timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
pwnwriter committed Jul 10, 2023
1 parent 76ef179 commit da55ddb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ pub struct Cli {
/// Height of the website // URL
pub height: Option<u32>,

#[arg(short = 'k', long, default_value = "10")]
/// Define timeout for urls
pub timeout_value: u64,

#[arg(short, long)]
/// Silent mode (suppress all console output)
pub silent: bool,

}
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
cli.binary_path,
cli.width,
cli.height,
cli.timeout_value,
cli.silent,
)
.await
Expand All @@ -58,6 +59,7 @@ async fn run(
binary_path: String,
width: Option<u32>,
height: Option<u32>,
timeout_value: u64,
silent: bool,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
// Check if the browser binary path is valid
Expand Down Expand Up @@ -131,7 +133,7 @@ async fn run(

for chunk in url_chunks {
let n_tab = browser.new_page("about:blank").await?;
let h = tokio::spawn(take_screenshots(n_tab, chunk, silent));
let h = tokio::spawn(take_screenshots(n_tab, chunk, silent, timeout_value));
handles.push(h);
}

Expand All @@ -154,10 +156,11 @@ async fn take_screenshots(
page: Page,
urls: Vec<reqwest::Url>,
silent: bool,
timeout_value: u64,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
for url in urls {
let url = url.as_str();
if let Ok(Ok(_res)) = timeout(Duration::from_secs(10), get(url)).await {
if let Ok(Ok(_res)) = timeout(Duration::from_secs(timeout_value), get(url)).await {
let filename = url.replace("://", "-").replace('/', "_") + ".png";
page.goto(url)
.await?
Expand Down

0 comments on commit da55ddb

Please sign in to comment.