2023-09-25 18:52:29 -04:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-10-07 04:17:43 -04:00
|
|
|
import json
|
2023-11-18 23:36:04 -05:00
|
|
|
from contextlib import asynccontextmanager
|
2023-10-01 20:04:22 -04:00
|
|
|
from functools import partialmethod
|
2023-11-18 23:36:04 -05:00
|
|
|
from typing import AsyncGenerator
|
2023-12-01 23:40:07 -05:00
|
|
|
from urllib.parse import urlparse
|
|
|
|
from curl_cffi.requests import AsyncSession, Session, Response
|
|
|
|
from .webdriver import WebDriver, WebDriverSession
|
|
|
|
from selenium.webdriver.common.by import By
|
|
|
|
from selenium.webdriver.support.ui import WebDriverWait
|
|
|
|
from selenium.webdriver.support import expected_conditions as EC
|
2023-09-25 18:52:29 -04:00
|
|
|
|
|
|
|
class StreamResponse:
|
2023-11-18 23:36:04 -05:00
|
|
|
def __init__(self, inner: Response) -> None:
|
2023-10-07 04:17:43 -04:00
|
|
|
self.inner: Response = inner
|
2023-10-01 20:04:22 -04:00
|
|
|
self.request = inner.request
|
2023-10-07 04:17:43 -04:00
|
|
|
self.status_code: int = inner.status_code
|
|
|
|
self.reason: str = inner.reason
|
|
|
|
self.ok: bool = inner.ok
|
2023-09-26 04:03:37 -04:00
|
|
|
self.headers = inner.headers
|
|
|
|
self.cookies = inner.cookies
|
2023-09-25 18:52:29 -04:00
|
|
|
|
|
|
|
async def text(self) -> str:
|
2023-11-18 23:36:04 -05:00
|
|
|
return await self.inner.atext()
|
2023-09-25 18:52:29 -04:00
|
|
|
|
2023-10-07 04:17:43 -04:00
|
|
|
def raise_for_status(self) -> None:
|
2023-11-18 23:36:04 -05:00
|
|
|
self.inner.raise_for_status()
|
2023-09-25 18:52:29 -04:00
|
|
|
|
2023-10-07 04:17:43 -04:00
|
|
|
async def json(self, **kwargs) -> dict:
|
2023-11-18 23:36:04 -05:00
|
|
|
return json.loads(await self.inner.acontent(), **kwargs)
|
2023-10-01 20:04:22 -04:00
|
|
|
|
2023-11-18 23:36:04 -05:00
|
|
|
async def iter_lines(self) -> AsyncGenerator[bytes, None]:
|
|
|
|
async for line in self.inner.aiter_lines():
|
|
|
|
yield line
|
2023-10-01 20:04:22 -04:00
|
|
|
|
2023-11-18 23:36:04 -05:00
|
|
|
async def iter_content(self) -> AsyncGenerator[bytes, None]:
|
|
|
|
async for chunk in self.inner.aiter_content():
|
2023-10-01 20:04:22 -04:00
|
|
|
yield chunk
|
|
|
|
|
|
|
|
class StreamSession(AsyncSession):
|
2023-11-18 23:36:04 -05:00
|
|
|
@asynccontextmanager
|
|
|
|
async def request(
|
2023-10-07 13:00:45 -04:00
|
|
|
self, method: str, url: str, **kwargs
|
2023-11-18 23:36:04 -05:00
|
|
|
) -> AsyncGenerator[StreamResponse]:
|
|
|
|
response = await super().request(method, url, stream=True, **kwargs)
|
|
|
|
try:
|
|
|
|
yield StreamResponse(response)
|
|
|
|
finally:
|
|
|
|
await response.aclose()
|
2023-10-07 04:17:43 -04:00
|
|
|
|
2023-09-26 04:03:37 -04:00
|
|
|
head = partialmethod(request, "HEAD")
|
|
|
|
get = partialmethod(request, "GET")
|
|
|
|
post = partialmethod(request, "POST")
|
|
|
|
put = partialmethod(request, "PUT")
|
|
|
|
patch = partialmethod(request, "PATCH")
|
2023-12-01 23:40:07 -05:00
|
|
|
delete = partialmethod(request, "DELETE")
|
|
|
|
|
|
|
|
def get_session_from_browser(url: str, webdriver: WebDriver = None, proxy: str = None, timeout: int = 120):
|
|
|
|
with WebDriverSession(webdriver, "", proxy=proxy, virtual_display=True) as driver:
|
|
|
|
driver.get(url)
|
|
|
|
|
|
|
|
# Is cloudflare protection
|
|
|
|
if driver.find_element(By.TAG_NAME, "body").get_attribute("class") == "no-js":
|
|
|
|
try:
|
|
|
|
# Click button in iframe
|
|
|
|
WebDriverWait(driver, 5).until(
|
|
|
|
EC.presence_of_element_located((By.CSS_SELECTOR, "#turnstile-wrapper iframe"))
|
|
|
|
)
|
|
|
|
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, "#turnstile-wrapper iframe"))
|
|
|
|
WebDriverWait(driver, 5).until(
|
|
|
|
EC.presence_of_element_located((By.CSS_SELECTOR, "#challenge-stage input"))
|
|
|
|
)
|
|
|
|
driver.find_element(By.CSS_SELECTOR, "#challenge-stage input").click()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
driver.switch_to.default_content()
|
|
|
|
# No cloudflare protection
|
|
|
|
WebDriverWait(driver, timeout).until(
|
|
|
|
EC.presence_of_element_located((By.CSS_SELECTOR, "body:not(.no-js)"))
|
|
|
|
)
|
|
|
|
|
|
|
|
cookies = dict([(cookie["name"], cookie["value"]) for cookie in driver.get_cookies()])
|
|
|
|
user_agent = driver.execute_script("return navigator.userAgent")
|
|
|
|
|
|
|
|
parse = urlparse(url)
|
|
|
|
return Session(
|
|
|
|
cookies=cookies,
|
|
|
|
headers={
|
|
|
|
'accept': '*/*',
|
|
|
|
'authority': parse.netloc,
|
|
|
|
'origin': f'{parse.scheme}://{parse.netloc}',
|
|
|
|
'referer': url,
|
|
|
|
'sec-fetch-dest': 'empty',
|
|
|
|
'sec-fetch-mode': 'cors',
|
|
|
|
'sec-fetch-site': 'same-origin',
|
|
|
|
'user-agent': user_agent
|
|
|
|
},
|
|
|
|
proxies={"https": proxy, "http": proxy},
|
|
|
|
timeout=timeout,
|
|
|
|
impersonate="chrome110"
|
|
|
|
)
|