refactor: internal API changes to be easily used as indicators for spiders

This commit is contained in:
Karim shoair
2026-01-08 00:52:06 +02:00
parent de79fe80bb
commit 6a73f9dcd7
4 changed files with 34 additions and 26 deletions
+6 -6
View File
@@ -37,14 +37,14 @@ class SyncSession:
self._max_wait_for_page = 60
self.playwright: Playwright | Any = None
self.context: BrowserContext | Any = None
self._closed = False
self._is_alive = False
def start(self):
pass
def close(self): # pragma: no cover
"""Close all resources"""
if self._closed:
if not self._is_alive:
return
if self.context:
@@ -55,7 +55,7 @@ class SyncSession:
self.playwright.stop()
self.playwright = None # pyright: ignore
self._closed = True
self._is_alive = False
def __enter__(self):
self.start()
@@ -137,7 +137,7 @@ class AsyncSession:
self._max_wait_for_page = 60
self.playwright: AsyncPlaywright | Any = None
self.context: AsyncBrowserContext | Any = None
self._closed = False
self._is_alive = False
self._lock = Lock()
async def start(self):
@@ -145,7 +145,7 @@ class AsyncSession:
async def close(self):
"""Close all resources"""
if self._closed: # pragma: no cover
if not self._is_alive: # pragma: no cover
return
if self.context:
@@ -156,7 +156,7 @@ class AsyncSession:
await self.playwright.stop()
self.playwright = None # pyright: ignore
self._closed = True
self._is_alive = False
async def __aenter__(self):
await self.start()
+6 -3
View File
@@ -31,7 +31,6 @@ class DynamicSession(SyncSession, DynamicSessionMixin):
"_max_wait_for_page",
"playwright",
"context",
"_closed",
)
def __init__(self, **kwargs: Unpack[PlaywrightSession]):
@@ -82,6 +81,8 @@ class DynamicSession(SyncSession, DynamicSessionMixin):
if self._config.cookies: # pragma: no cover
self.context.add_cookies(self._config.cookies)
self._is_alive = True
else:
raise RuntimeError("Session has been already started")
@@ -104,7 +105,7 @@ class DynamicSession(SyncSession, DynamicSessionMixin):
:return: A `Response` object.
"""
params = _validate(kwargs, self, PlaywrightConfig)
if self._closed: # pragma: no cover
if not self._is_alive: # pragma: no cover
raise RuntimeError("Context manager has been closed")
referer = (
@@ -211,6 +212,8 @@ class AsyncDynamicSession(AsyncSession, DynamicSessionMixin):
if self._config.cookies:
await self.context.add_cookies(self._config.cookies) # pyright: ignore
self._is_alive = True
else:
raise RuntimeError("Session has been already started")
@@ -234,7 +237,7 @@ class AsyncDynamicSession(AsyncSession, DynamicSessionMixin):
"""
params = _validate(kwargs, self, PlaywrightConfig)
if self._closed: # pragma: no cover
if not self._is_alive: # pragma: no cover
raise RuntimeError("Context manager has been closed")
referer = (
+2 -3
View File
@@ -39,7 +39,6 @@ class StealthySession(SyncSession, StealthySessionMixin):
"_max_wait_for_page",
"playwright",
"context",
"_closed",
)
def __init__(self, **kwargs: Unpack[StealthSession]):
@@ -191,7 +190,7 @@ class StealthySession(SyncSession, StealthySessionMixin):
:return: A `Response` object.
"""
params = _validate(kwargs, self, StealthConfig)
if self._closed: # pragma: no cover
if not self._is_alive: # pragma: no cover
raise RuntimeError("Context manager has been closed")
referer = (
@@ -404,7 +403,7 @@ class AsyncStealthySession(AsyncSession, StealthySessionMixin):
"""
params = _validate(kwargs, self, StealthConfig)
if self._closed: # pragma: no cover
if not self._is_alive: # pragma: no cover
raise RuntimeError("Context manager has been closed")
referer = (