Readd FlareRequests
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Michel Roux 2023-01-21 18:06:48 +01:00
parent a6326afa85
commit 060730eee8
2 changed files with 44 additions and 24 deletions

View File

@ -1,6 +1,7 @@
from io import BytesIO from io import BytesIO
from urllib import parse from urllib import parse
from requests import Response, Session, post from charset_normalizer import detect
from requests import RequestException, Response, Session, post
from .config import CLOUDPROXY_ENDPOINT from .config import CLOUDPROXY_ENDPOINT
@ -9,43 +10,61 @@ 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:
FLARE_SESSION = sessions["sessions"][0]
else:
response = post(CLOUDPROXY_ENDPOINT, json={"cmd": "sessions.create"})
session = response.json()
if "session" in session:
FLARE_SESSION = 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": FLARE_SESSION,
"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() content = response.json()
if "solution" in solution: if "solution" in content:
encoding = None solution = content["solution"]
headers = solution["solution"]["headers"] raw = solution["response"].encode()
if "content-type" in headers: encoding = detect(raw)
content_type = headers["content-type"].split(";")
if len(content_type) > 1:
charset = content_type[1].split("=")
if len(charset) > 1:
encoding = charset[1]
resolved = Response() resolved = Response()
resolved.status_code = solution["status"]
resolved.status_code = solution["solution"]["status"] resolved.headers = solution["headers"]
resolved.headers = headers resolved.raw = BytesIO(raw)
resolved.raw = BytesIO(solution["solution"]["response"].encode())
resolved.url = url resolved.url = url
resolved.encoding = encoding resolved.encoding = encoding["encoding"]
resolved.reason = solution["status"] resolved.reason = content["status"]
resolved.cookies = solution["solution"]["cookies"] resolved.cookies = solution["cookies"]
return resolved return resolved
raise RequestException(response)
except RequestException:
session = post(
CLOUDPROXY_ENDPOINT,
json={"cmd": "sessions.destroy", "session": FLARE_SESSION},
)
raise RequestException(solution)

View File

@ -6,6 +6,7 @@ WTForms==2.3.3
PyMySQL==1.0.2 PyMySQL==1.0.2
pg8000==1.29.4 pg8000==1.29.4
requests==2.28.2 requests==2.28.2
charset-normalizer==3.0.1
beautifulsoup4==4.11.1 beautifulsoup4==4.11.1
python-dotenv==0.21.0 python-dotenv==0.21.0
dateparser==1.1.6 dateparser==1.1.6