requests.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from __future__ import annotations
  2. import json
  3. from contextlib import asynccontextmanager
  4. from functools import partialmethod
  5. from typing import AsyncGenerator
  6. from urllib.parse import urlparse
  7. from curl_cffi.requests import AsyncSession, Session, Response
  8. from .webdriver import WebDriver, WebDriverSession, bypass_cloudflare
  9. class StreamResponse:
  10. def __init__(self, inner: Response) -> None:
  11. self.inner: Response = inner
  12. self.request = inner.request
  13. self.status_code: int = inner.status_code
  14. self.reason: str = inner.reason
  15. self.ok: bool = inner.ok
  16. self.headers = inner.headers
  17. self.cookies = inner.cookies
  18. async def text(self) -> str:
  19. return await self.inner.atext()
  20. def raise_for_status(self) -> None:
  21. self.inner.raise_for_status()
  22. async def json(self, **kwargs) -> dict:
  23. return json.loads(await self.inner.acontent(), **kwargs)
  24. async def iter_lines(self) -> AsyncGenerator[bytes, None]:
  25. async for line in self.inner.aiter_lines():
  26. yield line
  27. async def iter_content(self) -> AsyncGenerator[bytes, None]:
  28. async for chunk in self.inner.aiter_content():
  29. yield chunk
  30. class StreamSession(AsyncSession):
  31. @asynccontextmanager
  32. async def request(
  33. self, method: str, url: str, **kwargs
  34. ) -> AsyncGenerator[StreamResponse]:
  35. response = await super().request(method, url, stream=True, **kwargs)
  36. try:
  37. yield StreamResponse(response)
  38. finally:
  39. await response.aclose()
  40. head = partialmethod(request, "HEAD")
  41. get = partialmethod(request, "GET")
  42. post = partialmethod(request, "POST")
  43. put = partialmethod(request, "PUT")
  44. patch = partialmethod(request, "PATCH")
  45. delete = partialmethod(request, "DELETE")
  46. def get_session_from_browser(url: str, webdriver: WebDriver = None, proxy: str = None, timeout: int = 120):
  47. with WebDriverSession(webdriver, "", proxy=proxy, virtual_display=True) as driver:
  48. bypass_cloudflare(driver, url, timeout)
  49. cookies = dict([(cookie["name"], cookie["value"]) for cookie in driver.get_cookies()])
  50. user_agent = driver.execute_script("return navigator.userAgent")
  51. parse = urlparse(url)
  52. return Session(
  53. cookies=cookies,
  54. headers={
  55. 'accept': '*/*',
  56. 'authority': parse.netloc,
  57. 'origin': f'{parse.scheme}://{parse.netloc}',
  58. 'referer': url,
  59. 'sec-fetch-dest': 'empty',
  60. 'sec-fetch-mode': 'cors',
  61. 'sec-fetch-site': 'same-origin',
  62. 'user-agent': user_agent
  63. },
  64. proxies={"https": proxy, "http": proxy},
  65. timeout=timeout,
  66. impersonate="chrome110"
  67. )