From 993c9498c4276836864b01f66f5d08676a994520 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Thu, 14 Mar 2024 13:53:57 +0100 Subject: Add webview support to OpenaiChat --- g4f/requests/__init__.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'g4f/requests') diff --git a/g4f/requests/__init__.py b/g4f/requests/__init__.py index f2946fc1..3bb68e9c 100644 --- a/g4f/requests/__init__.py +++ b/g4f/requests/__init__.py @@ -81,22 +81,25 @@ def get_session_from_browser(url: str, webdriver: WebDriver = None, proxy: str = impersonate="chrome" ) +def is_cloudflare(text: str): + return '
' in text or "Just a moment..." in text + async def raise_for_status_async(response: Union[StreamResponse, ClientResponse], message: str = None): if response.status in (429, 402): raise RateLimitError(f"Response {response.status}: Rate limit reached") message = await response.text() if not response.ok and message is None else message - if response.status == 403 and "Just a moment..." in message: + if response.status == 403 and is_cloudflare(message): raise ResponseStatusError(f"Response {response.status}: Cloudflare detected") elif not response.ok: raise ResponseStatusError(f"Response {response.status}: {message}") def raise_for_status(response: Union[StreamResponse, ClientResponse, Response, RequestsResponse], message: str = None): - if isinstance(response, StreamSession) or isinstance(response, ClientResponse): + if hasattr(response, "status"): return raise_for_status_async(response, message) if response.status_code in (429, 402): raise RateLimitError(f"Response {response.status_code}: Rate limit reached") - elif response.status_code == 403 and "Just a moment..." in response.text: + elif response.status_code == 403 and is_cloudflare(response.text): raise ResponseStatusError(f"Response {response.status_code}: Cloudflare detected") elif not response.ok: raise ResponseStatusError(f"Response {response.status_code}: {response.text if message is None else message}") \ No newline at end of file -- cgit v1.2.3