2023-09-25 22:52:29 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-12-02 04:40:07 +00:00
|
|
|
from urllib.parse import urlparse
|
2024-01-14 06:45:41 +00:00
|
|
|
|
2024-01-26 06:54:13 +00:00
|
|
|
try:
|
2024-01-27 01:00:44 +00:00
|
|
|
from curl_cffi.requests import Session
|
2024-02-21 23:16:58 +00:00
|
|
|
from .curl_cffi import StreamResponse, StreamSession
|
2024-01-26 06:54:13 +00:00
|
|
|
has_curl_cffi = True
|
|
|
|
except ImportError:
|
2024-01-29 17:14:46 +00:00
|
|
|
from typing import Type as Session
|
2024-02-21 23:16:58 +00:00
|
|
|
from .aiohttp import StreamResponse, StreamSession
|
2024-01-26 06:54:13 +00:00
|
|
|
has_curl_cffi = False
|
2024-01-14 06:45:41 +00:00
|
|
|
|
2024-02-21 23:16:58 +00:00
|
|
|
from ..webdriver import WebDriver, WebDriverSession, bypass_cloudflare, get_driver_cookies
|
|
|
|
from ..errors import MissingRequirementsError
|
2024-01-29 17:14:46 +00:00
|
|
|
from .defaults import DEFAULT_HEADERS
|
2024-01-26 06:54:13 +00:00
|
|
|
|
2024-02-23 10:33:38 +00:00
|
|
|
def get_args_from_browser(
|
|
|
|
url: str,
|
|
|
|
webdriver: WebDriver = None,
|
|
|
|
proxy: str = None,
|
|
|
|
timeout: int = 120,
|
|
|
|
do_bypass_cloudflare: bool = True
|
|
|
|
) -> dict:
|
2024-01-14 06:45:41 +00:00
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
2024-01-27 01:00:44 +00:00
|
|
|
with WebDriverSession(webdriver, "", proxy=proxy, virtual_display=False) as driver:
|
2024-02-23 10:33:38 +00:00
|
|
|
if do_bypass_cloudflare:
|
|
|
|
bypass_cloudflare(driver, url, timeout)
|
2024-01-10 09:34:56 +00:00
|
|
|
cookies = get_driver_cookies(driver)
|
2023-12-02 04:40:07 +00:00
|
|
|
user_agent = driver.execute_script("return navigator.userAgent")
|
|
|
|
parse = urlparse(url)
|
2024-01-27 01:00:44 +00:00
|
|
|
return {
|
|
|
|
'cookies': cookies,
|
|
|
|
'headers': {
|
2024-01-29 17:14:46 +00:00
|
|
|
**DEFAULT_HEADERS,
|
|
|
|
'Authority': parse.netloc,
|
|
|
|
'Origin': f'{parse.scheme}://{parse.netloc}',
|
|
|
|
'Referer': url,
|
|
|
|
'User-Agent': user_agent,
|
2023-12-02 04:40:07 +00:00
|
|
|
},
|
2024-01-27 01:00:44 +00:00
|
|
|
}
|
2024-01-29 17:14:46 +00:00
|
|
|
|
2024-01-27 01:00:44 +00:00
|
|
|
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,
|
2023-12-02 04:40:07 +00:00
|
|
|
proxies={"https": proxy, "http": proxy},
|
|
|
|
timeout=timeout,
|
|
|
|
impersonate="chrome110"
|
2024-01-14 06:45:41 +00:00
|
|
|
)
|