summaryrefslogtreecommitdiffstats
path: root/g4f/requests
diff options
context:
space:
mode:
authorHeiner Lohaus <hlohaus@users.noreply.github.com>2024-03-15 11:46:06 +0100
committerHeiner Lohaus <hlohaus@users.noreply.github.com>2024-03-15 11:46:06 +0100
commit8cc6000ffbf4e12bf6c1d5e5878d376e36857ec0 (patch)
treeea190c7017d8e8982e63a502da0a828d5ba42f94 /g4f/requests
parentAdd export / import conversations (diff)
downloadgpt4free-8cc6000ffbf4e12bf6c1d5e5878d376e36857ec0.tar
gpt4free-8cc6000ffbf4e12bf6c1d5e5878d376e36857ec0.tar.gz
gpt4free-8cc6000ffbf4e12bf6c1d5e5878d376e36857ec0.tar.bz2
gpt4free-8cc6000ffbf4e12bf6c1d5e5878d376e36857ec0.tar.lz
gpt4free-8cc6000ffbf4e12bf6c1d5e5878d376e36857ec0.tar.xz
gpt4free-8cc6000ffbf4e12bf6c1d5e5878d376e36857ec0.tar.zst
gpt4free-8cc6000ffbf4e12bf6c1d5e5878d376e36857ec0.zip
Diffstat (limited to '')
-rw-r--r--g4f/requests/__init__.py31
-rw-r--r--g4f/requests/aiohttp.py8
-rw-r--r--g4f/requests/defaults.py10
3 files changed, 46 insertions, 3 deletions
diff --git a/g4f/requests/__init__.py b/g4f/requests/__init__.py
index 3bb68e9c..cfc6af42 100644
--- a/g4f/requests/__init__.py
+++ b/g4f/requests/__init__.py
@@ -12,11 +12,40 @@ except ImportError:
from typing import Type as Session, Type as Response
from .aiohttp import StreamResponse, StreamSession
has_curl_cffi = False
+try:
+ import webview
+ import asyncio
+ has_webview = True
+except ImportError:
+ has_webview = False
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 .defaults import DEFAULT_HEADERS, WEBVIEW_HAEDERS
+
+async def get_args_from_webview(url: str):
+ 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,
diff --git a/g4f/requests/aiohttp.py b/g4f/requests/aiohttp.py
index 6979b20a..505086a1 100644
--- a/g4f/requests/aiohttp.py
+++ b/g4f/requests/aiohttp.py
@@ -1,6 +1,6 @@
from __future__ import annotations
-from aiohttp import ClientSession, ClientResponse, ClientTimeout, BaseConnector
+from aiohttp import ClientSession, ClientResponse, ClientTimeout, BaseConnector, FormData
from typing import AsyncIterator, Any, Optional
from .defaults import DEFAULT_HEADERS
@@ -43,4 +43,8 @@ def get_connector(connector: BaseConnector = None, proxy: str = None, rdns: bool
connector = ProxyConnector.from_url(proxy, rdns=rdns)
except ImportError:
raise MissingRequirementsError('Install "aiohttp_socks" package for proxy support')
- return connector \ No newline at end of file
+ return connector
+
+class CurlMime(FormData):
+ def addpart(self, name: str, content_type: str = None, filename: str = None, data: bytes = None):
+ self.add_field(name, data, content_type=content_type, filename=filename) \ No newline at end of file
diff --git a/g4f/requests/defaults.py b/g4f/requests/defaults.py
index 2457f046..3183eb5a 100644
--- a/g4f/requests/defaults.py
+++ b/g4f/requests/defaults.py
@@ -16,4 +16,14 @@ DEFAULT_HEADERS = {
"referer": "",
"accept-encoding": "gzip, deflate, br",
"accept-language": "en-US",
+}
+WEBVIEW_HAEDERS = {
+ "Accept": "*/*",
+ "Accept-Encoding": "gzip, deflate, br",
+ "Accept-Language": "",
+ "Referer": "",
+ "Sec-Fetch-Dest": "empty",
+ "Sec-Fetch-Mode": "cors",
+ "Sec-Fetch-Site": "same-origin",
+ "User-Agent": "",
} \ No newline at end of file