Skip to content

Commit feaefdf

Browse files
committed
Experimental proxy support, WIP
1 parent c64895e commit feaefdf

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

shot_scraper/cli.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ def http_auth_options(fn):
8383
return fn
8484

8585

86+
def proxy_options(fn):
87+
click.option("--proxy-server", help="Server URL for an HTTP proxy")(fn)
88+
click.option("--proxy-username", help="Username for HTTP proxy")(fn)
89+
click.option("--proxy-password", help="Password for HTTP proxy")(fn)
90+
return fn
91+
92+
8693
def skip_or_fail(response, skip, fail):
8794
if skip and fail:
8895
raise click.ClickException("--skip and --fail cannot be used together")
@@ -245,6 +252,7 @@ def cli():
245252
@bypass_csp_option
246253
@silent_option
247254
@http_auth_options
255+
@proxy_options
248256
def shot(
249257
url,
250258
auth,
@@ -278,6 +286,9 @@ def shot(
278286
silent,
279287
auth_username,
280288
auth_password,
289+
proxy_server,
290+
proxy_username,
291+
proxy_password,
281292
):
282293
"""
283294
Take a single screenshot of a page or portion of a page.
@@ -345,6 +356,9 @@ def shot(
345356
bypass_csp=bypass_csp,
346357
auth_username=auth_username,
347358
auth_password=auth_password,
359+
proxy_server=proxy_server,
360+
proxy_username=proxy_username,
361+
proxy_password=proxy_password,
348362
)
349363
if interactive or devtools:
350364
use_existing_page = True
@@ -401,6 +415,9 @@ def _browser_context(
401415
auth_username=None,
402416
auth_password=None,
403417
record_har_path=None,
418+
proxy_server=None,
419+
proxy_username=None,
420+
proxy_password=None,
404421
):
405422
browser_kwargs = dict(
406423
headless=not interactive, devtools=devtools, args=browser_args
@@ -432,6 +449,16 @@ def _browser_context(
432449
}
433450
if record_har_path:
434451
context_args["record_har_path"] = record_har_path
452+
if proxy_server:
453+
proxy = {"server": proxy_server}
454+
if proxy_username:
455+
proxy["username"] = proxy_username
456+
if proxy_password:
457+
proxy["password"] = proxy_password
458+
context_args["proxy"] = proxy
459+
# Not sure why I needed this, figure that out!
460+
context_args["ignore_https_errors"] = True
461+
435462
context = browser_obj.new_context(**context_args)
436463
if timeout:
437464
context.set_default_timeout(timeout)
@@ -737,6 +764,7 @@ def accessibility(
737764
@skip_fail_options
738765
@bypass_csp_option
739766
@http_auth_options
767+
@proxy_options
740768
def har(
741769
url,
742770
zip_,
@@ -752,6 +780,9 @@ def har(
752780
bypass_csp,
753781
auth_username,
754782
auth_password,
783+
proxy_server,
784+
proxy_username,
785+
proxy_password,
755786
):
756787
"""
757788
Record a HAR file for the specified page
@@ -781,6 +812,9 @@ def har(
781812
auth_username=auth_username,
782813
auth_password=auth_password,
783814
record_har_path=str(output),
815+
proxy_server=proxy_server,
816+
proxy_username=proxy_username,
817+
proxy_password=proxy_password,
784818
)
785819
page = context.new_page()
786820
if log_console:

0 commit comments

Comments
 (0)