Skip to content

Commit

Permalink
HTTP Basic authentication support, closes #140
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Feb 5, 2024
1 parent 67ef7b6 commit 18148a7
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/accessibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Options:
--fail Fail with an error code if a page returns an HTTP error
--skip Skip pages that return HTTP errors
--bypass-csp Bypass Content-Security-Policy
--auth-password TEXT Password for HTTP Basic authentication
--auth-username TEXT Username for HTTP Basic authentication
--help Show this message and exit.
```
<!-- [[[end]]] -->
2 changes: 2 additions & 0 deletions docs/html.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ Options:
--skip Skip pages that return HTTP errors
--bypass-csp Bypass Content-Security-Policy
--silent Do not output any messages
--auth-password TEXT Password for HTTP Basic authentication
--auth-username TEXT Username for HTTP Basic authentication
--help Show this message and exit.
```
<!-- [[[end]]] -->
2 changes: 2 additions & 0 deletions docs/javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ Options:
HTTP error
--skip Skip pages that return HTTP errors
--bypass-csp Bypass Content-Security-Policy
--auth-password TEXT Password for HTTP Basic authentication
--auth-username TEXT Username for HTTP Basic authentication
--help Show this message and exit.
```
<!-- [[[end]]] -->
2 changes: 2 additions & 0 deletions docs/multi.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ Options:
HTTP error
--skip Skip pages that return HTTP errors
--silent Do not output any messages
--auth-password TEXT Password for HTTP Basic authentication
--auth-username TEXT Username for HTTP Basic authentication
--help Show this message and exit.
```
<!-- [[[end]]] -->
2 changes: 2 additions & 0 deletions docs/pdf.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ Options:
--skip Skip pages that return HTTP errors
--bypass-csp Bypass Content-Security-Policy
--silent Do not output any messages
--auth-password TEXT Password for HTTP Basic authentication
--auth-username TEXT Username for HTTP Basic authentication
--help Show this message and exit.
```
<!-- [[[end]]] -->
2 changes: 2 additions & 0 deletions docs/screenshots.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ Options:
--skip Skip pages that return HTTP errors
--bypass-csp Bypass Content-Security-Policy
--silent Do not output any messages
--auth-password TEXT Password for HTTP Basic authentication
--auth-username TEXT Username for HTTP Basic authentication
--help Show this message and exit.
```
<!-- [[[end]]] -->
64 changes: 61 additions & 3 deletions shot_scraper/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ def bypass_csp_option(fn):
return fn


def http_auth_options(fn):
click.option("--auth-username", help="Username for HTTP Basic authentication")(fn)
click.option("--auth-password", help="Password for HTTP Basic authentication")(fn)
return fn


