This repository has been archived on 2023-10-01. You can view files and clone it, but cannot push or open issues or pull requests.
PyNyaaTa/pynyaata/flarerequests.py
Michel Roux 8dc0b49e18
All checks were successful
continuous-integration/drone/push Build is passing
Remove FlareSolverr session management
2022-12-21 17:12:18 +00:00

35 lines
1.2 KiB
Python

from urllib import parse
from requests import RequestException, Response, Session, post
from .config import CLOUDPROXY_ENDPOINT, REQUESTS_TIMEOUT
class FlareRequests(Session):
def request(self, method, url, params=None, timeout=REQUESTS_TIMEOUT, **kwargs):
if not CLOUDPROXY_ENDPOINT:
return super().request(method, url, params, timeout=timeout, **kwargs)
response = post(
CLOUDPROXY_ENDPOINT,
json={
"cmd": f"request.{method.lower()}",
"url": url,
"postData": parse.urlencode(params) if params else "",
"maxTimeout": timeout * 1000,
},
)
solution = response.json()
if "solution" in solution:
resolved = Response()
resolved.raw = solution["solution"]["response"]
resolved.status_code = solution["solution"]["status"]
resolved.headers = solution["solution"]["headers"]
resolved.url = url
resolved.reason = solution["status"]
resolved.cookies = solution["solution"]["cookies"]
return resolved
raise RequestException(response)