python-rucaptcha 6.1__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.
@@ -0,0 +1,172 @@
1
+ from typing import Union
2
+
3
+ from .core.base import BaseCaptcha
4
+ from .core.enums import ReCaptchaEnm
5
+
6
+
7
+ class ReCaptcha(BaseCaptcha):
8
+ def __init__(
9
+ self,
10
+ websiteURL: str,
11
+ websiteKey: str,
12
+ minScore: float = 0.3,
13
+ method: Union[str, ReCaptchaEnm] = ReCaptchaEnm.RecaptchaV2TaskProxyless.value,
14
+ *args,
15
+ **kwargs,
16
+ ):
17
+ """
18
+ The class is used to work with ReCaptcha
19
+
20
+ Args:
21
+ rucaptcha_key: User API key
22
+ websiteURL: Full URL of the captcha page
23
+ websiteKey: The value of the `data-sitekey` parameter you found in the page code
24
+ version: `v3` - indicates that this is reCAPTCHA V3
25
+ method: Captcha type
26
+
27
+ Examples:
28
+ >>> ReCaptcha(rucaptcha_key="aa9011f31111181111168611f1151122",
29
+ ... websiteURL="https://rucaptcha.com/demo/recaptcha-v2",
30
+ ... websiteKey="6LeIxboZAAAAAFQy7d8GPzgRZu2bV0GwKS8ue_cH",
31
+ ... method=ReCaptchaEnm.RecaptchaV2TaskProxyless.value
32
+ ... ).captcha_handler()
33
+ {
34
+ "errorId":0,
35
+ "status":"ready",
36
+ "solution":{
37
+ "gRecaptchaResponse":"03ADUVZw...UWxTAe6ncIa",
38
+ "token":"03ADUVZw...UWxTAe6ncIa"
39
+ },
40
+ "cost":"0.00299",
41
+ "ip":"1.2.3.4",
42
+ "createTime":1692863536,
43
+ "endTime":1692863556,
44
+ "solveCount":1,
45
+ "taskId": 73043008354
46
+ }
47
+
48
+ >>> ReCaptcha(rucaptcha_key="aa9011f31111181111168611f1151122",
49
+ ... websiteURL="https://rucaptcha.com/demo/recaptcha-v2",
50
+ ... websiteKey="6LeIxboZAAAAAFQy7d8GPzgRZu2bV0GwKS8ue_cH"
51
+ ... ).captcha_handler()
52
+ {
53
+ "errorId":0,
54
+ "status":"ready",
55
+ "solution":{
56
+ "gRecaptchaResponse":"03ADUVZw...UWxTAe6ncIa",
57
+ "token":"03ADUVZw...UWxTAe6ncIa"
58
+ },
59
+ "cost":"0.00299",
60
+ "ip":"1.2.3.4",
61
+ "createTime":1692863536,
62
+ "endTime":1692863556,
63
+ "solveCount":1,
64
+ "taskId": 73043008354
65
+ }
66
+
67
+ >>> ReCaptcha(rucaptcha_key="aa9011f31111181111168611f1151122",
68
+ ... websiteURL="https://rucaptcha.com/demo/recaptcha-v2",
69
+ ... websiteKey="6LeIxboZAAAAAFQy7d8GPzgRZu2bV0GwKS8ue_cH",
70
+ ... ).captcha_handler()
71
+ {
72
+ "errorId":0,
73
+ "status":"ready",
74
+ "solution":{
75
+ "gRecaptchaResponse":"03ADUVZw...UWxTAe6ncIa",
76
+ "token":"03ADUVZw...UWxTAe6ncIa"
77
+ },
78
+ "cost":"0.00299",
79
+ "ip":"1.2.3.4",
80
+ "createTime":1692863536,
81
+ "endTime":1692863556,
82
+ "solveCount":1,
83
+ "taskId": 73043008354
84
+ }
85
+
86
+ >>> ReCaptcha(rucaptcha_key="aa9011f31111181111168611f1151122",
87
+ ... websiteURL="https://rucaptcha.com/demo/recaptcha-v2",
88
+ ... websiteKey="6LeIxboZAAAAAFQy7d8GPzgRZu2bV0GwKS8ue_cH",
89
+ ... method=ReCaptchaEnm.RecaptchaV3TaskProxyless.value,
90
+ ... min_score=0.3,
91
+ ... ).captcha_handler()
92
+ {
93
+ "errorId":0,
94
+ "status":"ready",
95
+ "solution":{
96
+ "gRecaptchaResponse":"03ADUVZw...UWxTAe6ncIa",
97
+ "token":"03ADUVZw...UWxTAe6ncIa"
98
+ },
99
+ "cost":"0.00299",
100
+ "ip":"1.2.3.4",
101
+ "createTime":1692863536,
102
+ "endTime":1692863556,
103
+ "solveCount":1,
104
+ "taskId": 73043008354
105
+ }
106
+
107
+ >>> await ReCaptcha(rucaptcha_key="aa9011f31111181111168611f1151122",
108
+ ... websiteURL="https://rucaptcha.com/demo/recaptcha-v2",
109
+ ... websiteKey="6LeIxboZAAAAAFQy7d8GPzgRZu2bV0GwKS8ue_cH",
110
+ ... method=ReCaptchaEnm.RecaptchaV2TaskProxyless.value
111
+ ... ).aio_captcha_handler()
112
+ {
113
+ "errorId":0,
114
+ "status":"ready",
115
+ "solution":{
116
+ "gRecaptchaResponse":"03ADUVZw...UWxTAe6ncIa",
117
+ "token":"03ADUVZw...UWxTAe6ncIa"
118
+ },
119
+ "cost":"0.00299",
120
+ "ip":"1.2.3.4",
121
+ "createTime":1692863536,
122
+ "endTime":1692863556,
123
+ "solveCount":1,
124
+ "taskId": 73043008354
125
+ }
126
+
127
+ Returns:
128
+ Dict with full server response
129
+
130
+ Notes:
131
+ https://rucaptcha.com/api-docs/recaptcha-v2
132
+
133
+ https://rucaptcha.com/api-docs/recaptcha-v3
134
+
135
+ https://rucaptcha.com/api-docs/recaptcha-v2-enterprise
136
+ """
137
+ super().__init__(method=method, *args, **kwargs)
138
+
139
+ self.create_task_payload["task"].update(
140
+ {"websiteURL": websiteURL, "websiteKey": websiteKey, "minScore": minScore}
141
+ )
142
+
143
+ # check user params
144
+ if method not in ReCaptchaEnm.list_values():
145
+ raise ValueError(f"Invalid method parameter set, available - {ReCaptchaEnm.list_values()}")
146
+
147
+ def captcha_handler(self, **kwargs) -> dict:
148
+ """
149
+ Sync solving method
150
+
151
+ Args:
152
+ kwargs: additional params for `requests` library
153
+
154
+ Returns:
155
+ Dict with full server response
156
+
157
+ Notes:
158
+ Check class docstirng for more info
159
+ """
160
+ return self._processing_response(**kwargs)
161
+
162
+ async def aio_captcha_handler(self) -> dict:
163
+ """
164
+ Async solving method
165
+
166
+ Returns:
167
+ Dict with full server response
168
+
169
+ Notes:
170
+ Check class docstirng for more info
171
+ """
172
+ return await self._aio_processing_response()
@@ -0,0 +1,196 @@
1
+ import shutil
2
+ import logging
3
+ from typing import Union, Optional
4
+
5
+ from .core.base import BaseCaptcha
6
+ from .core.enums import SaveFormatsEnm, RotateCaptchaEnm
7
+
8
+
9
+ class RotateCaptcha(BaseCaptcha):
10
+ def __init__(
11
+ self,
12
+ save_format: Union[str, SaveFormatsEnm] = SaveFormatsEnm.TEMP,
13
+ img_clearing: bool = True,
14
+ img_path: str = "PythonRotateCaptchaFiles",
15
+ *args,
16
+ **kwargs,
17
+ ):
18
+ """
19
+ The class is used to work with Rotate Captcha.
20
+
21
+ Args:
22
+ method: Captcha type
23
+ kwargs: Not required params for task creation request
24
+
25
+ Examples:
26
+ >>> RotateCaptcha(rucaptcha_key="aa9011f31111181111168611f1151122",
27
+ ... ).captcha_handler(captcha_file="examples/rotate/rotate_ex.png")
28
+ {
29
+ "errorId":0,
30
+ "status":"ready",
31
+ "solution":{
32
+ "rotate":180
33
+ },
34
+ "cost":"0.0005",
35
+ "ip":"1.2.3.4",
36
+ "createTime":1692863536,
37
+ "endTime":1692863556,
38
+ "solveCount":1,
39
+ "taskId": 73043008354
40
+ }
41
+
42
+ >>> with open("src/examples/rotate/rotate_ex.png", "rb") as f:
43
+ ... file_data = f.read()
44
+ >>> RotateCaptcha(rucaptcha_key="aa9011f31111181111168611f1151122"
45
+ ... ).captcha_handler(captcha_base64=file_data)
46
+ {
47
+ "errorId":0,
48
+ "status":"ready",
49
+ "solution":{
50
+ "rotate":180
51
+ },
52
+ "cost":"0.0005",
53
+ "ip":"1.2.3.4",
54
+ "createTime":1692863536,
55
+ "endTime":1692863556,
56
+ "solveCount":1,
57
+ "taskId": 73043008354
58
+ }
59
+
60
+ >>> await RotateCaptcha(rucaptcha_key="aa9011f31111181111168611f1151122",
61
+ ... ).aio_captcha_handler(captcha_file="examples/rotate/rotate_ex.png")
62
+ {
63
+ "errorId":0,
64
+ "status":"ready",
65
+ "solution":{
66
+ "rotate":180
67
+ },
68
+ "cost":"0.0005",
69
+ "ip":"1.2.3.4",
70
+ "createTime":1692863536,
71
+ "endTime":1692863556,
72
+ "solveCount":1,
73
+ "taskId": 73043008354
74
+ }
75
+
76
+ >>> await RotateCaptcha(rucaptcha_key="aa9011f31111181111168611f1151122",
77
+ ... angle=45).aio_captcha_handler(captcha_file="examples/rotate/rotate_ex.png")
78
+ {
79
+ "errorId":0,
80
+ "status":"ready",
81
+ "solution":{
82
+ "rotate":90
83
+ },
84
+ "cost":"0.0005",
85
+ "ip":"1.2.3.4",
86
+ "createTime":1692863536,
87
+ "endTime":1692863556,
88
+ "solveCount":1,
89
+ "taskId": 73043008354
90
+ }
91
+
92
+ >>> with open("src/examples/rotate/rotate_ex.png", "rb") as f:
93
+ ... file_data = f.read()
94
+ >>> await RotateCaptcha(rucaptcha_key="aa9011f31111181111168611f1151122"
95
+ ... ).aio_captcha_handler(captcha_base64=file_data)
96
+ {
97
+ "errorId":0,
98
+ "status":"ready",
99
+ "solution":{
100
+ "rotate":180
101
+ },
102
+ "cost":"0.0005",
103
+ "ip":"1.2.3.4",
104
+ "createTime":1692863536,
105
+ "endTime":1692863556,
106
+ "solveCount":1,
107
+ "taskId": 73043008354
108
+ }
109
+
110
+ Returns:
111
+ Dict with full server response
112
+
113
+ Notes:
114
+ https://rucaptcha.com/api-docs/rotate
115
+ """
116
+ super().__init__(method=RotateCaptchaEnm.RotateTask, *args, **kwargs)
117
+
118
+ self.save_format = save_format
119
+ self.img_clearing = img_clearing
120
+ self.img_path = img_path
121
+
122
+ def captcha_handler(
123
+ self,
124
+ captcha_link: Optional[str] = None,
125
+ captcha_file: Optional[str] = None,
126
+ captcha_base64: Optional[bytes] = None,
127
+ **kwargs,
128
+ ):
129
+ """
130
+ Async solving method
131
+
132
+ Args:
133
+ captcha_link: Captcha image URL
134
+ captcha_file: Captcha image file path
135
+ captcha_base64: Captcha image BASE64 info
136
+ kwargs: additional params for `requests` library
137
+
138
+ Returns:
139
+ Dict with full server response
140
+
141
+ Notes:
142
+ Check class docstirng for more info
143
+ """
144
+ self._body_file_processing(
145
+ save_format=self.save_format,
146
+ file_path=self.img_path,
147
+ captcha_link=captcha_link,
148
+ captcha_file=captcha_file,
149
+ captcha_base64=captcha_base64,
150
+ **kwargs,
151
+ )
152
+ logging.warning(f"{self.result = }")
153
+ if not self.result.errorId:
154
+ return self._processing_response(**kwargs)
155
+ logging.warning("ERROR ID IS EXISTS")
156
+ return self.result.to_dict()
157
+
158
+ async def aio_captcha_handler(
159
+ self,
160
+ captcha_link: Optional[str] = None,
161
+ captcha_file: Optional[str] = None,
162
+ captcha_base64: Optional[bytes] = None,
163
+ **kwargs,
164
+ ):
165
+ """
166
+ Async solving method
167
+
168
+ Args:
169
+ captcha_link: Captcha image URL
170
+ captcha_file: Captcha image file path
171
+ captcha_base64: Captcha image BASE64 info
172
+ kwargs: additional params for `aiohttp` library
173
+
174
+ Returns:
175
+ Dict with full server response
176
+
177
+ Notes:
178
+ Check class docstirng for more info
179
+ """
180
+
181
+ await self._aio_body_file_processing(
182
+ save_format=self.save_format,
183
+ file_path=self.img_path,
184
+ captcha_link=captcha_link,
185
+ captcha_file=captcha_file,
186
+ captcha_base64=captcha_base64,
187
+ **kwargs,
188
+ )
189
+ logging.warning(f"{self.result = }")
190
+ if not self.result.errorId:
191
+ return await self._aio_processing_response()
192
+ return self.result.to_dict()
193
+
194
+ def __del__(self):
195
+ if self.save_format == SaveFormatsEnm.CONST.value and self.img_clearing:
196
+ shutil.rmtree(self.img_path)
@@ -0,0 +1,119 @@
1
+ import logging
2
+
3
+ from .core.base import BaseCaptcha
4
+ from .core.enums import TextCaptchaEnm
5
+
6
+
7
+ class TextCaptcha(BaseCaptcha):
8
+ def __init__(
9
+ self,
10
+ languagePool: str = "en",
11
+ *args,
12
+ **kwargs,
13
+ ):
14
+ """
15
+ The class is used to work with Text Captcha.
16
+
17
+ Args:
18
+ rucaptcha_key: User API key
19
+ languagePool: Used to choose the workers for solving the captcha by their language.
20
+ Applicable to image-based and text-based captchas.\n
21
+ `en` - English-speaking workers\n
22
+ `rn` - Russian-speaking workers.
23
+ kwargs: Additional not required params for this captcha type
24
+
25
+ Examples:
26
+ >>> TextCaptcha(rucaptcha_key="aa90...51122",
27
+ ... languagePool='en'
28
+ ... ).captcha_handler(textcaptcha="If tomorrow is Saturday, what day is today?")
29
+ {
30
+ "errorId":0,
31
+ "status":"ready",
32
+ "solution":{
33
+ "text":"SUNDAY"
34
+ },
35
+ "cost":0.03669,
36
+ "ip":"46.53.241.91",
37
+ "createTime":1695617910,
38
+ "endTime":1695617965,
39
+ "solveCount":2,
40
+ "taskId":5423543
41
+ }
42
+
43
+ >>> TextCaptcha(rucaptcha_key="aa90...51122",
44
+ ... ).captcha_handler(textcaptcha="If tomorrow is Saturday, what day is today?")
45
+ {
46
+ "errorId":0,
47
+ "status":"ready",
48
+ "solution":{
49
+ "text":"SUNDAY"
50
+ },
51
+ "cost":0.03669,
52
+ "ip":"46.53.241.91",
53
+ "createTime":1695617910,
54
+ "endTime":1695617965,
55
+ "solveCount":2,
56
+ "taskId":5423543
57
+ }
58
+
59
+ >>> await TextCaptcha(rucaptcha_key="aa90...51122",
60
+ ... ).aio_captcha_handler(textcaptcha="If tomorrow is Saturday, what day is today?")
61
+ {
62
+ "errorId":0,
63
+ "status":"ready",
64
+ "solution":{
65
+ "text":"SUNDAY"
66
+ },
67
+ "cost":0.03669,
68
+ "ip":"46.53.241.91",
69
+ "createTime":1695617910,
70
+ "endTime":1695617965,
71
+ "solveCount":2,
72
+ "taskId":5423543
73
+ }
74
+
75
+ Returns:
76
+ Dict with full server response
77
+
78
+ Notes:
79
+ https://2captcha.com/api-docs/text
80
+
81
+ https://rucaptcha.com/api-docs/text
82
+ """
83
+
84
+ super().__init__(method=TextCaptchaEnm.TextCaptchaTask.value, *args, **kwargs)
85
+
86
+ self.create_task_payload.update({"languagePool": languagePool})
87
+ logging.warning(f"{self.create_task_payload = }")
88
+
89
+ def captcha_handler(self, textcaptcha: str, **kwargs) -> dict:
90
+ """
91
+ Synchronous method for captcha solving
92
+
93
+ Args:
94
+ textcaptcha: Captcha text
95
+
96
+ Returns:
97
+ Dict with full server response
98
+
99
+ Notes:
100
+ Check class docstirng for more info
101
+ """
102
+ self.create_task_payload["task"].update({"comment": textcaptcha})
103
+ return self._processing_response(**kwargs)
104
+
105
+ async def aio_captcha_handler(self, textcaptcha: str) -> dict:
106
+ """
107
+ Asynchronous method for captcha solving
108
+
109
+ Args:
110
+ textcaptcha: Captcha text
111
+
112
+ Returns:
113
+ Dict with full server response
114
+
115
+ Notes:
116
+ Check class docstirng for more info
117
+ """
118
+ self.create_task_payload["task"].update({"comment": textcaptcha})
119
+ return await self._aio_processing_response()
@@ -0,0 +1,111 @@
1
+ from typing import Union
2
+
3
+ from .core.base import BaseCaptcha
4
+ from .core.enums import TurnstileCaptchaEnm
5
+
6
+
7
+ class Turnstile(BaseCaptcha):
8
+ def __init__(
9
+ self,
10
+ websiteURL: str,
11
+ websiteKey: str,
12
+ userAgent: str,
13
+ method: Union[str, TurnstileCaptchaEnm] = TurnstileCaptchaEnm.TurnstileTaskProxyless,
14
+ *args,
15
+ **kwargs,
16
+ ):
17
+ """
18
+ The class is used to work with Cloudflare Turnstile.
19
+
20
+ Args:
21
+ rucaptcha_key: User API key
22
+ websiteURL: Full URL of the captcha page
23
+ websiteKey: The value of the `sitekey` parameter found on the site
24
+ userAgent: Your browser UserAgent
25
+ method: Captcha type
26
+ kwargs: Not required params for task creation request
27
+
28
+ Examples:
29
+ >>> Turnstile(rucaptcha_key="aa9011f31111181111168611f1151122",
30
+ ... websiteURL="https://www.geetest.com/en/demo",
31
+ ... websiteKey="0x4AAAAAAAC3DHQFLr1GavRN",
32
+ ... method=TurnstileCaptchaEnm.TurnstileTaskProxyless.value,
33
+ ... ).captcha_handler()
34
+ {
35
+ "errorId":0,
36
+ "status":"ready",
37
+ "solution":{
38
+ "token":"0.zrSnRHO7h0HwSjSCU8oyzbjEtD8p.d62306d4ee00c77dda697f959ebbd7bd97",
39
+ "userAgent":"Mozilla/5.0 (....."
40
+ },
41
+ "cost":"0.00145",
42
+ "ip":"1.2.3.4",
43
+ "createTime":1692863536,
44
+ "endTime":1692863556,
45
+ "solveCount":1,
46
+ "taskId": 73243152973,
47
+ }
48
+
49
+ >>> await Turnstile(rucaptcha_key="aa9011f31111181111168611f1151122",
50
+ ... websiteURL="https://www.geetest.com/en/demo",
51
+ ... websiteKey="0x4AAAAAAAC3DHQFLr1GavRN",
52
+ ... method=TurnstileCaptchaEnm.TurnstileTaskProxyless.value,
53
+ ... ).aio_captcha_handler()
54
+ {
55
+ "errorId":0,
56
+ "status":"ready",
57
+ "solution":{
58
+ "token":"0.zrSnRHO7h0HwSjSCU8oyzbjEtD8p.d62306d4ee00c77dda697f959ebbd7bd97",
59
+ "userAgent":"Mozilla/5.0 (....."
60
+ },
61
+ "cost":"0.00145",
62
+ "ip":"1.2.3.4",
63
+ "createTime":1692863536,
64
+ "endTime":1692863556,
65
+ "solveCount":1,
66
+ "taskId": 73243152973,
67
+ }
68
+
69
+ Returns:
70
+ Dict with full server response
71
+
72
+ Notes:
73
+ https://rucaptcha.com/api-docs/cloudflare-turnstile
74
+ """
75
+
76
+ super().__init__(method=method, *args, **kwargs)
77
+
78
+ self.create_task_payload["task"].update(
79
+ {"websiteURL": websiteURL, "websiteKey": websiteKey, "userAgent": userAgent}
80
+ )
81
+
82
+ # check user params
83
+ if method not in TurnstileCaptchaEnm.list_values():
84
+ raise ValueError(f"Invalid method parameter set, available - {TurnstileCaptchaEnm.list_values()}")
85
+
86
+ def captcha_handler(self, **kwargs) -> dict:
87
+ """
88
+ Sync solving method
89
+
90
+ Args:
91
+ kwargs: Parameters for the `requests` library
92
+
93
+ Returns:
94
+ Dict with full server response
95
+
96
+ Notes:
97
+ Check class docstirng for more info
98
+ """
99
+ return self._processing_response(**kwargs)
100
+
101
+ async def aio_captcha_handler(self) -> dict:
102
+ """
103
+ Async solving method
104
+
105
+ Returns:
106
+ Dict with full server response
107
+
108
+ Notes:
109
+ Check class docstirng for more info
110
+ """
111
+ return await self._aio_processing_response()