capmonster-python 3.2.0__py3-none-any.whl → 5.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- capmonster_python/__init__.py +47 -13
- capmonster_python/client.py +363 -0
- capmonster_python/exceptions.py +60 -0
- capmonster_python/methods.py +37 -0
- capmonster_python/tasks/__init__.py +66 -0
- capmonster_python/tasks/altcha.py +48 -0
- capmonster_python/tasks/amazon.py +54 -0
- capmonster_python/tasks/basilisk.py +24 -0
- capmonster_python/tasks/binance.py +31 -0
- capmonster_python/tasks/castle.py +46 -0
- capmonster_python/tasks/complex_image_recaptcha.py +73 -0
- capmonster_python/tasks/complex_image_recognition.py +33 -0
- capmonster_python/tasks/datadome.py +62 -0
- capmonster_python/tasks/funcaptcha.py +40 -0
- capmonster_python/tasks/geetest.py +86 -0
- capmonster_python/tasks/hunt.py +27 -0
- capmonster_python/tasks/image_to_text.py +37 -0
- capmonster_python/tasks/imperva.py +50 -0
- capmonster_python/tasks/mtcaptcha.py +36 -0
- capmonster_python/tasks/prosopo.py +27 -0
- capmonster_python/tasks/recaptcha_click.py +31 -0
- capmonster_python/tasks/recaptcha_v2.py +44 -0
- capmonster_python/tasks/recaptcha_v2_enterprise.py +44 -0
- capmonster_python/tasks/recaptcha_v3.py +34 -0
- capmonster_python/tasks/recaptcha_v3_enterprise.py +30 -0
- capmonster_python/tasks/task.py +146 -0
- capmonster_python/tasks/temu.py +32 -0
- capmonster_python/tasks/tendi.py +30 -0
- capmonster_python/tasks/tspd.py +44 -0
- capmonster_python/tasks/turnstile.py +111 -0
- capmonster_python/tasks/yidun.py +49 -0
- capmonster_python-5.0.0.dist-info/METADATA +232 -0
- capmonster_python-5.0.0.dist-info/RECORD +36 -0
- {capmonster_python-3.2.0.dist-info → capmonster_python-5.0.0.dist-info}/WHEEL +1 -1
- capmonster_python/aws_waf.py +0 -22
- capmonster_python/capmonster.py +0 -253
- capmonster_python/compleximage.py +0 -51
- capmonster_python/datadome.py +0 -19
- capmonster_python/fun_captcha.py +0 -30
- capmonster_python/geetest.py +0 -26
- capmonster_python/hcaptcha.py +0 -36
- capmonster_python/image_to_text.py +0 -43
- capmonster_python/recaptcha_v2.py +0 -27
- capmonster_python/recaptcha_v2_enterprise.py +0 -30
- capmonster_python/recaptcha_v3.py +0 -24
- capmonster_python/tendi.py +0 -19
- capmonster_python/turnstile.py +0 -23
- capmonster_python/utils.py +0 -35
- capmonster_python-3.2.0.dist-info/METADATA +0 -120
- capmonster_python-3.2.0.dist-info/RECORD +0 -20
- {capmonster_python-3.2.0.dist-info → capmonster_python-5.0.0.dist-info/licenses}/LICENSE +0 -0
- {capmonster_python-3.2.0.dist-info → capmonster_python-5.0.0.dist-info}/top_level.txt +0 -0
capmonster_python/__init__.py
CHANGED
|
@@ -1,13 +1,47 @@
|
|
|
1
|
-
from .
|
|
2
|
-
from .
|
|
3
|
-
from .
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
1
|
+
from .client import CapmonsterClient
|
|
2
|
+
from .exceptions import *
|
|
3
|
+
from .tasks import *
|
|
4
|
+
|
|
5
|
+
__all__ = [
|
|
6
|
+
"CapmonsterClient",
|
|
7
|
+
"VanillaTaskPayload",
|
|
8
|
+
"ProxyPayload",
|
|
9
|
+
"UserAgentPayload",
|
|
10
|
+
"AltchaTask",
|
|
11
|
+
"AltchaMetadata",
|
|
12
|
+
"AmazonTask",
|
|
13
|
+
"BasiliskTask",
|
|
14
|
+
"BinanceTask",
|
|
15
|
+
"CastleTask",
|
|
16
|
+
"CastleMetadata",
|
|
17
|
+
"ComplexImageRecaptchaTask",
|
|
18
|
+
"ComplexImageRecaptchaMetadata",
|
|
19
|
+
"ComplexImageRecognitionTask",
|
|
20
|
+
"DataDomeTask",
|
|
21
|
+
"DataDomeMetadata",
|
|
22
|
+
"FunCaptchaTask",
|
|
23
|
+
"GeeTestV3Task",
|
|
24
|
+
"GeeTestV4Task",
|
|
25
|
+
"HuntTask",
|
|
26
|
+
"ImageToTextTask",
|
|
27
|
+
"ImpervaTask",
|
|
28
|
+
"ImpervaTaskMetadata",
|
|
29
|
+
"MTCaptchaTask",
|
|
30
|
+
"ProsopoTask",
|
|
31
|
+
"RecaptchaClickTask",
|
|
32
|
+
"RecaptchaV2Task",
|
|
33
|
+
"RecaptchaV2EnterpriseTask",
|
|
34
|
+
"RecaptchaV3Task",
|
|
35
|
+
"RecaptchaV3EnterpriseTask",
|
|
36
|
+
"TemuTask",
|
|
37
|
+
"TenDITask",
|
|
38
|
+
"TSPDTask",
|
|
39
|
+
"TSPDMetadata",
|
|
40
|
+
"TurnstileTask",
|
|
41
|
+
"TurnstileCloudFlareTask",
|
|
42
|
+
"TurnstileWaitingRoomTask",
|
|
43
|
+
"YidunTask",
|
|
44
|
+
"CapmonsterException",
|
|
45
|
+
"CapmonsterAPIException",
|
|
46
|
+
"CapmonsterValidationException"
|
|
47
|
+
]
|
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
from time import sleep
|
|
3
|
+
from typing import Optional
|
|
4
|
+
|
|
5
|
+
from httpx import Response, AsyncClient, Client
|
|
6
|
+
|
|
7
|
+
from .exceptions import CapmonsterAPIException, CapmonsterException
|
|
8
|
+
from .methods import GetBalancePayload, GetTaskResultPayload, CreateTaskPayload, ReportIncorrectCaptchaPayload
|
|
9
|
+
from .tasks.task import TaskPayload
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class CapmonsterClient:
|
|
13
|
+
__BASE_URL = "https://api.capmonster.cloud"
|
|
14
|
+
__BALANCE_URL = "/getBalance"
|
|
15
|
+
__TASK_RESULT_URL = "/getTaskResult"
|
|
16
|
+
__CREATE_TASK_URL = "/createTask"
|
|
17
|
+
|
|
18
|
+
__REPORT_IMAGE_URL = "/reportIncorrectImageCaptcha"
|
|
19
|
+
__REPORT_TOKEN_URL = "/reportIncorrectTokenCaptcha"
|
|
20
|
+
__USER_AGENT_URL = "https://capmonster.cloud/api/useragent/actual"
|
|
21
|
+
|
|
22
|
+
def __init__(self, api_key: str, timeout: Optional[float] = 30.0,
|
|
23
|
+
max_retries: int = 120, retry_delay: float = 2.0) -> None:
|
|
24
|
+
self.api_key = api_key
|
|
25
|
+
self.__max_retries = max_retries
|
|
26
|
+
self.__retry_delay = retry_delay
|
|
27
|
+
self.__async_client = AsyncClient(timeout=timeout, base_url=self.__BASE_URL)
|
|
28
|
+
self.__sync_client = Client(timeout=timeout, base_url=self.__BASE_URL)
|
|
29
|
+
|
|
30
|
+
def __enter__(self) -> "CapmonsterClient":
|
|
31
|
+
return self
|
|
32
|
+
|
|
33
|
+
def __exit__(self, *args) -> None:
|
|
34
|
+
self.__sync_client.close()
|
|
35
|
+
|
|
36
|
+
async def __aenter__(self) -> "CapmonsterClient":
|
|
37
|
+
return self
|
|
38
|
+
|
|
39
|
+
async def __aexit__(self, *args) -> None:
|
|
40
|
+
await self.__async_client.aclose()
|
|
41
|
+
|
|
42
|
+
def get_balance(self) -> float:
|
|
43
|
+
"""
|
|
44
|
+
Fetches the current balance using the provided API key.
|
|
45
|
+
|
|
46
|
+
Raises:
|
|
47
|
+
This method may raise exceptions if the HTTP request fails or the JSON response
|
|
48
|
+
format is unexpected. These exceptions are not handled within the method.
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
float: The balance fetched from the external service. Defaults to 0.0 if the balance
|
|
52
|
+
key is not found in the response.
|
|
53
|
+
"""
|
|
54
|
+
payload = GetBalancePayload(clientKey=self.api_key).model_dump()
|
|
55
|
+
response = self.__make_sync_request(self.__BALANCE_URL, payload).json()
|
|
56
|
+
return response.get("balance", 0.0)
|
|
57
|
+
|
|
58
|
+
async def get_balance_async(self) -> float:
|
|
59
|
+
"""
|
|
60
|
+
Asynchronously fetches the current balance using the provided API key.
|
|
61
|
+
|
|
62
|
+
Raises:
|
|
63
|
+
This method may raise exceptions if the HTTP request fails or the JSON response
|
|
64
|
+
format is unexpected. These exceptions are not handled within the method.
|
|
65
|
+
|
|
66
|
+
Returns:
|
|
67
|
+
float: The balance fetched from the external service. Defaults to 0.0 if the balance
|
|
68
|
+
key is not found in the response.
|
|
69
|
+
"""
|
|
70
|
+
payload = GetBalancePayload(clientKey=self.api_key).model_dump()
|
|
71
|
+
response = await self.__make_async_request(self.__BALANCE_URL, payload)
|
|
72
|
+
return response.json().get("balance", 0.0)
|
|
73
|
+
|
|
74
|
+
def create_task(self, task: TaskPayload, callback_url: Optional[str] = None) -> int:
|
|
75
|
+
"""
|
|
76
|
+
Creates a task based on the provided payload and returns the task ID.
|
|
77
|
+
|
|
78
|
+
This method is responsible for initiating a task by preparing the request with the
|
|
79
|
+
provided task details and optional callback URL, sending a synchronous request to the
|
|
80
|
+
task creation endpoint, and parsing the response to retrieve the task ID.
|
|
81
|
+
|
|
82
|
+
Args:
|
|
83
|
+
task: The task configuration payload that defines the specifics of the task to be
|
|
84
|
+
created.
|
|
85
|
+
callback_url: An optional URL to be called back upon task completion or update.
|
|
86
|
+
|
|
87
|
+
Returns:
|
|
88
|
+
The unique identifier of the created task.
|
|
89
|
+
|
|
90
|
+
Raises:
|
|
91
|
+
CapmonsterException: If the task creation request fails or the response is not contains valid task id.
|
|
92
|
+
"""
|
|
93
|
+
payload = CreateTaskPayload(clientKey=self.api_key, task=task, callbackUrl=callback_url).model_dump(
|
|
94
|
+
exclude_none=True)
|
|
95
|
+
payload["softId"] = 30
|
|
96
|
+
response = self.__make_sync_request(self.__CREATE_TASK_URL, payload).json()
|
|
97
|
+
return response.get("taskId", 0)
|
|
98
|
+
|
|
99
|
+
async def create_task_async(self, task: TaskPayload, callback_url: Optional[str] = None) -> int:
|
|
100
|
+
"""
|
|
101
|
+
Asynchronously creates a task based on the provided payload and returns the task ID.
|
|
102
|
+
|
|
103
|
+
This method is responsible for initiating a task by preparing the request with the
|
|
104
|
+
provided task details and optional callback URL, sending an asynchronous request to the
|
|
105
|
+
task creation endpoint, and parsing the response to retrieve the task ID.
|
|
106
|
+
|
|
107
|
+
Args:
|
|
108
|
+
task: The task configuration payload that defines the specifics of the task to be
|
|
109
|
+
created.
|
|
110
|
+
callback_url: An optional URL to be called back upon task completion or update.
|
|
111
|
+
|
|
112
|
+
Returns:
|
|
113
|
+
The unique identifier of the created task.
|
|
114
|
+
|
|
115
|
+
Raises:
|
|
116
|
+
CapmonsterException: If the task creation request fails or the response is not contains valid task id.
|
|
117
|
+
"""
|
|
118
|
+
payload = CreateTaskPayload(clientKey=self.api_key, task=task, callbackUrl=callback_url).model_dump(
|
|
119
|
+
exclude_none=True)
|
|
120
|
+
payload["softId"] = 30
|
|
121
|
+
request = await self.__make_async_request(self.__CREATE_TASK_URL, payload)
|
|
122
|
+
response = request.json()
|
|
123
|
+
return response.get("taskId", 0)
|
|
124
|
+
|
|
125
|
+
def get_task_result(self, task_id: int) -> dict:
|
|
126
|
+
"""
|
|
127
|
+
Fetches the result of a specific task by its identifier.
|
|
128
|
+
|
|
129
|
+
This function interacts with API to retrieve the results of a
|
|
130
|
+
previously submitted task. The function uses the provided task ID and
|
|
131
|
+
sends a request to the API endpoint to get the result. It returns the
|
|
132
|
+
solution data extracted from the API response or an empty dictionary if
|
|
133
|
+
no solution data is found.
|
|
134
|
+
|
|
135
|
+
Args:
|
|
136
|
+
task_id (int): An integer representing the unique identifier of the task.
|
|
137
|
+
|
|
138
|
+
Returns:
|
|
139
|
+
dict: A dictionary containing the solution data retrieved from the task
|
|
140
|
+
result API endpoint. If the solution is not found in the response, an empty
|
|
141
|
+
dictionary is returned.
|
|
142
|
+
"""
|
|
143
|
+
payload = GetTaskResultPayload(clientKey=self.api_key, taskId=task_id).model_dump()
|
|
144
|
+
response = self.__make_sync_request(self.__TASK_RESULT_URL, payload).json()
|
|
145
|
+
return response.get("solution", {})
|
|
146
|
+
|
|
147
|
+
async def get_task_result_async(self, task_id: int) -> dict:
|
|
148
|
+
"""
|
|
149
|
+
Asynchronously fetches the result of a specific task by its identifier.
|
|
150
|
+
|
|
151
|
+
This function interacts with API to retrieve the results of a
|
|
152
|
+
previously submitted task. The function uses the provided task ID and
|
|
153
|
+
sends a request to the API endpoint to get the result. It returns the
|
|
154
|
+
solution data extracted from the API response or an empty dictionary if
|
|
155
|
+
no solution data is found.
|
|
156
|
+
|
|
157
|
+
Args:
|
|
158
|
+
task_id (int): An integer representing the unique identifier of the task.
|
|
159
|
+
|
|
160
|
+
Returns:
|
|
161
|
+
dict: A dictionary containing the solution data retrieved from the task
|
|
162
|
+
result API endpoint. If the solution is not found in the response, an empty
|
|
163
|
+
dictionary is returned.
|
|
164
|
+
"""
|
|
165
|
+
payload = GetTaskResultPayload(clientKey=self.api_key, taskId=task_id).model_dump()
|
|
166
|
+
request = await self.__make_async_request(self.__TASK_RESULT_URL, payload)
|
|
167
|
+
response = request.json()
|
|
168
|
+
return response.get("solution", {})
|
|
169
|
+
|
|
170
|
+
def join_task_result(self, task_id: int) -> dict:
|
|
171
|
+
"""
|
|
172
|
+
Joins and retrieves the result of a task given its ID, retrying a specified number
|
|
173
|
+
of times if the result is not immediately available.
|
|
174
|
+
|
|
175
|
+
Args:
|
|
176
|
+
task_id: The identifier of the task for which the result needs to be retrieved.
|
|
177
|
+
|
|
178
|
+
Returns:
|
|
179
|
+
dict:
|
|
180
|
+
The result of the task if successfully retrieved within the maximum retries.
|
|
181
|
+
|
|
182
|
+
Raises:
|
|
183
|
+
CapmonsterException:
|
|
184
|
+
If the maximum retry limit is exceeded without obtaining the task result.
|
|
185
|
+
"""
|
|
186
|
+
for _ in range(0, self.__max_retries + 1):
|
|
187
|
+
result = self.get_task_result(task_id)
|
|
188
|
+
if result:
|
|
189
|
+
return result
|
|
190
|
+
elif not result:
|
|
191
|
+
sleep(self.__retry_delay)
|
|
192
|
+
raise CapmonsterAPIException(
|
|
193
|
+
61, "ERROR_MAXIMUM_TIME_EXCEED", "Maximum time is exceed.")
|
|
194
|
+
|
|
195
|
+
async def join_task_result_async(self, task_id: int) -> dict:
|
|
196
|
+
"""
|
|
197
|
+
Asynchronously joins and retrieves the result of a task given its ID, retrying a specified number
|
|
198
|
+
of times if the result is not immediately available.
|
|
199
|
+
|
|
200
|
+
Args:
|
|
201
|
+
task_id: The identifier of the task for which the result needs to be retrieved.
|
|
202
|
+
|
|
203
|
+
Returns:
|
|
204
|
+
dict:
|
|
205
|
+
The result of the task if successfully retrieved within the maximum retries.
|
|
206
|
+
|
|
207
|
+
Raises:
|
|
208
|
+
CapmonsterException:
|
|
209
|
+
If the maximum retry limit is exceeded without obtaining the task result.
|
|
210
|
+
"""
|
|
211
|
+
for _ in range(0, self.__max_retries + 1):
|
|
212
|
+
result = await self.get_task_result_async(task_id)
|
|
213
|
+
if result:
|
|
214
|
+
return result
|
|
215
|
+
elif not result:
|
|
216
|
+
await asyncio.sleep(self.__retry_delay)
|
|
217
|
+
raise CapmonsterAPIException(
|
|
218
|
+
61, "ERROR_MAXIMUM_TIME_EXCEED", "Maximum time is exceed.")
|
|
219
|
+
|
|
220
|
+
def solve(self, task: TaskPayload, callback_url: Optional[str] = None) -> dict:
|
|
221
|
+
"""
|
|
222
|
+
Convenience method that creates a task and polls for its result.
|
|
223
|
+
|
|
224
|
+
Equivalent to calling create_task() followed by join_task_result().
|
|
225
|
+
|
|
226
|
+
Args:
|
|
227
|
+
task: The task configuration payload.
|
|
228
|
+
callback_url: An optional callback URL for task completion.
|
|
229
|
+
|
|
230
|
+
Returns:
|
|
231
|
+
dict: The solution dictionary from the completed task.
|
|
232
|
+
"""
|
|
233
|
+
task_id = self.create_task(task, callback_url)
|
|
234
|
+
return self.join_task_result(task_id)
|
|
235
|
+
|
|
236
|
+
async def solve_async(self, task: TaskPayload, callback_url: Optional[str] = None) -> dict:
|
|
237
|
+
"""
|
|
238
|
+
Async convenience method that creates a task and polls for its result.
|
|
239
|
+
|
|
240
|
+
Equivalent to calling create_task_async() followed by join_task_result_async().
|
|
241
|
+
|
|
242
|
+
Args:
|
|
243
|
+
task: The task configuration payload.
|
|
244
|
+
callback_url: An optional callback URL for task completion.
|
|
245
|
+
|
|
246
|
+
Returns:
|
|
247
|
+
dict: The solution dictionary from the completed task.
|
|
248
|
+
"""
|
|
249
|
+
task_id = await self.create_task_async(task, callback_url)
|
|
250
|
+
return await self.join_task_result_async(task_id)
|
|
251
|
+
|
|
252
|
+
def report_incorrect_image(self, task_id: int) -> None:
|
|
253
|
+
"""
|
|
254
|
+
Reports an incorrect image captcha solution.
|
|
255
|
+
|
|
256
|
+
Args:
|
|
257
|
+
task_id: The identifier of the task to report.
|
|
258
|
+
|
|
259
|
+
Raises:
|
|
260
|
+
CapmonsterAPIException: If the API returns an error.
|
|
261
|
+
"""
|
|
262
|
+
payload = ReportIncorrectCaptchaPayload(clientKey=self.api_key, taskId=task_id).model_dump()
|
|
263
|
+
self.__make_sync_request(self.__REPORT_IMAGE_URL, payload)
|
|
264
|
+
|
|
265
|
+
async def report_incorrect_image_async(self, task_id: int) -> None:
|
|
266
|
+
"""
|
|
267
|
+
Asynchronously reports an incorrect image captcha solution.
|
|
268
|
+
|
|
269
|
+
Args:
|
|
270
|
+
task_id: The identifier of the task to report.
|
|
271
|
+
|
|
272
|
+
Raises:
|
|
273
|
+
CapmonsterAPIException: If the API returns an error.
|
|
274
|
+
"""
|
|
275
|
+
payload = ReportIncorrectCaptchaPayload(clientKey=self.api_key, taskId=task_id).model_dump()
|
|
276
|
+
await self.__make_async_request(self.__REPORT_IMAGE_URL, payload)
|
|
277
|
+
|
|
278
|
+
def report_incorrect_token(self, task_id: int) -> None:
|
|
279
|
+
"""
|
|
280
|
+
Reports an incorrect token captcha solution (reCAPTCHA, GeeTest, Turnstile, etc.).
|
|
281
|
+
|
|
282
|
+
Args:
|
|
283
|
+
task_id: The identifier of the task to report.
|
|
284
|
+
|
|
285
|
+
Raises:
|
|
286
|
+
CapmonsterAPIException: If the API returns an error.
|
|
287
|
+
"""
|
|
288
|
+
payload = ReportIncorrectCaptchaPayload(clientKey=self.api_key, taskId=task_id).model_dump()
|
|
289
|
+
self.__make_sync_request(self.__REPORT_TOKEN_URL, payload)
|
|
290
|
+
|
|
291
|
+
async def report_incorrect_token_async(self, task_id: int) -> None:
|
|
292
|
+
"""
|
|
293
|
+
Asynchronously reports an incorrect token captcha solution (reCAPTCHA, GeeTest, Turnstile, etc.).
|
|
294
|
+
|
|
295
|
+
Args:
|
|
296
|
+
task_id: The identifier of the task to report.
|
|
297
|
+
|
|
298
|
+
Raises:
|
|
299
|
+
CapmonsterAPIException: If the API returns an error.
|
|
300
|
+
"""
|
|
301
|
+
payload = ReportIncorrectCaptchaPayload(clientKey=self.api_key, taskId=task_id).model_dump()
|
|
302
|
+
await self.__make_async_request(self.__REPORT_TOKEN_URL, payload)
|
|
303
|
+
|
|
304
|
+
def get_user_agent(self) -> str:
|
|
305
|
+
"""
|
|
306
|
+
Fetches the current valid Windows User-Agent string from CapMonster Cloud.
|
|
307
|
+
|
|
308
|
+
Returns:
|
|
309
|
+
str: The current User-Agent string to use with captcha tasks.
|
|
310
|
+
"""
|
|
311
|
+
try:
|
|
312
|
+
response = self.__sync_client.get(self.__USER_AGENT_URL)
|
|
313
|
+
return response.text
|
|
314
|
+
except Exception as e:
|
|
315
|
+
raise CapmonsterException(-1, type(e).__name__, str(e))
|
|
316
|
+
|
|
317
|
+
async def get_user_agent_async(self) -> str:
|
|
318
|
+
"""
|
|
319
|
+
Asynchronously fetches the current valid Windows User-Agent string from CapMonster Cloud.
|
|
320
|
+
|
|
321
|
+
Returns:
|
|
322
|
+
str: The current User-Agent string to use with captcha tasks.
|
|
323
|
+
"""
|
|
324
|
+
try:
|
|
325
|
+
response = await self.__async_client.get(self.__USER_AGENT_URL)
|
|
326
|
+
return response.text
|
|
327
|
+
except Exception as e:
|
|
328
|
+
raise CapmonsterException(-1, type(e).__name__, str(e))
|
|
329
|
+
|
|
330
|
+
def __make_sync_request(self, url: str, payload: dict) -> Response:
|
|
331
|
+
try:
|
|
332
|
+
response = self.__sync_client.post(url, json=payload)
|
|
333
|
+
return self.__check_response(response)
|
|
334
|
+
except CapmonsterException:
|
|
335
|
+
raise
|
|
336
|
+
except Exception as e:
|
|
337
|
+
raise CapmonsterException(-1, type(e).__name__, str(e))
|
|
338
|
+
|
|
339
|
+
async def __make_async_request(self, url: str, payload: dict) -> Response:
|
|
340
|
+
try:
|
|
341
|
+
post = await self.__async_client.post(url, json=payload)
|
|
342
|
+
return self.__check_response(post)
|
|
343
|
+
except CapmonsterException:
|
|
344
|
+
raise
|
|
345
|
+
except Exception as e:
|
|
346
|
+
raise CapmonsterException(-1, type(e).__name__, str(e))
|
|
347
|
+
|
|
348
|
+
@staticmethod
|
|
349
|
+
def __check_response(data: Response) -> Response:
|
|
350
|
+
response = data.json()
|
|
351
|
+
if type(response) is dict:
|
|
352
|
+
if response.get("errorId", 0) != 0:
|
|
353
|
+
raise CapmonsterAPIException(
|
|
354
|
+
response.get("errorId"),
|
|
355
|
+
response.get("errorCode", response.get("errorCode", "UNKNOWN_ERROR")),
|
|
356
|
+
response.get("errorDescription", response.get("errorDescription", "Unknown error"))
|
|
357
|
+
)
|
|
358
|
+
return data
|
|
359
|
+
else:
|
|
360
|
+
raise CapmonsterAPIException(error_id=-1,
|
|
361
|
+
error_code="CAPMONSTER_API_ERROR",
|
|
362
|
+
error_description="Sometimes can be happen if Capmonster "
|
|
363
|
+
"servers there is too much intensity")
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
class CapmonsterException(Exception):
|
|
2
|
+
"""
|
|
3
|
+
Generic exception class for all errors.
|
|
4
|
+
|
|
5
|
+
Notes:
|
|
6
|
+
This will be raised for errors that are not caught elsewhere.
|
|
7
|
+
These errors can be related HTTP requests, API responses, or other issues.
|
|
8
|
+
"""
|
|
9
|
+
pass
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class CapmonsterAPIException(CapmonsterException):
|
|
13
|
+
"""
|
|
14
|
+
Raised when the Capmonster API returns an error.
|
|
15
|
+
|
|
16
|
+
Attributes:
|
|
17
|
+
error_id: The error ID returned by the API.
|
|
18
|
+
error_code: The error code returned by the API.
|
|
19
|
+
error_description: The error description returned by the API.
|
|
20
|
+
|
|
21
|
+
Notes:
|
|
22
|
+
- Error codes are defined in the API documentation.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
def __init__(self, error_id, error_code, error_description, *args, **kwargs):
|
|
26
|
+
super(CapmonsterAPIException, self).__init__("[{}:{}]{}".format(error_code, error_id, error_description))
|
|
27
|
+
self.error_description = error_description
|
|
28
|
+
self.error_id = error_id
|
|
29
|
+
self.error_code = error_code
|
|
30
|
+
|
|
31
|
+
def __str__(self):
|
|
32
|
+
return "[{}] {}".format(self.error_code, self.error_description)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class CapmonsterValidationException(CapmonsterException):
|
|
36
|
+
"""
|
|
37
|
+
Raised when a Capmonster task configuration is invalid.
|
|
38
|
+
|
|
39
|
+
Attributes:
|
|
40
|
+
message: The error message describing the invalid configuration.
|
|
41
|
+
field: The field in the configuration that caused the error, if applicable.
|
|
42
|
+
task: The task type that caused the error, if applicable.
|
|
43
|
+
|
|
44
|
+
Notes:
|
|
45
|
+
- This error only raises when the validation fails within the SDK task creation.
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
def __init__(self, message: str, *, field: str = None, task: str = None):
|
|
49
|
+
self.message = message
|
|
50
|
+
self.field = field
|
|
51
|
+
self.task = task
|
|
52
|
+
super().__init__(self._format_message())
|
|
53
|
+
|
|
54
|
+
def _format_message(self) -> str:
|
|
55
|
+
base = f"[CapmonsterValidationError] {self.message}"
|
|
56
|
+
if self.field:
|
|
57
|
+
base += f" (field: {self.field})"
|
|
58
|
+
if self.task:
|
|
59
|
+
base += f" [task: {self.task}]"
|
|
60
|
+
return base
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
|
|
3
|
+
from pydantic import BaseModel, Field, field_serializer
|
|
4
|
+
|
|
5
|
+
from .tasks.task import TaskPayload
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class GetBalancePayload(BaseModel):
|
|
9
|
+
"""
|
|
10
|
+
Represents a payload for retrieving the balance.
|
|
11
|
+
|
|
12
|
+
Attributes:
|
|
13
|
+
clientKey (str): The key representing the client, used for identifying
|
|
14
|
+
the client in balance-related operations.
|
|
15
|
+
"""
|
|
16
|
+
clientKey: str = Field(..., description="Client key")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class GetTaskResultPayload(BaseModel):
|
|
20
|
+
clientKey: str = Field(..., description="Client key")
|
|
21
|
+
taskId: int = Field(..., description="ID which was obtained from create task method.")
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class ReportIncorrectCaptchaPayload(BaseModel):
|
|
25
|
+
clientKey: str = Field(..., description="Client key")
|
|
26
|
+
taskId: int = Field(..., description="ID of the task to report as incorrect.")
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class CreateTaskPayload(BaseModel):
|
|
30
|
+
clientKey: str = Field(..., description="Client key")
|
|
31
|
+
task: TaskPayload = Field(..., description="Task data")
|
|
32
|
+
callbackUrl: Optional[str] = Field(default=None,
|
|
33
|
+
description="Web address for sending the captcha task result. Data is sent by POST request.")
|
|
34
|
+
|
|
35
|
+
@field_serializer("task")
|
|
36
|
+
def serialize_task(self, task: TaskPayload, _info) -> dict:
|
|
37
|
+
return task.to_request()
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
from .altcha import AltchaTask, AltchaMetadata
|
|
2
|
+
from .amazon import AmazonTask
|
|
3
|
+
from .basilisk import BasiliskTask
|
|
4
|
+
from .binance import BinanceTask
|
|
5
|
+
from .castle import CastleTask, CastleMetadata
|
|
6
|
+
from .complex_image_recaptcha import ComplexImageRecaptchaTask, ComplexImageRecaptchaMetadata
|
|
7
|
+
from .complex_image_recognition import ComplexImageRecognitionTask
|
|
8
|
+
from .datadome import DataDomeTask, DataDomeMetadata
|
|
9
|
+
from .funcaptcha import FunCaptchaTask
|
|
10
|
+
from .geetest import GeeTestV4Task, GeeTestV3Task
|
|
11
|
+
from .hunt import HuntTask
|
|
12
|
+
from .image_to_text import ImageToTextTask
|
|
13
|
+
from .imperva import ImpervaTask, ImpervaTaskMetadata
|
|
14
|
+
from .mtcaptcha import MTCaptchaTask
|
|
15
|
+
from .prosopo import ProsopoTask
|
|
16
|
+
from .recaptcha_click import RecaptchaClickTask
|
|
17
|
+
from .recaptcha_v2 import RecaptchaV2Task
|
|
18
|
+
from .recaptcha_v2_enterprise import RecaptchaV2EnterpriseTask
|
|
19
|
+
from .recaptcha_v3 import RecaptchaV3Task
|
|
20
|
+
from .recaptcha_v3_enterprise import RecaptchaV3EnterpriseTask
|
|
21
|
+
from .task import VanillaTaskPayload, ProxyPayload, UserAgentPayload
|
|
22
|
+
from .temu import TemuTask
|
|
23
|
+
from .tendi import TenDITask
|
|
24
|
+
from .tspd import TSPDTask, TSPDMetadata
|
|
25
|
+
from .turnstile import TurnstileTask, TurnstileCloudFlareTask, TurnstileWaitingRoomTask
|
|
26
|
+
from .yidun import YidunTask
|
|
27
|
+
|
|
28
|
+
__all__ = [
|
|
29
|
+
"VanillaTaskPayload",
|
|
30
|
+
"ProxyPayload",
|
|
31
|
+
"UserAgentPayload",
|
|
32
|
+
"AltchaTask",
|
|
33
|
+
"AltchaMetadata",
|
|
34
|
+
"AmazonTask",
|
|
35
|
+
"BasiliskTask",
|
|
36
|
+
"BinanceTask",
|
|
37
|
+
"CastleTask",
|
|
38
|
+
"CastleMetadata",
|
|
39
|
+
"ComplexImageRecaptchaTask",
|
|
40
|
+
"ComplexImageRecaptchaMetadata",
|
|
41
|
+
"ComplexImageRecognitionTask",
|
|
42
|
+
"DataDomeTask",
|
|
43
|
+
"DataDomeMetadata",
|
|
44
|
+
"FunCaptchaTask",
|
|
45
|
+
"GeeTestV3Task",
|
|
46
|
+
"GeeTestV4Task",
|
|
47
|
+
"HuntTask",
|
|
48
|
+
"ImageToTextTask",
|
|
49
|
+
"ImpervaTask",
|
|
50
|
+
"ImpervaTaskMetadata",
|
|
51
|
+
"MTCaptchaTask",
|
|
52
|
+
"ProsopoTask",
|
|
53
|
+
"RecaptchaClickTask",
|
|
54
|
+
"RecaptchaV2Task",
|
|
55
|
+
"RecaptchaV2EnterpriseTask",
|
|
56
|
+
"RecaptchaV3Task",
|
|
57
|
+
"RecaptchaV3EnterpriseTask",
|
|
58
|
+
"TemuTask",
|
|
59
|
+
"TenDITask",
|
|
60
|
+
"TSPDTask",
|
|
61
|
+
"TSPDMetadata",
|
|
62
|
+
"TurnstileTask",
|
|
63
|
+
"TurnstileCloudFlareTask",
|
|
64
|
+
"TurnstileWaitingRoomTask",
|
|
65
|
+
"YidunTask"
|
|
66
|
+
]
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
from typing import Any, Optional
|
|
2
|
+
|
|
3
|
+
from pydantic import Field, BaseModel
|
|
4
|
+
|
|
5
|
+
from .task import TaskPayload, ProxyPayload, UserAgentPayload
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class AltchaMetadata(BaseModel):
|
|
9
|
+
"""
|
|
10
|
+
Metadata for Altcha task.
|
|
11
|
+
|
|
12
|
+
Attributes:
|
|
13
|
+
challenge: Unique task identifier obtained from the website.
|
|
14
|
+
iterations: Number of iterations (corresponds to maxnumber value).
|
|
15
|
+
salt: Salt obtained from the site, used for hash generation.
|
|
16
|
+
signature: Digital signature of the request.
|
|
17
|
+
"""
|
|
18
|
+
challenge: str = Field(..., description='Unique task identifier obtained from the website.')
|
|
19
|
+
iterations: str = Field(..., description='Number of iterations (corresponds to maxnumber value).')
|
|
20
|
+
salt: str = Field(..., description='Salt obtained from the site, used for hash generation.')
|
|
21
|
+
signature: str = Field(..., description='Digital signature of the request.')
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class AltchaTask(TaskPayload, UserAgentPayload):
|
|
25
|
+
"""
|
|
26
|
+
Represents a task for solving Altcha challenges.
|
|
27
|
+
|
|
28
|
+
Uses CustomTask type with class "altcha".
|
|
29
|
+
|
|
30
|
+
Attributes:
|
|
31
|
+
websiteURL: Main page URL where Altcha is located.
|
|
32
|
+
websiteKey: For this task, sending an empty string is allowed.
|
|
33
|
+
metadata: Altcha-specific metadata (challenge, iterations, salt, signature).
|
|
34
|
+
proxy: Optional proxy settings.
|
|
35
|
+
"""
|
|
36
|
+
type: str = Field(default="CustomTask", frozen=True)
|
|
37
|
+
class_: str = Field(default="altcha", alias="class", frozen=True)
|
|
38
|
+
websiteURL: str = Field(..., description='Main page URL where Altcha is located.')
|
|
39
|
+
websiteKey: str = Field(default="", description='For this task, sending an empty string is allowed.')
|
|
40
|
+
metadata: AltchaMetadata = Field(..., description='Altcha-specific metadata.')
|
|
41
|
+
proxy: Optional[ProxyPayload] = Field(default=None, description='Proxy settings.')
|
|
42
|
+
|
|
43
|
+
def to_request(self) -> dict[str, Any]:
|
|
44
|
+
base = self.model_dump(exclude_none=True, by_alias=True)
|
|
45
|
+
proxy_dict = base.pop("proxy", {})
|
|
46
|
+
if proxy_dict:
|
|
47
|
+
base.update(proxy_dict)
|
|
48
|
+
return base
|