Skip to content

Commit 943dbe3

Browse files
committed
1) Changes in Solver.py
- Added the “extendedResponse” parameter to the initialization method. — In the get_result method, the output of an extended response is added if the ‘ExtendedResponse’ parameter is passed. - Added processing of extended responses using in the “solve” method if the ‘ExtendedResponse’ parameter is passed. 2) Also changed examples of coordinates_options.py, hcaptcha_options.py, geetest_options.py - output of extended answer Signed-off-by: Maxim S <[email protected]>
1 parent 6aef124 commit 943dbe3

File tree

4 files changed

+45
-15
lines changed

4 files changed

+45
-15
lines changed

examples/coordinates_options.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
api_key = os.getenv('APIKEY_2CAPTCHA', 'YOUR_API_KEY')
1515

16-
solver = TwoCaptcha(api_key, defaultTimeout=120, pollingInterval=5)
16+
solver = TwoCaptcha(api_key, defaultTimeout=120, pollingInterval=5, extendedResponse=True)
1717

1818
try:
1919
result = solver.coordinates('./images/grid_2.jpg',

examples/geetest_options.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
api_key = os.getenv('APIKEY_2CAPTCHA', 'YOUR_API_KEY')
1515

16-
solver = TwoCaptcha(api_key, defaultTimeout=300, pollingInterval=10)
16+
solver = TwoCaptcha(api_key, defaultTimeout=300, pollingInterval=10, extendedResponse=True)
1717

1818
"""
1919
Important: the value of the 'challenge' parameter is dynamic, for each request to our API you need to get a new value.

examples/hcaptcha_options.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222
'defaultTimeout': 120,
2323
'recaptchaTimeout': 600,
2424
'pollingInterval': 10,
25+
'extendedResponse': True,
2526
}
2627

2728
solver = TwoCaptcha(**config)
2829

2930
try:
30-
result = solver.hcaptcha(sitekey='f7de0da3-3303-44e8-ab48-fa32ff8ccc7b',
31-
url='https://2captcha.com/ru/demo/hcaptcha-invisible',
31+
result = solver.hcaptcha(sitekey='c0421d06-b92e-47fc-ab9a-5caa43c04538',
32+
url='https://2captcha.com/ru/demo/hcaptcha',
3233
# invisible=1,
3334
# data="rqdata",
3435
# useragent="",
@@ -42,4 +43,5 @@
4243
sys.exit(e)
4344

4445
else:
45-
sys.exit('result: ' + str(result))
46+
# sys.exit('result: ' + str(result))
47+
sys.exit(result)

twocaptcha/solver.py

+38-10
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ def __init__(self,
4141
defaultTimeout=120,
4242
recaptchaTimeout=600,
4343
pollingInterval=10,
44-
server = '2captcha.com'):
44+
server = '2captcha.com',
45+
extendedResponse=None):
4546

4647
self.API_KEY = apiKey
4748
self.soft_id = softId
@@ -52,6 +53,7 @@ def __init__(self,
5253
self.api_client = ApiClient(post_url = str(server))
5354
self.max_files = 9
5455
self.exceptions = SolverExceptions
56+
self.extendedResponse = extendedResponse
5557

5658
def normal(self, file, **kwargs):
5759
'''Wrapper for solving a normal captcha (image).
@@ -831,14 +833,23 @@ def solve(self, timeout=0, polling_interval=0, **kwargs):
831833
result = {'captchaId': id_}
832834

833835
if self.callback is None:
834-
835836
timeout = float(timeout or self.default_timeout)
836837
sleep = int(polling_interval or self.polling_interval)
837838

838839
code = self.wait_result(id_, timeout, sleep)
839-
result.update({'code': code})
840840

841-
return result
841+
if self.extendedResponse == True:
842+
843+
new_code = {
844+
key if key != 'request' else 'code': value
845+
for key, value in code.items()
846+
if key != 'status'
847+
}
848+
result.update(new_code)
849+
else:
850+
result.update({'code': code})
851+
852+
return result
842853

843854
def wait_result(self, id_, timeout, polling_interval):
844855

@@ -900,6 +911,7 @@ def send(self, **kwargs):
900911
return response[3:]
901912

902913
def get_result(self, id_):
914+
import json
903915
"""This method can be used for manual captcha answer polling.
904916
905917
Parameters
@@ -911,15 +923,31 @@ def get_result(self, id_):
911923
answer : text
912924
"""
913925

914-
response = self.api_client.res(key=self.API_KEY, action='get', id=id_)
926+
if self.extendedResponse == True:
915927

916-
if response == 'CAPCHA_NOT_READY':
917-
raise NetworkException
928+
response = self.api_client.res(key=self.API_KEY, action='get', id=id_, json=1)
918929

919-
if not response.startswith('OK|'):
920-
raise ApiException(f'cannot recognize response {response}')
930+
response_data = json.loads(response)
921931

922-
return response[3:]
932+
if response_data.get("status") == 0:
933+
raise NetworkException
934+
935+
if not response_data.get("status") == 1:
936+
raise ApiException(f'Unexpected status in response: {response_data}')
937+
938+
return response_data
939+
940+
else:
941+
942+
response = self.api_client.res(key=self.API_KEY, action='get', id=id_)
943+
944+
if response == 'CAPCHA_NOT_READY':
945+
raise NetworkException
946+
947+
if not response.startswith('OK|'):
948+
raise ApiException(f'cannot recognize response {response}')
949+
950+
return response[3:]
923951

924952
def balance(self):
925953
'''Get my balance

0 commit comments

Comments
 (0)