From 8dc0b49e18b7bf04c6f2bcd6f6c7053afa7feb96 Mon Sep 17 00:00:00 2001 From: Michel Roux Date: Wed, 21 Dec 2022 17:12:18 +0000 Subject: [PATCH] Remove FlareSolverr session management --- pynyaata/flarerequests.py | 62 +++++++++++++-------------------------- 1 file changed, 20 insertions(+), 42 deletions(-) diff --git a/pynyaata/flarerequests.py b/pynyaata/flarerequests.py index 1920b97..93a21e0 100644 --- a/pynyaata/flarerequests.py +++ b/pynyaata/flarerequests.py @@ -8,49 +8,27 @@ class FlareRequests(Session): if not CLOUDPROXY_ENDPOINT: return super().request(method, url, params, timeout=timeout, **kwargs) - sessions = post(CLOUDPROXY_ENDPOINT, json={"cmd": "sessions.list"}).json() + 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 "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 "solution" in solution: + resolved = Response() - if "session" in session: - FLARESESSION = session["session"] - else: - raise RequestException(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"] - try: - response = post( - CLOUDPROXY_ENDPOINT, - json={ - "cmd": f"request.{method.lower()}", - "session": FLARESESSION, - "url": url, - "postData": parse.urlencode(params) if params else "", - "maxTimeout": timeout * 1000, - }, - ) - solution = response.json() + return resolved - 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) - except RequestException: - session = post( - CLOUDPROXY_ENDPOINT, - json={"cmd": "sessions.destroy", "session": FLARESESSION}, - ) - - raise RequestException(solution) + raise RequestException(response)