Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a device option to specify device name for convenience #129

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions shot_scraper/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ def cli():
type=int,
help="Height of browser window and shot - defaults to the full height of the page",
)
@click.option(
"-d",
"--device",
help="Device name. Specify device name will override width height and retina option.",
)
@click.option(
"device_ratio",
"--device-ratio",
help="Ratio string(i.e 16:9), use with --device option."
)
@click.option(
"-o",
"--output",
Expand Down Expand Up @@ -207,6 +217,8 @@ def shot(
output,
width,
height,
device,
device_ratio,
selectors,
selectors_all,
js_selectors,
Expand Down Expand Up @@ -269,6 +281,7 @@ def shot(
"javascript": javascript,
"width": width,
"height": height,
"device": device,
"quality": quality,
"wait": wait,
"wait_for": wait_for,
Expand All @@ -291,6 +304,8 @@ def shot(
timeout=timeout,
reduced_motion=reduced_motion,
bypass_csp=bypass_csp,
device=device,
device_ratio=device_ratio,
)
if interactive or devtools:
use_existing_page = True
Expand Down Expand Up @@ -341,6 +356,8 @@ def _browser_context(
timeout=None,
reduced_motion=False,
bypass_csp=False,
device=None,
device_ratio=None,
):
browser_kwargs = dict(headless=not interactive, devtools=devtools)
if browser == "chromium":
Expand All @@ -363,6 +380,14 @@ def _browser_context(
context_args["user_agent"] = user_agent
if bypass_csp:
context_args["bypass_csp"] = bypass_csp
if device:
if device not in p.devices:
raise click.BadParameter("Invalid device name, please refer to playwright.devices")
device_spec = {**p.devices[device]}
if device_ratio:
rw, rh = map(int, device_ratio.split(":"))
device_spec["viewport"]["height"] = int(device_spec["viewport"]["width"] * rh / rw)
context_args.update(device_spec)
context = browser_obj.new_context(**context_args)
if timeout:
context.set_default_timeout(timeout)
Expand All @@ -378,6 +403,7 @@ def _browser_context(
help="Path to JSON authentication context file",
)
@click.option("--retina", is_flag=True, help="Use device scale factor of 2")
@click.option("--device", help="Device name. Specify device name will override retina option")
@click.option(
"--timeout",
type=int,
Expand Down Expand Up @@ -412,6 +438,7 @@ def multi(
config,
auth,
retina,
device,
timeout,
fail_on_error,
noclobber,
Expand Down Expand Up @@ -453,6 +480,7 @@ def multi(
user_agent=user_agent,
timeout=timeout,
reduced_motion=reduced_motion,
device=device
)
for shot in shots:
if (
Expand Down Expand Up @@ -1008,9 +1036,10 @@ def on_response(response):
if log_console:
page.on("console", console_log)

viewport = {}
full_page = True
if shot.get("width") or shot.get("height"):
if shot.get("device"):
full_page = False
elif shot.get("width") or shot.get("height"):
viewport = {
"width": shot.get("width") or 1280,
"height": shot.get("height") or 720,
Expand Down
2 changes: 1 addition & 1 deletion shot_scraper/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def filename_for_url(url, ext=None, file_exists=file_exists_never):
ext = ext or "png"
bits = urllib.parse.urlparse(url)
filename = (bits.netloc + bits.path).replace(".", "-").replace("/", "-").rstrip("-")
# Remove any characters outside of the allowed range
# Remove any characters outside the allowed range
base_filename = disallowed_re.sub("", filename).lstrip("-")
filename = base_filename + "." + ext
suffix = 0
Expand Down