Skip to content

Commit 2ccb40d

Browse files
committed
DataDomeTask added
- Added detailed docstrings for all Capmonster class methods - Made syntax corrections and improvements in source code - Imported DataDomeTask in the init.py file
1 parent ee0d497 commit 2ccb40d

4 files changed

Lines changed: 66 additions & 13 deletions

File tree

capmonster_python/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
from .geetest import GeeTestTask
88
from .utils import CapmonsterException
99
from .compleximage import ComplexImageTask
10+
from .datadome import DataDomeTask

capmonster_python/capmonster.py

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66

77
class Capmonster:
8+
"""A class that interacts with the Capmonster API."""
89
__SOFT_ID = 30
910
_HOST_URL = "https://api.capmonster.cloud"
1011
_CREATE_TASK_URL = "/createTask"
@@ -16,11 +17,20 @@ class Capmonster:
1617
def __init__(self, client_key):
1718
self._client_key = client_key
1819

19-
def get_balance(self):
20+
def get_balance(self) -> float:
21+
"""
22+
Retrieves the balance of the client.
23+
24+
:return: The balance of the client as a float.
25+
"""
2026
data = {"clientKey": self._client_key}
2127
return self._make_request("getBalance", data).get("balance")
2228

2329
def get_task_result(self, task_id: int):
30+
"""
31+
:param task_id: The ID of the task for which the result is requested.
32+
:return: The result of the task if it is ready, otherwise False.
33+
"""
2434
data = {
2535
"clientKey": self._client_key,
2636
"taskId": task_id
@@ -55,7 +65,15 @@ async def join_task_result_async(self, task_id: int, maximum_time: int = 120):
5565
61, "ERROR_MAXIMUM_TIME_EXCEED", "Maximum time is exceed.")
5666

