55
66
77class 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
0 commit comments