diff --git a/src/ridiwise/api/browser_base_client.py b/src/ridiwise/api/browser_base_client.py index 21f6bac..8759471 100644 --- a/src/ridiwise/api/browser_base_client.py +++ b/src/ridiwise/api/browser_base_client.py @@ -14,11 +14,13 @@ def __init__( self, cache_dir: Path, headless: bool = True, + browser_timeout_seconds: int = 10, *args, **kwargs, ): self.cache_dir = cache_dir self.headless = headless + self.browser_timeout_seconds = browser_timeout_seconds self.playwright = None self.browser = None @@ -37,6 +39,8 @@ def __enter__(self): except FileNotFoundError: self.browser_context = self.browser.new_context() + self.browser_context.set_default_timeout(self.browser_timeout_seconds * 1000) + super().__enter__() return self diff --git a/src/ridiwise/api/ridibooks.py b/src/ridiwise/api/ridibooks.py index f509b99..3880334 100644 --- a/src/ridiwise/api/ridibooks.py +++ b/src/ridiwise/api/ridibooks.py @@ -112,7 +112,7 @@ def login(self): page.click('button[type="submit"]') try: - page.wait_for_url('**/myridi', timeout=3000) + page.wait_for_url('**/myridi') self.cache_dir.mkdir(parents=True, exist_ok=True) self.browser_context.storage_state( path=self.cache_dir / self.storage_state_filename diff --git a/src/ridiwise/cmd/common_option.py b/src/ridiwise/cmd/common_option.py index 08c703d..aa50871 100644 --- a/src/ridiwise/cmd/common_option.py +++ b/src/ridiwise/cmd/common_option.py @@ -3,7 +3,7 @@ import typer -from ridiwise.cmd.context import AuthState +from ridiwise.cmd.context import AuthState, ContextState @enum.unique @@ -18,13 +18,17 @@ def check_common_options( user_id: Optional[str], password: Optional[str], headless_mode: bool, + browser_timeout_seconds: int, ): + context: ContextState = ctx.ensure_object(dict) + auth_state: AuthState = { 'auth_method': auth_method, } - ctx.obj['auth'] = auth_state - ctx.obj['headless_mode'] = headless_mode + context['auth'] = auth_state + context['headless_mode'] = headless_mode + context['browser_timeout_seconds'] = browser_timeout_seconds if auth_method == RidiAuthMethod.HEADLESS_BROWSER: if not all([user_id, password]): @@ -56,6 +60,11 @@ def common_params( envvar='HEADLESS_MODE', help='Hide the browser window (headless mode).', ), + browser_timeout_seconds: int = typer.Option( + default=10, + envvar='BROWSER_TIMEOUT_SECONDS', + help='Timeout for browser page loading in seconds.', + ), ): ctx.ensure_object(dict) check_common_options( @@ -64,4 +73,5 @@ def common_params( user_id=user_id, password=password, headless_mode=headless_mode, + browser_timeout_seconds=browser_timeout_seconds, ) diff --git a/src/ridiwise/cmd/context.py b/src/ridiwise/cmd/context.py index aedb569..ca9f6e4 100644 --- a/src/ridiwise/cmd/context.py +++ b/src/ridiwise/cmd/context.py @@ -15,4 +15,6 @@ class ContextState(TypedDict): config_dir: Path cache_dir: Path + # headless browser options headless_mode: bool + browser_timeout_seconds: int diff --git a/src/ridiwise/cmd/sync/main.py b/src/ridiwise/cmd/sync/main.py index b0b0709..b0a2d09 100644 --- a/src/ridiwise/cmd/sync/main.py +++ b/src/ridiwise/cmd/sync/main.py @@ -4,7 +4,7 @@ from ridiwise.api.readwise import ReadwiseClient from ridiwise.api.ridibooks import RidiClient from ridiwise.cmd.common_option import common_params -from ridiwise.cmd.context import AuthState +from ridiwise.cmd.context import ContextState from ridiwise.cmd.utils import with_extra_parameters app = typer.Typer() @@ -33,15 +33,16 @@ def readwise( Sync Ridibooks book notes to Readwise.io. """ - logger = ctx.obj['logger'] - auth_state: AuthState = ctx.obj['auth'] + context: ContextState = ctx.ensure_object(dict) + logger = context['logger'] with ( RidiClient( - user_id=auth_state['user_id'], - password=auth_state['password'], - cache_dir=ctx.obj['cache_dir'], - headless=ctx.obj['headless_mode'], + user_id=context['auth']['user_id'], + password=context['auth']['password'], + cache_dir=context['cache_dir'], + headless=context['headless_mode'], + browser_timeout_seconds=context['browser_timeout_seconds'], ) as ridi_client, ReadwiseClient(token=readwise_token) as readwise_client, ):