Use flaresolverr v3
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Michel Roux 2023-01-05 15:19:04 +01:00
parent da4aaff8d4
commit 125d0bde2d

View File

@ -1,6 +1,6 @@
from io import BytesIO from io import BytesIO
from urllib import parse from urllib import parse
from requests import RequestException, Response, Session, post from requests import Response, Session, post
from .config import CLOUDPROXY_ENDPOINT from .config import CLOUDPROXY_ENDPOINT
@ -9,67 +9,42 @@ class FlareRequests(Session):
if not CLOUDPROXY_ENDPOINT: if not CLOUDPROXY_ENDPOINT:
return super().request(method, url, params, data, **kwargs) return super().request(method, url, params, data, **kwargs)
sessions = post(CLOUDPROXY_ENDPOINT, json={"cmd": "sessions.list"}).json()
if "sessions" in sessions and len(sessions["sessions"]) > 0:
FLARESESSION = sessions["sessions"][0]
else:
response = post(CLOUDPROXY_ENDPOINT, json={"cmd": "sessions.create"})
session = response.json()
if "session" in session:
FLARESESSION = session["session"]
else:
raise RequestException(response)
if params: if params:
url += "&" if len(url.split("?")) > 1 else "?" url += "&" if len(url.split("?")) > 1 else "?"
url = f"{url}{parse.urlencode(params)}" url = f"{url}{parse.urlencode(params)}"
post_data = { post_data = {
"cmd": f"request.{method.lower()}", "cmd": f"request.{method.lower()}",
"session": FLARESESSION,
"url": url, "url": url,
} }
if data: if data:
post_data["postData"] = parse.urlencode(data) post_data["postData"] = parse.urlencode(data)
try: response = post(
response = post( CLOUDPROXY_ENDPOINT,
CLOUDPROXY_ENDPOINT, json=post_data,
json=post_data, )
)
solution = response.json() solution = response.json()
if "solution" in solution: if "solution" in solution:
if "content-type" in solution["solution"]["headers"]: headers = solution["solution"]["headers"]
content_type = solution["solution"]["headers"][ if "content-type" in headers:
"content-type" content_type = headers["content-type"].split(";")
].split(";") if len(content_type) > 1:
if len(content_type) > 1: charset = content_type[1].split("=")
charset = content_type[1].split("=") if len(charset) > 1:
if len(charset) > 1: encoding = charset[1]
encoding = charset[1]
resolved = Response() resolved = Response()
resolved.status_code = solution["solution"]["status"] resolved.status_code = solution["solution"]["status"]
resolved.headers = solution["solution"]["headers"] resolved.headers = headers
resolved.raw = BytesIO(solution["solution"]["response"].encode()) resolved.raw = BytesIO(solution["solution"]["response"].encode())
resolved.url = url resolved.url = url
resolved.encoding = encoding or None resolved.encoding = encoding or None
resolved.reason = solution["status"] resolved.reason = solution["status"]
resolved.cookies = solution["solution"]["cookies"] resolved.cookies = solution["solution"]["cookies"]
return resolved return resolved
raise RequestException(response)
except RequestException:
session = post(
CLOUDPROXY_ENDPOINT,
json={"cmd": "sessions.destroy", "session": FLARESESSION},
)
raise RequestException(solution)