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 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
@ -9,43 +10,61 @@ class FlareRequests(Session):
if not CLOUDPROXY_ENDPOINT:
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:
url += "&" if len(url.split("?")) > 1 else "?"
url = f"{url}{parse.urlencode(params)}"
post_data = {
"cmd": f"request.{method.lower()}",
"session": FLARE_SESSION,
"url": url,
}
if data:
post_data["postData"] = parse.urlencode(data)
response = post(
CLOUDPROXY_ENDPOINT,
json=post_data,
)
try:
response = post(
CLOUDPROXY_ENDPOINT,
json=post_data,
)
solution = response.json()
content = response.json()
if "solution" in solution:
encoding = None
headers = solution["solution"]["headers"]
if "content-type" in headers:
content_type = headers["content-type"].split(";")
if len(content_type) > 1:
charset = content_type[1].split("=")
if len(charset) > 1:
encoding = charset[1]
if "solution" in content:
solution = content["solution"]
raw = solution["response"].encode()
encoding = detect(raw)
resolved = Response()
resolved = Response()
resolved.status_code = solution["status"]
resolved.headers = solution["headers"]
resolved.raw = BytesIO(raw)
resolved.url = url
resolved.encoding = encoding["encoding"]
resolved.reason = content["status"]
resolved.cookies = solution["cookies"]
resolved.status_code = solution["solution"]["status"]
resolved.headers = headers
resolved.raw = BytesIO(solution["solution"]["response"].encode())
resolved.url = url
resolved.encoding = encoding
resolved.reason = solution["status"]
resolved.cookies = solution["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
pg8000==1.29.4
requests==2.28.2
charset-normalizer==3.0.1
beautifulsoup4==4.11.1
python-dotenv==0.21.0
dateparser==1.1.6