misc: add command to open browser for url patterns
This commit is contained in:
parent
e73b68a131
commit
5167152ca8
|
@ -10,6 +10,7 @@ from click import Path as PathArgument
|
|||
from click import argument, group, option, pass_context, pass_obj
|
||||
|
||||
from frontools.config import Config
|
||||
from frontools.misc import open_urls
|
||||
from frontools.screenshot import screenshot
|
||||
from frontools.sources import CachedSource
|
||||
|
||||
|
@ -128,5 +129,17 @@ async def screenshot_cli(
|
|||
)
|
||||
|
||||
|
||||
@main.command(name="open")
|
||||
@argument("url_pattern", type=str, required=True)
|
||||
@_async_command
|
||||
@pass_obj
|
||||
async def open_cli(
|
||||
config: Config,
|
||||
url_pattern: str,
|
||||
) -> None:
|
||||
"""Generate screenshot diffs"""
|
||||
await open_urls(config, url_pattern)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main() # pylint: disable=no-value-for-parameter
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
"""Commands to open pages"""
|
||||
from asyncio import sleep
|
||||
from re import compile as re_compile
|
||||
|
||||
from frontools.config import Config
|
||||
|
||||
|
||||
async def open_urls(config: Config, pattern: str) -> None:
|
||||
re_pattern = re_compile(pattern)
|
||||
|
||||
async with config.source.get_browser(headless=False) as browser:
|
||||
urls = [url for _, url in config.urls if re_pattern.match(url)]
|
||||
|
||||
for url in urls:
|
||||
async with browser.load_page(url) as page:
|
||||
await page.pause()
|
|
@ -8,7 +8,6 @@ from re import Pattern
|
|||
from re import compile as re_compile
|
||||
from shutil import rmtree
|
||||
from typing import AsyncGenerator, Optional, cast
|
||||
from frontools.utils import get_url_slug
|
||||
|
||||
from aiohttp import ClientConnectionError, ClientPayloadError, ClientSession
|
||||
from playwright.async_api import BrowserContext, Error, Page, Route
|
||||
|
@ -16,6 +15,8 @@ from playwright.async_api import TimeoutError as PlaywrightTimeoutError
|
|||
from playwright.async_api import ViewportSize, async_playwright
|
||||
from xdg import xdg_cache_home
|
||||
|
||||
from frontools.utils import get_url_slug
|
||||
|
||||
_LOGGER = getLogger("frontools")
|
||||
|
||||
|
||||
|
@ -69,7 +70,10 @@ class Source(ABC):
|
|||
|
||||
@asynccontextmanager
|
||||
async def get_browser(
|
||||
self, width: Optional[int] = None, height: Optional[int] = None
|
||||
self,
|
||||
width: Optional[int] = None,
|
||||
height: Optional[int] = None,
|
||||
headless: bool = True,
|
||||
) -> AsyncGenerator[Browser, None]:
|
||||
"""Return a Playwright browser that will eventually get files from local cache"""
|
||||
|
||||
|
@ -85,7 +89,7 @@ class Source(ABC):
|
|||
)
|
||||
|
||||
async with async_playwright() as pwright:
|
||||
browser = await pwright.firefox.launch(headless=True)
|
||||
browser = await pwright.firefox.launch(headless=headless)
|
||||
context = await browser.new_context(
|
||||
viewport=viewport, ignore_https_errors=True
|
||||
)
|
||||
|
@ -109,7 +113,9 @@ class CachedSource(Source):
|
|||
|
||||
cache_base = xdg_cache_home() / "frontools"
|
||||
|
||||
def __init__(self, block_urls: list[Pattern[str]], name: str, disabled: bool = False) -> None:
|
||||
def __init__(
|
||||
self, block_urls: list[Pattern[str]], name: str, disabled: bool = False
|
||||
) -> None:
|
||||
super().__init__(block_urls)
|
||||
self._name = name
|
||||
self._disabled = disabled
|
||||
|
|
Loading…
Reference in New Issue