summaryrefslogtreecommitdiffstats
path: root/g4f/requests/__init__.py
diff options
context:
space:
mode:
authorH Lohaus <hlohaus@users.noreply.github.com>2024-02-23 03:08:30 +0100
committerGitHub <noreply@github.com>2024-02-23 03:08:30 +0100
commitcf87d467c927d4b93916a42e7139f4e801172d62 (patch)
tree175b39f8486ce72be00a40cc345834abe859c84c /g4f/requests/__init__.py
parent~ (diff)
parentFix unittests, use Union typing (diff)
downloadgpt4free-cf87d467c927d4b93916a42e7139f4e801172d62.tar
gpt4free-cf87d467c927d4b93916a42e7139f4e801172d62.tar.gz
gpt4free-cf87d467c927d4b93916a42e7139f4e801172d62.tar.bz2
gpt4free-cf87d467c927d4b93916a42e7139f4e801172d62.tar.lz
gpt4free-cf87d467c927d4b93916a42e7139f4e801172d62.tar.xz
gpt4free-cf87d467c927d4b93916a42e7139f4e801172d62.tar.zst
gpt4free-cf87d467c927d4b93916a42e7139f4e801172d62.zip
Diffstat (limited to 'g4f/requests/__init__.py')
-rw-r--r--g4f/requests/__init__.py56
1 files changed, 56 insertions, 0 deletions
diff --git a/g4f/requests/__init__.py b/g4f/requests/__init__.py
new file mode 100644
index 00000000..d278ffaf
--- /dev/null
+++ b/g4f/requests/__init__.py
@@ -0,0 +1,56 @@
+from __future__ import annotations
+
+from urllib.parse import urlparse
+
+try:
+ from curl_cffi.requests import Session
+ from .curl_cffi import StreamResponse, StreamSession
+ has_curl_cffi = True
+except ImportError:
+ from typing import Type as Session
+ from .aiohttp import StreamResponse, StreamSession
+ has_curl_cffi = False
+
+from ..webdriver import WebDriver, WebDriverSession, bypass_cloudflare, get_driver_cookies
+from ..errors import MissingRequirementsError
+from .defaults import DEFAULT_HEADERS
+
+def get_args_from_browser(url: str, webdriver: WebDriver = None, proxy: str = None, timeout: int = 120) -> dict:
+ """
+ Create a Session object using a WebDriver to handle cookies and headers.
+
+ Args:
+ url (str): The URL to navigate to using the WebDriver.
+ webdriver (WebDriver, optional): The WebDriver instance to use.
+ proxy (str, optional): Proxy server to use for the Session.
+ timeout (int, optional): Timeout in seconds for the WebDriver.
+
+ Returns:
+ Session: A Session object configured with cookies and headers from the WebDriver.
+ """
+ with WebDriverSession(webdriver, "", proxy=proxy, virtual_display=False) as driver:
+ bypass_cloudflare(driver, url, timeout)
+ cookies = get_driver_cookies(driver)
+ user_agent = driver.execute_script("return navigator.userAgent")
+ parse = urlparse(url)
+ return {
+ 'cookies': cookies,
+ 'headers': {
+ **DEFAULT_HEADERS,
+ 'Authority': parse.netloc,
+ 'Origin': f'{parse.scheme}://{parse.netloc}',
+ 'Referer': url,
+ 'User-Agent': user_agent,
+ },
+ }
+
+def get_session_from_browser(url: str, webdriver: WebDriver = None, proxy: str = None, timeout: int = 120) -> Session:
+ if not has_curl_cffi:
+ raise MissingRequirementsError('Install "curl_cffi" package')
+ args = get_args_from_browser(url, webdriver, proxy, timeout)
+ return Session(
+ **args,
+ proxies={"https": proxy, "http": proxy},
+ timeout=timeout,
+ impersonate="chrome110"
+ ) \ No newline at end of file