summaryrefslogtreecommitdiffstats
path: root/g4f/requests/__init__.py
diff options
context:
space:
mode:
authorHeiner Lohaus <hlohaus@users.noreply.github.com>2024-03-14 13:53:57 +0100
committerHeiner Lohaus <hlohaus@users.noreply.github.com>2024-03-14 13:53:57 +0100
commit993c9498c4276836864b01f66f5d08676a994520 (patch)
tree9d0c3e2544669e09aacdcf635fec9095270c08bd /g4f/requests/__init__.py
parentAdd model preselection in gui (diff)
downloadgpt4free-993c9498c4276836864b01f66f5d08676a994520.tar
gpt4free-993c9498c4276836864b01f66f5d08676a994520.tar.gz
gpt4free-993c9498c4276836864b01f66f5d08676a994520.tar.bz2
gpt4free-993c9498c4276836864b01f66f5d08676a994520.tar.lz
gpt4free-993c9498c4276836864b01f66f5d08676a994520.tar.xz
gpt4free-993c9498c4276836864b01f66f5d08676a994520.tar.zst
gpt4free-993c9498c4276836864b01f66f5d08676a994520.zip
Diffstat (limited to '')
-rw-r--r--g4f/requests/__init__.py9
1 files changed, 6 insertions, 3 deletions
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 '<div id="cf-please-wait">' in text or "<title>Just a moment...</title>" 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 "<title>Just a moment...</title>" 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 "<title>Just a moment...</title>" 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