Skip to content

Commit

Permalink
feat: make browser timeout configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
bskim45 committed Aug 5, 2024
1 parent 1b5b557 commit ad5cdb0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/ridiwise/api/browser_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/ridiwise/api/ridibooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 13 additions & 3 deletions src/ridiwise/cmd/common_option.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import typer

from ridiwise.cmd.context import AuthState
from ridiwise.cmd.context import AuthState, ContextState


@enum.unique
Expand All @@ -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]):
Expand Down Expand Up @@ -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(
Expand All @@ -64,4 +73,5 @@ def common_params(
user_id=user_id,
password=password,
headless_mode=headless_mode,
browser_timeout_seconds=browser_timeout_seconds,
)
2 changes: 2 additions & 0 deletions src/ridiwise/cmd/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ class ContextState(TypedDict):
config_dir: Path
cache_dir: Path

# headless browser options
headless_mode: bool
browser_timeout_seconds: int
15 changes: 8 additions & 7 deletions src/ridiwise/cmd/sync/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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,
):
Expand Down

0 comments on commit ad5cdb0

Please sign in to comment.