simonw / shot-scraper

A command-line utility for taking automated screenshots of websites
https://shot-scraper.datasette.io
Apache License 2.0
1.67k stars 73 forks source link

Add --proxy parameter to CLI interface #123

Open kirillklimenko opened 11 months ago

kirillklimenko commented 11 months ago

Please add --proxy parameter to CLI interface (https://playwright.dev/python/docs/network#http-proxy):

cli.py

def proxy_option(fn):
    click.option("--proxy", help="HTTP Proxy to use")(fn)
    return fn

# other options
@proxy_option
def shot(...):
    ...

def _browser_context(
    p,
    auth,
    interactive=False,
    devtools=False,
    retina=False,
    browser="chromium",
    user_agent=None,
    proxy=None,
    timeout=None,
    reduced_motion=False,
):
    browser_kwargs = dict(headless=not interactive, devtools=devtools, proxy=proxy)
    if browser == "chromium":
        browser_obj = p.chromium.launch(**browser_kwargs)
    elif browser == "firefox":
        browser_obj = p.firefox.launch(**browser_kwargs)
    elif browser == "webkit":
        browser_obj = p.webkit.launch(**browser_kwargs)
    else:
        browser_kwargs["channel"] = browser
        browser_obj = p.chromium.launch(**browser_kwargs)
    context_args = {}
    if auth:
        context_args["storage_state"] = json.load(auth)
    if retina:
        context_args["device_scale_factor"] = 2
    if reduced_motion:
        context_args["reduced_motion"] = "reduce"
    if user_agent is not None:
        context_args["user_agent"] = user_agent
    if proxy is not None:
        context_args["proxy"] = proxy
    context = browser_obj.new_context(**context_args)
    if timeout:
        context.set_default_timeout(timeout)
    return context, browser_obj
simonw commented 11 months ago

I'm happy to add this but I'm not sure how best to test this. Do you have any guidance on an easy way to test that this proxy option is working? Maybe an existing proxy I can run this through, or a proxy server that's easy to get running on macOS?

kirillklimenko commented 11 months ago

Sure, you can find free proxies on https://free-proxy-list.net/ and test the parameter with them.

manugarri commented 2 weeks ago

@simonw is there any plan to add this? seems like an easy PR that would add a ton of functionality

simonw commented 2 weeks ago

I’d accept a PR for this!

On Sat, Sep 14, 2024 at 21:14 Manuel Garrido @.***> wrote:

@simonw https://github.com/simonw is there any plan to add this? seems like an easy PR that would add a ton of functionality

— Reply to this email directly, view it on GitHub https://github.com/simonw/shot-scraper/issues/123#issuecomment-2351136236, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAACK7ZGW3DPL6ZDTIJ7YNDZWSKLXAVCNFSM6AAAAABOHECW2OVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGNJRGEZTMMRTGY . You are receiving this because you were mentioned.Message ID: @.***>