Skip to content

Commit

Permalink
DataDomeTask added
Browse files Browse the repository at this point in the history
- Added detailed docstrings for all Capmonster class methods
- Made syntax corrections and improvements in source code
- Imported DataDomeTask in the init.py file
  • Loading branch information
alperensert committed Jan 18, 2024
1 parent ee0d497 commit 2ccb40d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 13 deletions.
1 change: 1 addition & 0 deletions capmonster_python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
from .geetest import GeeTestTask
from .utils import CapmonsterException
from .compleximage import ComplexImageTask
from .datadome import DataDomeTask
63 changes: 57 additions & 6 deletions capmonster_python/capmonster.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


class Capmonster:
"""A class that interacts with the Capmonster API."""
__SOFT_ID = 30
_HOST_URL = "https://api.capmonster.cloud"
_CREATE_TASK_URL = "/createTask"
Expand All @@ -16,11 +17,20 @@ class Capmonster:
def __init__(self, client_key):
self._client_key = client_key

def get_balance(self):
def get_balance(self) -> float:
"""
Retrieves the balance of the client.
:return: The balance of the client as a float.
"""
data = {"clientKey": self._client_key}
return self._make_request("getBalance", data).get("balance")

def get_task_result(self, task_id: int):
"""
:param task_id: The ID of the task for which the result is requested.
:return: The result of the task if it is ready, otherwise False.
"""
data = {
"clientKey": self._client_key,
"taskId": task_id
Expand Down Expand Up @@ -55,7 +65,15 @@ async def join_task_result_async(self, task_id: int, maximum_time: int = 120):
61, "ERROR_MAXIMUM_TIME_EXCEED", "Maximum time is exceed.")

def report_incorrect_captcha(self, captcha_type: str, task_id: int) -> bool:
if captcha_type is not "image" or "token":
"""
Reports an incorrect captcha.
:param captcha_type: The type of captcha ("image" or "token").
:param task_id: The ID of the task.
:return: True if the captcha is successfully reported, False otherwise.
:raises CapmonsterException: If the captcha type is invalid.
"""
if captcha_type != "image" or "token":
raise CapmonsterException(
1, "ERROR_INCORRECT_CAPTCHA_TYPE", "Valid captcha_type parameters are only 'image' or 'token'.")
try:
Expand All @@ -71,7 +89,7 @@ def _report_incorrect_captcha(self, captcha_type: str, task_id: int):
"clientKey": self._client_key,
"taskId": task_id
}
if captcha_type is "image":
if captcha_type == "image":
response = self._make_request("reportIncorrectImageCaptcha", data)
else:
response = self._make_request("reportIncorrectTokenCaptcha", data)
Expand Down Expand Up @@ -111,13 +129,13 @@ def _add_cookies(cookies, data):
if cookies is None:
return data
str_cookies = ""
if type(cookies) == dict:
if type(cookies) is dict:
for key, value in cookies.items():
if value == list(cookies.items())[-1][1]:
str_cookies += "{}={}".format(key, value)
else:
str_cookies += "{}={};".format(key, value)
elif type(cookies) == list:
elif type(cookies) is list:
for i in cookies:
if not len(cookies) % 2 == 0:
raise AttributeError(
Expand All @@ -128,7 +146,7 @@ def _add_cookies(cookies, data):
str_cookies += "{}".format(i)
elif cookies.index(i) % 2 == 1:
str_cookies += "{};".format(i)
elif type(cookies) == str:
elif type(cookies) is str:
data["task"]["cookies"] = cookies
return data
data["task"]["cookies"] = str_cookies
Expand All @@ -146,13 +164,28 @@ def __init__(self, client_key):

def set_proxy(self, proxy_type: str, proxy_address: str, proxy_port: int,
proxy_login: str = None, proxy_password: str = None):
"""
Sets the proxy settings for the client.
:param proxy_type: The type of the proxy. (e.g. "HTTP", "SOCKS5")
:param proxy_address: The address of the proxy server.
:param proxy_port: The port number of the proxy server.
:param proxy_login: (optional) The login username for proxy authentication.
:param proxy_password: (optional) The login password for proxy authentication.
:return: None
"""
self._proxy_type = proxy_type
self._proxy_address = proxy_address
self._proxy_port = proxy_port
self._proxy_login = proxy_login
self._proxy_password = proxy_password

def disable_proxy(self):
"""
Disables the proxy settings.
:return: None
"""
self._proxy_type = None
self._proxy_address = None
self._proxy_port = None
Expand Down Expand Up @@ -184,9 +217,20 @@ def __init__(self, client_key):
self._fallback = False

def set_user_agent(self, user_agent: str):
"""
Set the user agent for the instance.
:param user_agent: The user agent string to set.
:return: None
"""
self._user_agent = user_agent

def reset_user_agent(self):
"""
Reset the user agent to None.
:return:
"""
self._user_agent = None

def _add_user_agent(self, data):
Expand All @@ -197,4 +241,11 @@ def _add_user_agent(self, data):
return data, False

def set_fallback_to_actual_user_agent(self, fallback: bool):
"""
Set the fallback value for the actual user agent.
:param fallback: A boolean value indicating whether to enable or disable the fallback.
:type fallback: bool
:return: None
"""
self._fallback = fallback
11 changes: 6 additions & 5 deletions capmonster_python/compleximage.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
from .capmonster import UserAgent


class ComplexImageTask(UserAgent):
def __init__(self, client_key):
super(ComplexImageTask, self).__init__(client_key)

def create_task(self, _class: str, grid: str = None,
task_definition: str = None,
image_urls: list = None,
images_base64: list = None,
task: str = None,
websiteUrl: str = None):
if _class is not "recaptcha" or _class is not "hcaptcha":
if _class != "recaptcha" or _class != "hcaptcha":
raise ValueError("Currently only recaptcha or hcaptcha is supported as _class value.")
data = {
"clientKey": self._client_key,
Expand All @@ -26,7 +27,7 @@ def create_task(self, _class: str, grid: str = None,
data["task"]["imagesBase64"] = images_base64
else:
raise ValueError("image_urls or images_base64 must be sent")
if _class is "recaptcha":
if _class == "recaptcha":
if grid is None:
raise ValueError("Grid parameter must sent with recaptcha")
else:
Expand All @@ -37,12 +38,12 @@ def create_task(self, _class: str, grid: str = None,
data["task"]["metadata"]["TaskDefinition"] = task_definition
else:
raise ValueError("task_definition or task parameter must be sent")
elif _class is "hcaptcha":
elif _class == "hcaptcha":
if task is not None:
data["task"]["metadata"]["Task"] = task
else:
raise ValueError("task parameter must be sent with hcaptcha")
if websiteUrl is not None:
data["task"]["websiteUrl"] = websiteUrl
data, is_user_agent = self._add_user_agent(data)
return self._make_request("createTask", data).get("taskId")
return self._make_request("createTask", data).get("taskId")
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from distutils.core import setup
from os import path

this_directory = path.abspath(path.dirname(__file__))
with open(path.join(this_directory, "README.md"), encoding="utf-8") as f:
long_description = f.read()


setup(
name="capmonster_python",
version="2.5.0",
version="2.6.0",
packages=["capmonster_python"],
url="https://github.com/alperensert/capmonster_python",
long_description=long_description,
Expand Down

0 comments on commit 2ccb40d

Please sign in to comment.