summaryrefslogtreecommitdiffstats
path: root/g4f/requests/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'g4f/requests/__init__.py')
-rw-r--r--g4f/requests/__init__.py64
1 files changed, 35 insertions, 29 deletions
diff --git a/g4f/requests/__init__.py b/g4f/requests/__init__.py
index f2946fc1..e65de99a 100644
--- a/g4f/requests/__init__.py
+++ b/g4f/requests/__init__.py
@@ -1,22 +1,48 @@
from __future__ import annotations
-from typing import Union
-from aiohttp import ClientResponse
-from requests import Response as RequestsResponse
-
try:
from curl_cffi.requests import Session, Response
- from .curl_cffi import StreamResponse, StreamSession
+ from .curl_cffi import StreamResponse, StreamSession, FormData
has_curl_cffi = True
except ImportError:
from typing import Type as Session, Type as Response
- from .aiohttp import StreamResponse, StreamSession
+ from .aiohttp import StreamResponse, StreamSession, FormData
has_curl_cffi = False
+try:
+ import webview
+ import asyncio
+ has_webview = True
+except ImportError:
+ has_webview = False
+from .raise_for_status import raise_for_status
from ..webdriver import WebDriver, WebDriverSession
from ..webdriver import bypass_cloudflare, get_driver_cookies
-from ..errors import MissingRequirementsError, RateLimitError, ResponseStatusError
-from .defaults import DEFAULT_HEADERS
+from ..errors import MissingRequirementsError
+from .defaults import DEFAULT_HEADERS, WEBVIEW_HAEDERS
+
+async def get_args_from_webview(url: str) -> dict:
+ if not has_webview:
+ raise MissingRequirementsError('Install "webview" package')
+ window = webview.create_window("", url, hidden=True)
+ await asyncio.sleep(2)
+ body = None
+ while body is None:
+ try:
+ await asyncio.sleep(1)
+ body = window.dom.get_element("body:not(.no-js)")
+ except:
+ ...
+ headers = {
+ **WEBVIEW_HAEDERS,
+ "User-Agent": window.evaluate_js("this.navigator.userAgent"),
+ "Accept-Language": window.evaluate_js("this.navigator.language"),
+ "Referer": window.real_url
+ }
+ cookies = [list(*cookie.items()) for cookie in window.get_cookies()]
+ cookies = dict([(name, cookie.value) for name, cookie in cookies])
+ window.destroy()
+ return {"headers": headers, "cookies": cookies}
def get_args_from_browser(
url: str,
@@ -79,24 +105,4 @@ def get_session_from_browser(url: str, webdriver: WebDriver = None, proxy: str =
proxies={"https": proxy, "http": proxy},
timeout=timeout,
impersonate="chrome"
- )
-
-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:
- 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):
- 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:
- 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
+ ) \ No newline at end of file