refactor(fetchers): rename internal api

To make it easier to use to use sessions outside `with` context
This commit is contained in:
Karim shoair
2025-12-17 01:27:40 +02:00
parent ae719a9d54
commit b507e4d4a0
3 changed files with 61 additions and 49 deletions
+4 -4
View File
@@ -40,7 +40,7 @@ class SyncSession:
self.context: BrowserContext | Any = None self.context: BrowserContext | Any = None
self._closed = False self._closed = False
def __create__(self): def start(self):
pass pass
def close(self): # pragma: no cover def close(self): # pragma: no cover
@@ -59,7 +59,7 @@ class SyncSession:
self._closed = True self._closed = True
def __enter__(self): def __enter__(self):
self.__create__() self.start()
return self return self
def __exit__(self, exc_type, exc_val, exc_tb): def __exit__(self, exc_type, exc_val, exc_tb):
@@ -145,7 +145,7 @@ class AsyncSession:
self._closed = False self._closed = False
self._lock = Lock() self._lock = Lock()
async def __create__(self): async def start(self):
pass pass
async def close(self): async def close(self):
@@ -164,7 +164,7 @@ class AsyncSession:
self._closed = True self._closed = True
async def __aenter__(self): async def __aenter__(self):
await self.__create__() await self.start()
return self return self
async def __aexit__(self, exc_type, exc_val, exc_tb): async def __aexit__(self, exc_type, exc_val, exc_tb):
+8 -2
View File
@@ -102,8 +102,9 @@ class StealthySession(StealthySessionMixin, SyncSession):
self.__validate__(**kwargs) self.__validate__(**kwargs)
super().__init__(max_pages=self._max_pages) super().__init__(max_pages=self._max_pages)
def __create__(self): def start(self):
"""Create a browser for this instance and context.""" """Create a browser for this instance and context."""
if not self.playwright:
self.playwright = sync_playwright().start() self.playwright = sync_playwright().start()
self.context = self.playwright.firefox.launch_persistent_context(**self.launch_options) self.context = self.playwright.firefox.launch_persistent_context(**self.launch_options)
@@ -112,6 +113,8 @@ class StealthySession(StealthySessionMixin, SyncSession):
if self._cookies: # pragma: no cover if self._cookies: # pragma: no cover
self.context.add_cookies(self._cookies) self.context.add_cookies(self._cookies)
else:
raise RuntimeError("Session has been already started")
def _cloudflare_solver(self, page: Page) -> None: # pragma: no cover def _cloudflare_solver(self, page: Page) -> None: # pragma: no cover
"""Solve the cloudflare challenge displayed on the playwright page passed """Solve the cloudflare challenge displayed on the playwright page passed
@@ -299,8 +302,9 @@ class AsyncStealthySession(StealthySessionMixin, AsyncSession):
self.__validate__(**kwargs) self.__validate__(**kwargs)
super().__init__(max_pages=self._max_pages) super().__init__(max_pages=self._max_pages)
async def __create__(self): async def start(self):
"""Create a browser for this instance and context.""" """Create a browser for this instance and context."""
if not self.playwright:
self.playwright: AsyncPlaywright = await async_playwright().start() self.playwright: AsyncPlaywright = await async_playwright().start()
self.context: AsyncBrowserContext = await self.playwright.firefox.launch_persistent_context( self.context: AsyncBrowserContext = await self.playwright.firefox.launch_persistent_context(
**self.launch_options **self.launch_options
@@ -311,6 +315,8 @@ class AsyncStealthySession(StealthySessionMixin, AsyncSession):
if self._cookies: if self._cookies:
await self.context.add_cookies(self._cookies) # pyright: ignore [reportArgumentType] await self.context.add_cookies(self._cookies) # pyright: ignore [reportArgumentType]
else:
raise RuntimeError("Session has been already started")
async def _cloudflare_solver(self, page: async_Page): # pragma: no cover async def _cloudflare_solver(self, page: async_Page): # pragma: no cover
"""Solve the cloudflare challenge displayed on the playwright page passed. The async version """Solve the cloudflare challenge displayed on the playwright page passed. The async version
+8 -2
View File
@@ -95,8 +95,9 @@ class DynamicSession(DynamicSessionMixin, SyncSession):
self.__validate__(**kwargs) self.__validate__(**kwargs)
super().__init__(max_pages=self._max_pages) super().__init__(max_pages=self._max_pages)
def __create__(self): def start(self):
"""Create a browser for this instance and context.""" """Create a browser for this instance and context."""
if not self.playwright:
sync_context = sync_patchright if self._stealth else sync_playwright sync_context = sync_patchright if self._stealth else sync_playwright
self.playwright: Playwright = sync_context().start() # pyright: ignore [reportAttributeAccessIssue] self.playwright: Playwright = sync_context().start() # pyright: ignore [reportAttributeAccessIssue]
@@ -113,6 +114,8 @@ class DynamicSession(DynamicSessionMixin, SyncSession):
if self._cookies: # pragma: no cover if self._cookies: # pragma: no cover
self.context.add_cookies(self._cookies) self.context.add_cookies(self._cookies)
else:
raise RuntimeError("Session has been already started")
def fetch(self, url: str, **kwargs: Unpack[PlaywrightFetchParams]) -> Response: def fetch(self, url: str, **kwargs: Unpack[PlaywrightFetchParams]) -> Response:
"""Opens up the browser and do your request based on your chosen options. """Opens up the browser and do your request based on your chosen options.
@@ -227,8 +230,9 @@ class AsyncDynamicSession(DynamicSessionMixin, AsyncSession):
self.__validate__(**kwargs) self.__validate__(**kwargs)
super().__init__(max_pages=self._max_pages) super().__init__(max_pages=self._max_pages)
async def __create__(self): async def start(self):
"""Create a browser for this instance and context.""" """Create a browser for this instance and context."""
if not self.playwright:
async_context = async_patchright if self._stealth else async_playwright async_context = async_patchright if self._stealth else async_playwright
self.playwright: AsyncPlaywright = await async_context().start() # pyright: ignore [reportAttributeAccessIssue] self.playwright: AsyncPlaywright = await async_context().start() # pyright: ignore [reportAttributeAccessIssue]
@@ -246,6 +250,8 @@ class AsyncDynamicSession(DynamicSessionMixin, AsyncSession):
if self._cookies: if self._cookies:
await self.context.add_cookies(self._cookies) # pyright: ignore await self.context.add_cookies(self._cookies) # pyright: ignore
else:
raise RuntimeError("Session has been already started")
async def fetch(self, url: str, **kwargs: Unpack[PlaywrightFetchParams]) -> Response: async def fetch(self, url: str, **kwargs: Unpack[PlaywrightFetchParams]) -> Response:
"""Opens up the browser and do your request based on your chosen options. """Opens up the browser and do your request based on your chosen options.