def skip_or_fail(response, skip, fail):
if skip and fail:
raise click.ClickException("--skip and --fail cannot be used together")
Expand Down Expand Up @@ -201,6 +207,7 @@ def cli():
@skip_fail_options
@bypass_csp_option
@silent_option
@http_auth_options
def shot(
url,
auth,
Expand Down Expand Up @@ -230,6 +237,8 @@ def shot(
fail,
bypass_csp,
silent,
auth_username,
auth_password,
):
"""
Take a single screenshot of a page or portion of a page.
Expand Down Expand Up @@ -291,6 +300,8 @@ def shot(
timeout=timeout,
reduced_motion=reduced_motion,
bypass_csp=bypass_csp,
auth_username=auth_username,
auth_password=auth_password,
)
if interactive or devtools:
use_existing_page = True
Expand Down Expand Up @@ -341,6 +352,8 @@ def _browser_context(
timeout=None,
reduced_motion=False,
bypass_csp=False,
auth_username=None,
auth_password=None,
):
browser_kwargs = dict(headless=not interactive, devtools=devtools)
if browser == "chromium":
Expand All @@ -363,6 +376,11 @@ def _browser_context(
context_args["user_agent"] = user_agent
if bypass_csp:
context_args["bypass_csp"] = bypass_csp
if auth_username and auth_password:
context_args["http_credentials"] = {
"username": auth_username,
"password": auth_password,
}
context = browser_obj.new_context(**context_args)
if timeout:
context.set_default_timeout(timeout)
Expand Down Expand Up @@ -408,6 +426,7 @@ def _browser_context(
@log_console_option
@skip_fail_options
@silent_option
@http_auth_options
def multi(
config,
auth,
Expand All @@ -423,6 +442,8 @@ def multi(
skip,
fail,
silent,
auth_username,
auth_password,
):
"""
Take multiple screenshots, defined by a YAML file
Expand Down Expand Up @@ -453,6 +474,8 @@ def multi(
user_agent=user_agent,
timeout=timeout,
reduced_motion=reduced_motion,
auth_username=auth_username,
auth_password=auth_password,
)
for shot in shots:
if (
Expand Down Expand Up @@ -504,8 +527,19 @@ def multi(
@log_console_option
@skip_fail_options
@bypass_csp_option
@http_auth_options
def accessibility(
url, auth, output, javascript, timeout, log_console, skip, fail, bypass_csp
url,
auth,
output,
javascript,
timeout,
log_console,
skip,
fail,
bypass_csp,
auth_username,
auth_password,
):
"""
Dump the Chromium accessibility tree for the specifed page
Expand All @@ -517,7 +551,12 @@ def accessibility(
url = url_or_file_path(url, _check_and_absolutize)
with sync_playwright() as p:
context, browser_obj = _browser_context(
p, auth, timeout=timeout, bypass_csp=bypass_csp
p,
auth,
timeout=timeout,
bypass_csp=bypass_csp,
auth_username=auth_username,
auth_password=auth_password,
)
page = context.new_page()
if log_console:
Expand Down Expand Up @@ -567,6 +606,7 @@ def accessibility(
@log_console_option
@skip_fail_options
@bypass_csp_option
@http_auth_options
def javascript(
url,
javascript,
Expand All @@ -581,6 +621,8 @@ def javascript(
skip,
fail,
bypass_csp,
auth_username,
auth_password,
):
"""
Execute JavaScript against the page and return the result as JSON
Expand Down Expand Up @@ -618,6 +660,8 @@ def javascript(
user_agent=user_agent,
reduced_motion=reduced_motion,
bypass_csp=bypass_csp,
auth_username=auth_username,
auth_password=auth_password,
)
page = context.new_page()
if log_console:
Expand Down Expand Up @@ -687,6 +731,7 @@ def javascript(
@skip_fail_options
@bypass_csp_option
@silent_option
@http_auth_options
def pdf(
url,
auth,
Expand All @@ -705,6 +750,8 @@ def pdf(
fail,
bypass_csp,
silent,
auth_username,
auth_password,
):
"""
Create a PDF of the specified page
Expand All @@ -725,7 +772,13 @@ def pdf(
if output is None:
output = filename_for_url(url, ext="pdf", file_exists=os.path.exists)
with sync_playwright() as p:
context, browser_obj = _browser_context(p, auth, bypass_csp=bypass_csp)
context, browser_obj = _browser_context(
p,
auth,
bypass_csp=bypass_csp,
auth_username=auth_username,
auth_password=auth_password,
)
page = context.new_page()
if log_console:
page.on("console", console_log)
Expand Down Expand Up @@ -789,6 +842,7 @@ def pdf(
@skip_fail_options
@bypass_csp_option
@silent_option
@http_auth_options
def html(
url,
auth,
Expand All @@ -803,6 +857,8 @@ def html(
fail,
bypass_csp,
silent,
auth_username,
auth_password,
):
"""
Output the final HTML of the specified page
Expand All @@ -825,6 +881,8 @@ def html(
browser=browser,
user_agent=user_agent,
bypass_csp=bypass_csp,
auth_username=auth_username,
auth_password=auth_password,
)
page = context.new_page()
if log_console:
Expand Down

0 comments on commit 18148a7

Please sign in to comment.