Skip to content

Commit 9d21153

Browse files
committed
Added ability of report incorrect captchas
1 parent 13f4c3b commit 9d21153

7 files changed

Lines changed: 114 additions & 11 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
**/public
33
**/.cache
44
**/node_modules
5-
**/.DS_Store
5+
**/.DS_Store
6+
**/.venv

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"[python]": {
3+
"editor.defaultFormatter": "ms-python.autopep8"
4+
},
5+
"python.formatting.provider": "none"
6+
}

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,24 @@ print(result.get("gRecaptchaResponse"))
6464
```python
6565
from capmonster_python import GeeTestTask
6666

67-
capmonster_python = GeeTestTask("API_KEY")
68-
task_id = capmonster_python.create_task("website_url", "gt", "challenge")
69-
result= capmonster_python.join_task_result(task_id)
67+
capmonster = GeeTestTask("API_KEY")
68+
task_id = capmonster.create_task("website_url", "gt", "challenge")
69+
result= capmonster.join_task_result(task_id)
7070
print(result.get("challenge"))
7171
print(result.get("seccode"))
7272
print(result.get("validate"))
7373
```
7474

75+
#### Report incorrect captchas
76+
77+
```python
78+
from capmonster_python import RecaptchaV2Task
79+
80+
capmonster = RecaptchaV2Task("API_KEY")
81+
task_id = capmonster.create_task("website_url", "website_key")
82+
result = capmonster.join_task_result(task_id)
83+
report_result = capmonster.report_incorrect_captcha("token", task_id)
84+
print(report_result)
85+
```
86+
7587
For other examples and api documentation please visit [wiki](https://alperensert.github.io/capmonster_python)

capmonster_python/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
from .image_to_text import ImageToTextTask
77
from .geetest import GeeTestTask
88
from .utils import CapmonsterException
9+
from .compleximage import ComplexImageTask

capmonster_python/capmonster.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class Capmonster:
1010
_CREATE_TASK_URL = "/createTask"
1111
_TASK_RESULT_URL = "/getTaskResult"
1212
_BALANCE_URL = "/getBalance"
13+
_INCORRECT_IMAGE_CAPTCHA_URL = "/reportIncorrectImageCaptcha"
14+
_INCORRECT_TOKEN_CAPTCHA_URL = "/reportIncorrectTokenCaptcha"
1315

1416
def __init__(self, client_key):
1517
self._client_key = client_key
@@ -38,7 +40,8 @@ def join_task_result(self, task_id: int, maximum_time: int = 120):
3840
elif result is False:
3941
i += 1
4042
sleep(2)
41-
raise CapmonsterException(61, "ERROR_MAXIMUM_TIME_EXCEED", "Maximum time is exceed.")
43+
raise CapmonsterException(
44+
61, "ERROR_MAXIMUM_TIME_EXCEED", "Maximum time is exceed.")
4245

4346
async def join_task_result_async(self, task_id: int, maximum_time: int = 120):
4447
for i in range(0, maximum_time + 1, 2):
@@ -48,7 +51,31 @@ async def join_task_result_async(self, task_id: int, maximum_time: int = 120):
4851
elif result is False:
4952
i += 1
5053
await asyncio.sleep(2)
51-
raise CapmonsterException(61, "ERROR_MAXIMUM_TIME_EXCEED", "Maximum time is exceed.")
54+
raise CapmonsterException(
55+
61, "ERROR_MAXIMUM_TIME_EXCEED", "Maximum time is exceed.")
56+
57+
def report_incorrect_captcha(self, captcha_type: str, task_id: int) -> bool:
58+
if captcha_type is not "image" or "token":
59+
raise CapmonsterException(
60+
1, "ERROR_INCORRECT_CAPTCHA_TYPE", "Valid captcha_type parameters are only 'image' or 'token'.")
61+
try:
62+
self._report_incorrect_captcha(
63+
captcha_type=captcha_type, task_id=task_id)
64+
return True
65+
except:
66+
return False
67+
68+
@check_response()
69+
def _report_incorrect_captcha(self, captcha_type: str, task_id: int):
70+
data = {
71+
"clientKey": self._client_key,
72+
"taskId": task_id
73+
}
74+
if captcha_type is "image":
75+
response = self._make_request("reportIncorrectImageCaptcha", data)
76+
else:
77+
response = self._make_request("reportIncorrectTokenCaptcha", data)
78+
return response
5279

5380
@staticmethod
5481
def _is_ready(response: dict):
@@ -68,8 +95,13 @@ def _make_request(self, method: str, data: dict):
6895
elif method == "createTask":
6996
_method = self._CREATE_TASK_URL
7097
data["softId"] = self.__SOFT_ID
98+
elif method == "reportIncorrectImageCaptcha":
99+
_method = self._INCORRECT_IMAGE_CAPTCHA_URL
100+
elif method == "reportIncorrectTokenCaptcha":
101+
_method = self._INCORRECT_TOKEN_CAPTCHA_URL
71102
try:
72-
response = requests.post("{}{}".format(self._HOST_URL, _method), json=data).json()
103+
response = requests.post("{}{}".format(
104+
self._HOST_URL, _method), json=data).json()
73105
except Exception as err:
74106
raise CapmonsterException(-1, type(err).__name__, str(err))
75107
return response
@@ -88,7 +120,8 @@ def _add_cookies(cookies, data):
88120
elif type(cookies) == list:
89121
for i in cookies:
90122
if not len(cookies) % 2 == 0:
91-
raise AttributeError("List cookies length must be even numbers")
123+
raise AttributeError(
124+
"List cookies length must be even numbers")
92125
if cookies.index(i) % 2 == 0:
93126
str_cookies += "{}=".format(i)
94127
elif cookies[cookies.index(i)] == cookies[-1]:

capmonster_python/compleximage.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from .capmonster import UserAgent
2+
3+
class ComplexImageTask(UserAgent):
4+
def __init__(self, client_key):
5+
super(ComplexImageTask, self).__init__(client_key)
6+
7+
def create_task(self, _class: str, grid: str = None,
8+
task_definition: str = None,
9+
image_urls: list = None,
10+
images_base64: list = None,
11+
task: str = None,
12+
websiteUrl: str = None):
13+
if _class is not "recaptcha" or _class is not "hcaptcha":
14+
raise ValueError("Currently only recaptcha or hcaptcha is supported as _class value.")
15+
data = {
16+
"clientKey": self._client_key,
17+
"task": {
18+
"type": "ComplexImageTask",
19+
"class": _class,
20+
"metadata": {}
21+
}
22+
}
23+
if image_urls is not None:
24+
data["task"]["imageUrls"] = image_urls
25+
elif images_base64 is not None:
26+
data["task"]["imagesBase64"] = images_base64
27+
else:
28+
raise ValueError("image_urls or images_base64 must be sent")
29+
if _class is "recaptcha":
30+
if grid is None:
31+
raise ValueError("Grid parameter must sent with recaptcha")
32+
else:
33+
data["task"]["metadata"]["Grid"] = grid
34+
if task is not None:
35+
data["task"]["metadata"]["Task"] = task
36+
elif task_definition is not None:
37+
data["task"]["metadata"]["TaskDefinition"] = task_definition
38+
else:
39+
raise ValueError("task_definition or task parameter must be sent")
40+
elif _class is "hcaptcha":
41+
if task is not None:
42+
data["task"]["metadata"]["Task"] = task
43+
else:
44+
raise ValueError("task parameter must be sent with hcaptcha")
45+
if websiteUrl is not None:
46+
data["task"]["websiteUrl"] = websiteUrl
47+
data, is_user_agent = self._add_user_agent(data)
48+
return self._make_request("createTask", data).get("taskId")

setup.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77

88
setup(
99
name="capmonster_python",
10-
version="2.3",
10+
version="2.4.1",
1111
packages=["capmonster_python"],
1212
url="https://github.com/alperensert/capmonster_python",
1313
long_description=long_description,
1414
long_description_content_type="text/markdown",
1515
license="MIT",
1616
author="Alperen Sert",
17-
author_email="mail@alperenn.com",
17+
author_email="business@alperen.io",
1818
description="capmonster.cloud library for Python",
1919
requires=["requests"],
2020
classifiers=[
@@ -27,6 +27,8 @@
2727
"Programming Language :: Python :: 3.7",
2828
"Programming Language :: Python :: 3.8",
2929
"Programming Language :: Python :: 3.9",
30+
"Programming Language :: Python :: 3.10",
31+
"Programming Language :: Python :: 3.11",
3032
],
3133
use_scm_version=True,
3234
setup_requires=["setuptools_scm", "wheel"],
@@ -37,4 +39,4 @@
3739
"Source": 'https://github.com/alperensert/capmonster_python/',
3840
"Tracker": 'https://github.com/alperensert/capmonster_python/issues',
3941
},
40-
)
42+
)

0 commit comments

Comments
 (0)