Skip to content

Commit df9f4af

Browse files
authored
Merge pull request #107 from 2captcha/RC-2910-use-json-format
RС-2910-use-json-format
2 parents 2bd5b2d + 9dc0da4 commit df9f4af

File tree

5 files changed

+51
-19
lines changed

5 files changed

+51
-19
lines changed

README.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Examples of API requests for different captcha types are available on the [Pytho
4444
- [Datadome](#datadome)
4545
- [CyberSiARA](#cybersiara)
4646
- [Other methods](#other-methods)
47-
- [send / get_result](#send--get_result)
47+
- [send / get\_result](#send--get_result)
4848
- [balance](#balance)
4949
- [report](#report)
5050
- [Error handling](#error-handling)
@@ -53,10 +53,10 @@ Examples of API requests for different captcha types are available on the [Pytho
5353
- [Examples](#examples)
5454
- [Examples using Selenium](#examples-using-selenium)
5555
- [Useful articles](#useful-articles)
56-
- [Get in touch](#get-in-touch)
57-
- [Join the team 👪](#join-the-team-)
58-
- [License](#license)
59-
- [Graphics and Trademarks](#graphics-and-trademarks)
56+
- [Get in touch](#get-in-touch)
57+
- [Join the team 👪](#join-the-team-)
58+
- [License](#license)
59+
- [Graphics and Trademarks](#graphics-and-trademarks)
6060

6161
## Installation
6262

@@ -101,6 +101,8 @@ solver = TwoCaptcha(**config)
101101
| defaultTimeout | 120 | Polling timeout in seconds for all captcha types except reCAPTCHA. Defines how long the module tries to get the answer from the `res.php` API endpoint |
102102
| recaptchaTimeout | 600 | Polling timeout for reCAPTCHA in seconds. Defines how long the module tries to get the answer from the `res.php` API endpoint |
103103
| pollingInterval | 10 | Interval in seconds between requests to the `res.php` API endpoint. Setting values less than 5 seconds is not recommended |
104+
| extendedResponse | None | Set to `True` to get the response with additional fields or in more practical format (enables `JSON` response from `res.php` API endpoint). Suitable for [hCaptcha](#hcaptcha), [ClickCaptcha](#clickcaptcha), [Canvas](#canvas) |
105+
104106

105107
> [!IMPORTANT]
106108
> Once `callback` is defined for the `TwoCaptcha` instance, all methods return only the captcha ID and DO NOT poll the API to get the result. The result will be sent to the callback URL.

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

+4-2
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="",
@@ -43,3 +44,4 @@
4344

4445
else:
4546
sys.exit('result: ' + str(result))
47+

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).
@@ -875,14 +877,23 @@ def solve(self, timeout=0, polling_interval=0, **kwargs):
875877
result = {'captchaId': id_}
876878

877879
if self.callback is None:
878-
879880
timeout = float(timeout or self.default_timeout)
880881
sleep = int(polling_interval or self.polling_interval)
881882

882883
code = self.wait_result(id_, timeout, sleep)
883-
result.update({'code': code})
884884

885-
return result
885+
if self.extendedResponse == True:
886+
887+
new_code = {
888+
key if key != 'request' else 'code': value
889+
for key, value in code.items()
890+
if key != 'status'
891+
}
892+
result.update(new_code)
893+
else:
894+
result.update({'code': code})
895+
896+
return result
886897

887898
def wait_result(self, id_, timeout, polling_interval):
888899

@@ -944,6 +955,7 @@ def send(self, **kwargs):
944955
return response[3:]
945956

946957
def get_result(self, id_):
958+
import json
947959
"""This method can be used for manual captcha answer polling.
948960
949961
Parameters
@@ -955,15 +967,31 @@ def get_result(self, id_):
955967
answer : text
956968
"""
957969

958-
response = self.api_client.res(key=self.API_KEY, action='get', id=id_)
970+
if self.extendedResponse == True:
959971

960-
if response == 'CAPCHA_NOT_READY':
961-
raise NetworkException
972+
response = self.api_client.res(key=self.API_KEY, action='get', id=id_, json=1)
962973

963-
if not response.startswith('OK|'):
964-
raise ApiException(f'cannot recognize response {response}')
974+
response_data = json.loads(response)
965975

966-
return response[3:]
976+
if response_data.get("status") == 0:
977+
raise NetworkException
978+
979+
if not response_data.get("status") == 1:
980+
raise ApiException(f'Unexpected status in response: {response_data}')
981+
982+
return response_data
983+
984+
else:
985+
986+
response = self.api_client.res(key=self.API_KEY, action='get', id=id_)
987+
988+
if response == 'CAPCHA_NOT_READY':
989+
raise NetworkException
990+
991+
if not response.startswith('OK|'):
992+
raise ApiException(f'cannot recognize response {response}')
993+
994+
return response[3:]
967995

968996
def balance(self):
969997
'''Get my balance

0 commit comments

Comments
 (0)