Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RС-2910-use-json-format #107

Merged
merged 4 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Examples of API requests for different captcha types are available on the [Pytho
- [Cutcaptcha](#cutcaptcha)
- [Tencent](#tencent)
- [Other methods](#other-methods)
- [send / get_result](#send--get_result)
- [send / get\_result](#send--get_result)
- [balance](#balance)
- [report](#report)
- [Error handling](#error-handling)
Expand All @@ -51,10 +51,10 @@ Examples of API requests for different captcha types are available on the [Pytho
- [Examples](#examples)
- [Examples using Selenium](#examples-using-selenium)
- [Useful articles](#useful-articles)
- [Get in touch](#get-in-touch)
- [Join the team 👪](#join-the-team-)
- [License](#license)
- [Graphics and Trademarks](#graphics-and-trademarks)
- [Get in touch](#get-in-touch)
- [Join the team 👪](#join-the-team-)
- [License](#license)
- [Graphics and Trademarks](#graphics-and-trademarks)

## Installation

Expand Down Expand Up @@ -99,6 +99,8 @@ solver = TwoCaptcha(**config)
| 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 |
| recaptchaTimeout | 600 | Polling timeout for reCAPTCHA in seconds. Defines how long the module tries to get the answer from the `res.php` API endpoint |
| pollingInterval | 10 | Interval in seconds between requests to the `res.php` API endpoint. Setting values less than 5 seconds is not recommended |
| 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) |


> [!IMPORTANT]
> 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.
Expand Down
2 changes: 1 addition & 1 deletion examples/coordinates_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion examples/geetest_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions examples/hcaptcha_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="",
Expand All @@ -43,3 +44,4 @@

else:
sys.exit('result: ' + str(result))

48 changes: 38 additions & 10 deletions twocaptcha/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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).
Expand Down Expand Up @@ -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):

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down