From 060730eee87e862bcc0be413805c5f0b706b4274 Mon Sep 17 00:00:00 2001 From: Michel Roux Date: Sat, 21 Jan 2023 18:06:48 +0100 Subject: [PATCH] Readd FlareRequests --- pynyaata/flarerequests.py | 67 +++++++++++++++++++++++++-------------- requirements.txt | 1 + 2 files changed, 44 insertions(+), 24 deletions(-) diff --git a/pynyaata/flarerequests.py b/pynyaata/flarerequests.py index 9ee5e6a..5cf2f11 100644 --- a/pynyaata/flarerequests.py +++ b/pynyaata/flarerequests.py @@ -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) diff --git a/requirements.txt b/requirements.txt index 6b279b3..2b7a84e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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