From 04f09c3d5692474b49f0e651e26f27a7bf7a3c9c Mon Sep 17 00:00:00 2001 From: Sandeep Parajuli Date: Thu, 18 Jan 2024 13:13:25 +0545 Subject: [PATCH] browser instance --- Pipfile.lock | 20 ++++++++++++++++++++ shot_scraper/cli.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 Pipfile.lock diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 0000000..eb6410c --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,20 @@ +{ + "_meta": { + "hash": { + "sha256": "fedbd2ab7afd84cf16f128af0619749267b62277b4cb6989ef16d4bef6e4eef2" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.10" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": {}, + "develop": {} +} diff --git a/shot_scraper/cli.py b/shot_scraper/cli.py index faf45e5..936f931 100644 --- a/shot_scraper/cli.py +++ b/shot_scraper/cli.py @@ -16,6 +16,8 @@ BROWSERS = ("chromium", "firefox", "webkit", "chrome", "chrome-beta") + + def console_log(msg): click.echo(msg, err=True) @@ -230,6 +232,7 @@ def shot( fail, bypass_csp, silent, + browser_instance=None ): """ Take a single screenshot of a page or portion of a page. @@ -278,6 +281,35 @@ def shot( "retina": retina, } interactive = interactive or devtools + + if browser_instance: + try: + if output == "-": + shot = take_shot( + context, + shot, + return_bytes=True, + use_existing_page=use_existing_page, + log_requests=log_requests, + log_console=log_console, + silent=silent, + ) + sys.stdout.buffer.write(shot) + else: + shot["output"] = str(output) + shot = take_shot( + context, + shot, + use_existing_page=use_existing_page, + log_requests=log_requests, + log_console=log_console, + skip=skip, + fail=fail, + silent=silent, + ) + except TimeoutError as e: + raise click.ClickException(str(e)) + with sync_playwright() as p: use_existing_page = False context, browser_obj = _browser_context(