5767
def report_incorrect_captcha(self, captcha_type: str, task_id: int) -> bool:
58-
if captcha_type is not "image" or "token":
68+
"""
69+
Reports an incorrect captcha.
70+
71+
:param captcha_type: The type of captcha ("image" or "token").
72+
:param task_id: The ID of the task.
73+
:return: True if the captcha is successfully reported, False otherwise.
74+
:raises CapmonsterException: If the captcha type is invalid.
75+
"""
76+
if captcha_type != "image" or "token":
5977
raise CapmonsterException(
6078
1, "ERROR_INCORRECT_CAPTCHA_TYPE", "Valid captcha_type parameters are only 'image' or 'token'.")
6179
try:
@@ -71,7 +89,7 @@ def _report_incorrect_captcha(self, captcha_type: str, task_id: int):
7189
"clientKey": self._client_key,
7290
"taskId": task_id
7391
}
74-
if captcha_type is "image":
92+
if captcha_type == "image":
7593
response = self._make_request("reportIncorrectImageCaptcha", data)
7694
else:
7795
response = self._make_request("reportIncorrectTokenCaptcha", data)
@@ -111,13 +129,13 @@ def _add_cookies(cookies, data):
111129
if cookies is None:
112130
return data
113131
str_cookies = ""
114-
if type(cookies) == dict:
132+
if type(cookies) is dict:
115133
for key, value in cookies.items():
116134
if value == list(cookies.items())[-1][1]:
117135
str_cookies += "{}={}".format(key, value)
118136
else:
119137
str_cookies += "{}={};".format(key, value)
120-
elif type(cookies) == list:
138+
elif type(cookies) is list:
121139
for i in cookies:
122140
if not len(cookies) % 2 == 0:
123141
raise AttributeError(
@@ -128,7 +146,7 @@ def _add_cookies(cookies, data):
128146
str_cookies += "{}".format(i)
129147
elif cookies.index(i) % 2 == 1:
130148
str_cookies += "{};".format(i)
131-
elif type(cookies) == str:
149+
elif type(cookies) is str:
132150
data["task"]["cookies"] = cookies
133151
return data
134152
data["task"]["cookies"] = str_cookies
@@ -146,13 +164,28 @@ def __init__(self, client_key):
146164

147165
def set_proxy(self, proxy_type: str, proxy_address: str, proxy_port: int,
148166
proxy_login: str = None, proxy_password: str = None):
167+
"""
168+
Sets the proxy settings for the client.
169+
170+
:param proxy_type: The type of the proxy. (e.g. "HTTP", "SOCKS5")
171+
:param proxy_address: The address of the proxy server.
172+
:param proxy_port: The port number of the proxy server.
173+
:param proxy_login: (optional) The login username for proxy authentication.
174+
:param proxy_password: (optional) The login password for proxy authentication.
175+
:return: None
176+
"""
149177
self._proxy_type = proxy_type
150178
self._proxy_address = proxy_address
151179
self._proxy_port = proxy_port
152180
self._proxy_login = proxy_login
153181
self._proxy_password = proxy_password
154182

155183
def disable_proxy(self):
184+
"""
185+
Disables the proxy settings.
186+
187+
:return: None
188+
"""
156189
self._proxy_type = None
157190
self._proxy_address = None
158191
self._proxy_port = None
@@ -184,9 +217,20 @@ def __init__(self, client_key):
184217
self._fallback = False
185218

186219
def set_user_agent(self, user_agent: str):
220+
"""
221+
Set the user agent for the instance.
222+
223+
:param user_agent: The user agent string to set.
224+
:return: None
225+
"""
187226
self._user_agent = user_agent
188227

189228
def reset_user_agent(self):
229+
"""
230+
Reset the user agent to None.
231+
232+
:return:
233+
"""
190234
self._user_agent = None
191235

192236
def _add_user_agent(self, data):
@@ -197,4 +241,11 @@ def _add_user_agent(self, data):
197241
return data, False
198242

199243
def set_fallback_to_actual_user_agent(self, fallback: bool):
244+
"""
245+
Set the fallback value for the actual user agent.
246+
247+
:param fallback: A boolean value indicating whether to enable or disable the fallback.
248+
:type fallback: bool
249+
:return: None
250+
"""
200251
self._fallback = fallback

capmonster_python/compleximage.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
from .capmonster import UserAgent
22

3+
34
class ComplexImageTask(UserAgent):
45
def __init__(self, client_key):
56
super(ComplexImageTask, self).__init__(client_key)
6-
7+
78
def create_task(self, _class: str, grid: str = None,
89
task_definition: str = None,
910
image_urls: list = None,
1011
images_base64: list = None,
1112
task: str = None,
1213
websiteUrl: str = None):
13-
if _class is not "recaptcha" or _class is not "hcaptcha":
14+
if _class != "recaptcha" or _class != "hcaptcha":
1415
raise ValueError("Currently only recaptcha or hcaptcha is supported as _class value.")
1516
data = {
1617
"clientKey": self._client_key,
@@ -26,7 +27,7 @@ def create_task(self, _class: str, grid: str = None,
2627
data["task"]["imagesBase64"] = images_base64
2728
else:
2829
raise ValueError("image_urls or images_base64 must be sent")
29-
if _class is "recaptcha":
30+
if _class == "recaptcha":
3031
if grid is None:
3132
raise ValueError("Grid parameter must sent with recaptcha")
3233
else:
@@ -37,12 +38,12 @@ def create_task(self, _class: str, grid: str = None,
3738
data["task"]["metadata"]["TaskDefinition"] = task_definition
3839
else:
3940
raise ValueError("task_definition or task parameter must be sent")
40-
elif _class is "hcaptcha":
41+
elif _class == "hcaptcha":
4142
if task is not None:
4243
data["task"]["metadata"]["Task"] = task
4344
else:
4445
raise ValueError("task parameter must be sent with hcaptcha")
4546
if websiteUrl is not None:
4647
data["task"]["websiteUrl"] = websiteUrl
4748
data, is_user_agent = self._add_user_agent(data)
48-
return self._make_request("createTask", data).get("taskId")
49+
return self._make_request("createTask", data).get("taskId")

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from distutils.core import setup
22
from os import path
3+
34
this_directory = path.abspath(path.dirname(__file__))
45
with open(path.join(this_directory, "README.md"), encoding="utf-8") as f:
56
long_description = f.read()
67

7-
88
setup(
99
name="capmonster_python",
10-
version="2.5.0",
10+
version="2.6.0",
1111
packages=["capmonster_python"],
1212
url="https://github.com/alperensert/capmonster_python",
1313
long_description=long_description,

0 commit comments

Comments
 (0)