From 943dbe3de5e4690f439424aa9245df26f4375cb3 Mon Sep 17 00:00:00 2001 From: Maxim S Date: Fri, 23 Aug 2024 17:54:40 +0200 Subject: [PATCH] =?UTF-8?q?1)=20Changes=20in=20Solver.py=20-=20Added=20the?= =?UTF-8?q?=20=E2=80=9CextendedResponse=E2=80=9D=20parameter=20to=20the=20?= =?UTF-8?q?initialization=20method.=20=E2=80=94=20In=20the=20get=5Fresult?= =?UTF-8?q?=20method,=20the=20output=20of=20an=20extended=20response=20is?= =?UTF-8?q?=20added=20if=20the=20=E2=80=98ExtendedResponse=E2=80=99=20para?= =?UTF-8?q?meter=20is=20passed.=20-=20Added=20processing=20of=20extended?= =?UTF-8?q?=20responses=20using=20in=20the=20=E2=80=9Csolve=E2=80=9D=20met?= =?UTF-8?q?hod=20if=20the=20=E2=80=98ExtendedResponse=E2=80=99=20parameter?= =?UTF-8?q?=20is=20passed.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 2) Also changed examples of coordinates_options.py, hcaptcha_options.py, geetest_options.py - output of extended answer Signed-off-by: Maxim S --- examples/coordinates_options.py | 2 +- examples/geetest_options.py | 2 +- examples/hcaptcha_options.py | 8 +++--- twocaptcha/solver.py | 48 ++++++++++++++++++++++++++------- 4 files changed, 45 insertions(+), 15 deletions(-) diff --git a/examples/coordinates_options.py b/examples/coordinates_options.py index 5ee9fba..91f6000 100644 --- a/examples/coordinates_options.py +++ b/examples/coordinates_options.py @@ -13,7 +13,7 @@ api_key = os.getenv('APIKEY_2CAPTCHA', 'YOUR_API_KEY') -solver = TwoCaptcha(api_key, defaultTimeout=120, pollingInterval=5) +solver = TwoCaptcha(api_key, defaultTimeout=120, pollingInterval=5, extendedResponse=True) try: result = solver.coordinates('./images/grid_2.jpg', diff --git a/examples/geetest_options.py b/examples/geetest_options.py index 39c491c..35e74a8 100644 --- a/examples/geetest_options.py +++ b/examples/geetest_options.py @@ -13,7 +13,7 @@ api_key = os.getenv('APIKEY_2CAPTCHA', 'YOUR_API_KEY') -solver = TwoCaptcha(api_key, defaultTimeout=300, pollingInterval=10) +solver = TwoCaptcha(api_key, defaultTimeout=300, pollingInterval=10, extendedResponse=True) """ Important: the value of the 'challenge' parameter is dynamic, for each request to our API you need to get a new value. diff --git a/examples/hcaptcha_options.py b/examples/hcaptcha_options.py index 7a56dc9..3ebf4d7 100644 --- a/examples/hcaptcha_options.py +++ b/examples/hcaptcha_options.py @@ -22,13 +22,14 @@ 'defaultTimeout': 120, 'recaptchaTimeout': 600, 'pollingInterval': 10, + 'extendedResponse': True, } solver = TwoCaptcha(**config) try: - result = solver.hcaptcha(sitekey='f7de0da3-3303-44e8-ab48-fa32ff8ccc7b', - url='https://2captcha.com/ru/demo/hcaptcha-invisible', + result = solver.hcaptcha(sitekey='c0421d06-b92e-47fc-ab9a-5caa43c04538', + url='https://2captcha.com/ru/demo/hcaptcha', # invisible=1, # data="rqdata", # useragent="", @@ -42,4 +43,5 @@ sys.exit(e) else: - sys.exit('result: ' + str(result)) + # sys.exit('result: ' + str(result)) + sys.exit(result) diff --git a/twocaptcha/solver.py b/twocaptcha/solver.py index f460baf..0d58fa7 100755 --- a/twocaptcha/solver.py +++ b/twocaptcha/solver.py @@ -41,7 +41,8 @@ def __init__(self, defaultTimeout=120, recaptchaTimeout=600, pollingInterval=10, - server = '2captcha.com'): + server = '2captcha.com', + extendedResponse=None): self.API_KEY = apiKey self.soft_id = softId @@ -52,6 +53,7 @@ def __init__(self, self.api_client = ApiClient(post_url = str(server)) self.max_files = 9 self.exceptions = SolverExceptions + self.extendedResponse = extendedResponse def normal(self, file, **kwargs): '''Wrapper for solving a normal captcha (image). @@ -831,14 +833,23 @@ def solve(self, timeout=0, polling_interval=0, **kwargs): result = {'captchaId': id_} if self.callback is None: - timeout = float(timeout or self.default_timeout) sleep = int(polling_interval or self.polling_interval) code = self.wait_result(id_, timeout, sleep) - result.update({'code': code}) - return result + if self.extendedResponse == True: + + new_code = { + key if key != 'request' else 'code': value + for key, value in code.items() + if key != 'status' + } + result.update(new_code) + else: + result.update({'code': code}) + + return result def wait_result(self, id_, timeout, polling_interval): @@ -900,6 +911,7 @@ def send(self, **kwargs): return response[3:] def get_result(self, id_): + import json """This method can be used for manual captcha answer polling. Parameters @@ -911,15 +923,31 @@ def get_result(self, id_): answer : text """ - response = self.api_client.res(key=self.API_KEY, action='get', id=id_) + if self.extendedResponse == True: - if response == 'CAPCHA_NOT_READY': - raise NetworkException + response = self.api_client.res(key=self.API_KEY, action='get', id=id_, json=1) - if not response.startswith('OK|'): - raise ApiException(f'cannot recognize response {response}') + response_data = json.loads(response) - return response[3:] + if response_data.get("status") == 0: + raise NetworkException + + if not response_data.get("status") == 1: + raise ApiException(f'Unexpected status in response: {response_data}') + + return response_data + + else: + + response = self.api_client.res(key=self.API_KEY, action='get', id=id_) + + if response == 'CAPCHA_NOT_READY': + raise NetworkException + + if not response.startswith('OK|'): + raise ApiException(f'cannot recognize response {response}') + + return response[3:] def balance(self): '''Get my balance