Skip to content

Commit

Permalink
--silent option for shot, pdf, html, refs #107
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Apr 27, 2023
1 parent 3edcd4b commit c2aa351
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/html.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Options:
--fail Fail with an error code if a page returns an
HTTP error
--skip Skip pages that return HTTP errors
--silent Do not output any messages
--help Show this message and exit.
```
<!-- [[[end]]] -->
1 change: 1 addition & 0 deletions docs/pdf.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Options:
--fail Fail with an error code if a page returns an
HTTP error
--skip Skip pages that return HTTP errors
--silent Do not output any messages
--help Show this message and exit.
```
<!-- [[[end]]] -->
1 change: 1 addition & 0 deletions docs/screenshots.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ Options:
--fail Fail with an error code if a page returns an
HTTP error
--skip Skip pages that return HTTP errors
--silent Do not output any messages
--help Show this message and exit.
```
<!-- [[[end]]] -->
31 changes: 23 additions & 8 deletions shot_scraper/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ def log_console_option(fn):
return fn


def silent_option(fn):
click.option("--silent", is_flag=True, help="Do not output any messages")(fn)
return fn


def skip_fail_options(fn):
click.option("--skip", is_flag=True, help="Skip pages that return HTTP errors")(fn)
click.option(
Expand Down Expand Up @@ -187,6 +192,7 @@ def cli():
@user_agent_option
@reduced_motion_option
@skip_fail_options
@silent_option
def shot(
url,
auth,
Expand Down Expand Up @@ -214,6 +220,7 @@ def shot(
reduced_motion,
skip,
fail,
silent,
):
"""
Take a single screenshot of a page or portion of a page.
Expand Down Expand Up @@ -293,6 +300,7 @@ def shot(
use_existing_page=use_existing_page,
log_requests=log_requests,
log_console=log_console,
silent=silent,
)
sys.stdout.buffer.write(shot)
else:
Expand All @@ -305,6 +313,7 @@ def shot(
log_console=log_console,
skip=skip,
fail=fail,
silent=silent,
)
except TimeoutError as e:
raise click.ClickException(str(e))
Expand Down Expand Up @@ -652,6 +661,7 @@ def javascript(
@click.option("--print-background", is_flag=True, help="Print background graphics")
@log_console_option
@skip_fail_options
@silent_option
def pdf(
url,
auth,
Expand All @@ -668,6 +678,7 @@ def pdf(
log_console,
skip,
fail,
silent,
):
"""
Create a PDF of the specified page
Expand Down Expand Up @@ -717,10 +728,8 @@ def pdf(

if output == "-":
sys.stdout.buffer.write(pdf)
else:
click.echo(
"Screenshot of '{}' written to '{}'".format(url, output), err=True
)
elif not silent:
click.echo("PDF of '{}' written to '{}'".format(url, output), err=True)

browser_obj.close()

Expand Down Expand Up @@ -752,6 +761,7 @@ def pdf(
@browser_option
@user_agent_option
@skip_fail_options
@silent_option
def html(
url,
auth,
Expand All @@ -764,6 +774,7 @@ def html(
user_agent,
skip,
fail,
silent,
):
"""
Output the final HTML of the specified page
Expand Down Expand Up @@ -802,9 +813,11 @@ def html(
sys.stdout.write(html)
else:
open(output, "w").write(html)
click.echo(
"HTML snapshot of '{}' written to '{}'".format(url, output), err=True
)
if not silent:
click.echo(
"HTML snapshot of '{}' written to '{}'".format(url, output),
err=True,
)

browser_obj.close()

Expand Down Expand Up @@ -899,6 +912,7 @@ def take_shot(
log_console=False,
skip=False,
fail=False,
silent=False,
):
url = shot.get("url") or ""
if not url:
Expand Down Expand Up @@ -1049,7 +1063,8 @@ def on_response(response):
else:
page.screenshot(**screenshot_args)
message = "Screenshot of '{}' written to '{}'".format(url, output)
click.echo(message, err=True)
if not silent:
click.echo(message, err=True)


def _js_selector_javascript(js_selectors, js_selectors_all):
Expand Down

0 comments on commit c2aa351

Please sign in to comment.