Skip to content

Commit ec15d20

Browse files
committed
--skip and --fail for shot and multi, refs #102
1 parent fceddad commit ec15d20

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

docs/multi.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ Options:
144144
--user-agent TEXT User-Agent header to use
145145
--reduced-motion Emulate 'prefers-reduced-motion' media feature
146146
--log-console Write console.log() to stderr
147+
--fail Fail with an error code if a page returns an
148+
HTTP error
149+
--skip Skip pages that return HTTP errors
147150
--help Show this message and exit.
148151
```
149152
<!-- [[[end]]] -->

docs/screenshots.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,9 @@ Options:
301301
Which browser to use
302302
--user-agent TEXT User-Agent header to use
303303
--reduced-motion Emulate 'prefers-reduced-motion' media feature
304+
--fail Fail with an error code if a page returns an
305+
HTTP error
306+
--skip Skip pages that return HTTP errors
304307
--help Show this message and exit.
305308
```
306309
<!-- [[[end]]] -->

shot_scraper/cli.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ def log_console_option(fn):
4343
return fn
4444

4545

46+
def skip_fail_options(fn):
47+
click.option("--skip", is_flag=True, help="Skip pages that return HTTP errors")(fn)
48+
click.option(
49+
"--fail",
50+
is_flag=True,
51+
help="Fail with an error code if a page returns an HTTP error",
52+
)(fn)
53+
return fn
54+
55+
4656
def reduced_motion_option(fn):
4757
click.option(
4858
"--reduced-motion",
@@ -154,6 +164,7 @@ def cli():
154164
@browser_option
155165
@user_agent_option
156166
@reduced_motion_option
167+
@skip_fail_options
157168
def shot(
158169
url,
159170
auth,
@@ -178,6 +189,8 @@ def shot(
178189
browser,
179190
user_agent,
180191
reduced_motion,
192+
skip,
193+
fail,
181194
):
182195
"""
183196
Take a single screenshot of a page or portion of a page.
@@ -266,6 +279,8 @@ def shot(
266279
use_existing_page=use_existing_page,
267280
log_requests=log_requests,
268281
log_console=log_console,
282+
skip=skip,
283+
fail=fail,
269284
)
270285
except TimeoutError as e:
271286
raise click.ClickException(str(e))
@@ -341,6 +356,7 @@ def _browser_context(
341356
@user_agent_option
342357
@reduced_motion_option
343358
@log_console_option
359+
@skip_fail_options
344360
def multi(
345361
config,
346362
auth,
@@ -353,6 +369,8 @@ def multi(
353369
user_agent,
354370
reduced_motion,
355371
log_console,
372+
skip,
373+
fail,
356374
):
357375
"""
358376
Take multiple screenshots, defined by a YAML file
@@ -394,7 +412,9 @@ def multi(
394412
if outputs and shot.get("output") not in outputs:
395413
continue
396414
try:
397-
take_shot(context, shot, log_console=log_console)
415+
take_shot(
416+
context, shot, log_console=log_console, skip=skip, fail=fail,
417+
)
398418
except TimeoutError as e:
399419
if fail_on_error:
400420
raise click.ClickException(str(e))
@@ -813,10 +833,6 @@ def auth(url, context_file, browser, user_agent, devtools, log_console):
813833
pathlib.Path(context_file).chmod(0o600)
814834

815835

816-
class ShotError(Exception):
817-
pass
818-
819-
820836
def _check_and_absolutize(filepath):
821837
path = pathlib.Path(filepath)
822838
if path.exists():
@@ -831,10 +847,15 @@ def take_shot(
831847
use_existing_page=False,
832848
log_requests=None,
833849
log_console=False,
850+
skip=False,
851+
fail=False,
834852
):
835853
url = shot.get("url") or ""
836854
if not url:
837-
raise ShotError("url is required")
855+
raise click.ClickException("url is required")
856+
857+
if skip and fail:
858+
raise click.ClickException("--skip and --fail cannot be used together")
838859

839860
url = url_or_file_path(url, file_exists=_check_and_absolutize)
840861

@@ -901,7 +922,16 @@ def on_response(response):
901922
if shot.get("height"):
902923
full_page = False
903924

904-
page.goto(url)
925+
response = page.goto(url)
926+
# Check if page was a 404 or 500 or other error
927+
if str(response.status)[0] in ("4", "5"):
928+
if skip:
929+
click.echo(
930+
"{} error for {}, skipping".format(response.status, url), err=True
931+
)
932+
return
933+
elif fail:
934+
raise click.ClickException("{} error for {}".format(response.status, url))
905935

906936
if wait:
907937
time.sleep(wait / 1000)

0 commit comments

Comments
 (0)