From 8cc6000ffbf4e12bf6c1d5e5878d376e36857ec0 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Fri, 15 Mar 2024 11:46:06 +0100 Subject: Add Pyinstaller support, Use curl_cffi in You provider --- g4f/requests/__init__.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'g4f/requests/__init__.py') 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, -- cgit v1.2.3