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.
- python_rucaptcha/__init__.py +0 -0
- python_rucaptcha/__version__.py +1 -0
- python_rucaptcha/amazon_waf.py +114 -0
- python_rucaptcha/audio_captcha.py +161 -0
- python_rucaptcha/bounding_box_captcha.py +310 -0
- python_rucaptcha/capy_puzzle.py +138 -0
- python_rucaptcha/control.py +174 -0
- python_rucaptcha/coordinates_captcha.py +295 -0
- python_rucaptcha/core/__init__.py +0 -0
- python_rucaptcha/core/base.py +244 -0
- python_rucaptcha/core/config.py +21 -0
- python_rucaptcha/core/enums.py +146 -0
- python_rucaptcha/core/result_handler.py +65 -0
- python_rucaptcha/core/serializer.py +74 -0
- python_rucaptcha/cutcaptcha.py +116 -0
- python_rucaptcha/cyber_siara_captcha.py +110 -0
- python_rucaptcha/datadome_captcha.py +121 -0
- python_rucaptcha/draw_around_captcha.py +359 -0
- python_rucaptcha/fun_captcha.py +103 -0
- python_rucaptcha/gee_test.py +216 -0
- python_rucaptcha/grid_captcha.py +271 -0
- python_rucaptcha/hcaptcha.py +111 -0
- python_rucaptcha/image_captcha.py +249 -0
- python_rucaptcha/key_captcha.py +111 -0
- python_rucaptcha/lemin_captcha.py +112 -0
- python_rucaptcha/mt_captcha.py +104 -0
- python_rucaptcha/re_captcha.py +172 -0
- python_rucaptcha/rotate_captcha.py +196 -0
- python_rucaptcha/text_captcha.py +119 -0
- python_rucaptcha/turnstile.py +111 -0
- python_rucaptcha-6.1.0.dist-info/METADATA +138 -0
- python_rucaptcha-6.1.0.dist-info/RECORD +34 -0
- python_rucaptcha-6.1.0.dist-info/WHEEL +5 -0
- python_rucaptcha-6.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import time
|
|
3
|
+
import uuid
|
|
4
|
+
import base64
|
|
5
|
+
import asyncio
|
|
6
|
+
from typing import Optional
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
import aiohttp
|
|
10
|
+
import requests
|
|
11
|
+
from requests.adapters import HTTPAdapter
|
|
12
|
+
|
|
13
|
+
from .enums import ServiceEnm, SaveFormatsEnm
|
|
14
|
+
from .config import RETRIES, ASYNC_RETRIES
|
|
15
|
+
from .serializer import TaskSer, CaptchaOptionsSer, CreateTaskBaseSer, GetTaskResultRequestSer, GetTaskResultResponseSer
|
|
16
|
+
from .result_handler import get_sync_result, get_async_result
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class BaseCaptcha:
|
|
20
|
+
NO_CAPTCHA_ERR = "You did not send any file, local link or URL."
|
|
21
|
+
|
|
22
|
+
def __init__(
|
|
23
|
+
self,
|
|
24
|
+
rucaptcha_key: str,
|
|
25
|
+
method: str,
|
|
26
|
+
sleep_time: int = 10,
|
|
27
|
+
service_type: str = ServiceEnm.TWOCAPTCHA.value,
|
|
28
|
+
**kwargs,
|
|
29
|
+
):
|
|
30
|
+
"""
|
|
31
|
+
:param rucaptcha_key: User API key
|
|
32
|
+
:param method: Captcha type
|
|
33
|
+
:param sleep_time: Time to wait for captcha solution
|
|
34
|
+
:param service_type: URL with which the program will work, "2captcha" option is possible (standard)
|
|
35
|
+
and "rucaptcha"
|
|
36
|
+
:param kwargs: Designed to pass OPTIONAL parameters to the payload for a request to RuCaptcha
|
|
37
|
+
"""
|
|
38
|
+
self.result = GetTaskResultResponseSer()
|
|
39
|
+
# assign args to validator
|
|
40
|
+
self.params = CaptchaOptionsSer(sleep_time=sleep_time, service_type=service_type)
|
|
41
|
+
self.params.urls_set()
|
|
42
|
+
|
|
43
|
+
# prepare create task payload
|
|
44
|
+
self.create_task_payload = CreateTaskBaseSer(
|
|
45
|
+
clientKey=rucaptcha_key, task=TaskSer(type=method).to_dict()
|
|
46
|
+
).to_dict()
|
|
47
|
+
# prepare get task result data payload
|
|
48
|
+
self.get_task_payload = GetTaskResultRequestSer(clientKey=rucaptcha_key)
|
|
49
|
+
|
|
50
|
+
for key in kwargs:
|
|
51
|
+
self.create_task_payload["task"].update({key: kwargs[key]})
|
|
52
|
+
|
|
53
|
+
# prepare session
|
|
54
|
+
self.session = requests.Session()
|
|
55
|
+
self.session.mount("http://", HTTPAdapter(max_retries=RETRIES))
|
|
56
|
+
self.session.mount("https://", HTTPAdapter(max_retries=RETRIES))
|
|
57
|
+
|
|
58
|
+
def _processing_response(self, **kwargs: dict) -> dict:
|
|
59
|
+
"""
|
|
60
|
+
Method processing captcha solving task creation result
|
|
61
|
+
:param kwargs: additional params for Requests library
|
|
62
|
+
"""
|
|
63
|
+
try:
|
|
64
|
+
response = GetTaskResultResponseSer(
|
|
65
|
+
**self.session.post(self.params.url_request, json=self.create_task_payload, **kwargs).json()
|
|
66
|
+
)
|
|
67
|
+
# check response status
|
|
68
|
+
if response.errorId == 0:
|
|
69
|
+
self.get_task_payload.taskId = response.taskId
|
|
70
|
+
else:
|
|
71
|
+
return response.to_dict()
|
|
72
|
+
except Exception as error:
|
|
73
|
+
self.result.errorId = 12
|
|
74
|
+
self.result.errorCode = self.NO_CAPTCHA_ERR
|
|
75
|
+
self.result.errorDescription = str(error)
|
|
76
|
+
return self.result.to_dict()
|
|
77
|
+
|
|
78
|
+
# wait captcha solving
|
|
79
|
+
time.sleep(self.params.sleep_time)
|
|
80
|
+
|
|
81
|
+
return get_sync_result(
|
|
82
|
+
get_payload=self.get_task_payload, sleep_time=self.params.sleep_time, url_response=self.params.url_response
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
def url_open(self, url: str, **kwargs):
|
|
86
|
+
"""
|
|
87
|
+
Method open links
|
|
88
|
+
"""
|
|
89
|
+
return self.session.get(url=url, **kwargs)
|
|
90
|
+
|
|
91
|
+
async def aio_url_read(self, url: str, **kwargs) -> bytes:
|
|
92
|
+
"""
|
|
93
|
+
Async method read bytes from link
|
|
94
|
+
"""
|
|
95
|
+
async with aiohttp.ClientSession() as session:
|
|
96
|
+
async for attempt in ASYNC_RETRIES:
|
|
97
|
+
with attempt:
|
|
98
|
+
async with session.get(url=url, **kwargs) as resp:
|
|
99
|
+
return await resp.content.read()
|
|
100
|
+
|
|
101
|
+
async def _aio_processing_response(self) -> dict:
|
|
102
|
+
"""
|
|
103
|
+
Method processing async captcha solving task creation result
|
|
104
|
+
"""
|
|
105
|
+
try:
|
|
106
|
+
# make async or sync request
|
|
107
|
+
response = await self.__aio_create_task()
|
|
108
|
+
# check response status
|
|
109
|
+
if response.errorId == 0:
|
|
110
|
+
self.get_task_payload.taskId = response.taskId
|
|
111
|
+
else:
|
|
112
|
+
return response.to_dict()
|
|
113
|
+
except Exception as error:
|
|
114
|
+
self.result.errorId = 12
|
|
115
|
+
self.result.errorCode = self.NO_CAPTCHA_ERR
|
|
116
|
+
self.result.errorDescription = str(error)
|
|
117
|
+
return self.result.to_dict()
|
|
118
|
+
|
|
119
|
+
# wait captcha solving
|
|
120
|
+
await asyncio.sleep(self.params.sleep_time)
|
|
121
|
+
return await get_async_result(
|
|
122
|
+
get_payload=self.get_task_payload,
|
|
123
|
+
sleep_time=self.params.sleep_time,
|
|
124
|
+
url_response=self.params.url_response,
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
async def __aio_create_task(self) -> GetTaskResultResponseSer:
|
|
128
|
+
async with aiohttp.ClientSession() as session:
|
|
129
|
+
async for attempt in ASYNC_RETRIES:
|
|
130
|
+
with attempt:
|
|
131
|
+
async with session.post(
|
|
132
|
+
self.params.url_request, json=self.create_task_payload, raise_for_status=True
|
|
133
|
+
) as resp:
|
|
134
|
+
response_json = await resp.json(content_type=None)
|
|
135
|
+
return GetTaskResultResponseSer(**response_json)
|
|
136
|
+
|
|
137
|
+
# Working with images methods
|
|
138
|
+
|
|
139
|
+
@staticmethod
|
|
140
|
+
def _local_file_captcha(captcha_file: str):
|
|
141
|
+
"""
|
|
142
|
+
Method get local file, read it and prepare for sending to Captcha solving service
|
|
143
|
+
"""
|
|
144
|
+
with open(captcha_file, "rb") as file:
|
|
145
|
+
return file.read()
|
|
146
|
+
|
|
147
|
+
def _file_const_saver(self, content: bytes, file_path: str, file_extension: str = "png"):
|
|
148
|
+
"""
|
|
149
|
+
Method create and save file in folder
|
|
150
|
+
"""
|
|
151
|
+
Path(file_path).mkdir(parents=True, exist_ok=True)
|
|
152
|
+
|
|
153
|
+
# generate image name
|
|
154
|
+
self._file_name = f"file-{uuid.uuid4()}.{file_extension}"
|
|
155
|
+
|
|
156
|
+
# save image to folder
|
|
157
|
+
with open(os.path.join(file_path, self._file_name), "wb") as out_image:
|
|
158
|
+
out_image.write(content)
|
|
159
|
+
|
|
160
|
+
def _body_file_processing(
|
|
161
|
+
self,
|
|
162
|
+
save_format: SaveFormatsEnm,
|
|
163
|
+
file_path: str,
|
|
164
|
+
file_extension: str = "png",
|
|
165
|
+
captcha_link: Optional[str] = None,
|
|
166
|
+
captcha_file: Optional[str] = None,
|
|
167
|
+
captcha_base64: Optional[bytes] = None,
|
|
168
|
+
**kwargs,
|
|
169
|
+
):
|
|
170
|
+
# if a local file link is passed
|
|
171
|
+
if captcha_file:
|
|
172
|
+
self.create_task_payload["task"].update(
|
|
173
|
+
{"body": base64.b64encode(self._local_file_captcha(captcha_file)).decode("utf-8")}
|
|
174
|
+
)
|
|
175
|
+
# if the file is transferred in base64 encoding
|
|
176
|
+
elif captcha_base64:
|
|
177
|
+
self.create_task_payload["task"].update({"body": base64.b64encode(captcha_base64).decode("utf-8")})
|
|
178
|
+
# if a URL is passed
|
|
179
|
+
elif captcha_link:
|
|
180
|
+
try:
|
|
181
|
+
content = self.url_open(url=captcha_link, **kwargs).content
|
|
182
|
+
# according to the value of the passed parameter, select the function to save the image
|
|
183
|
+
if save_format == SaveFormatsEnm.CONST.value:
|
|
184
|
+
self._file_const_saver(content, file_path, file_extension=file_extension)
|
|
185
|
+
self.create_task_payload["task"].update({"body": base64.b64encode(content).decode("utf-8")})
|
|
186
|
+
except Exception as error:
|
|
187
|
+
self.result.errorId = 12
|
|
188
|
+
self.result.errorCode = self.NO_CAPTCHA_ERR
|
|
189
|
+
self.result.errorDescription = str(error)
|
|
190
|
+
|
|
191
|
+
else:
|
|
192
|
+
self.result.errorId = 12
|
|
193
|
+
self.result.errorCode = self.NO_CAPTCHA_ERR
|
|
194
|
+
|
|
195
|
+
async def _aio_body_file_processing(
|
|
196
|
+
self,
|
|
197
|
+
save_format: SaveFormatsEnm,
|
|
198
|
+
file_path: str,
|
|
199
|
+
file_extension: str = "png",
|
|
200
|
+
captcha_link: Optional[str] = None,
|
|
201
|
+
captcha_file: Optional[str] = None,
|
|
202
|
+
captcha_base64: Optional[bytes] = None,
|
|
203
|
+
**kwargs,
|
|
204
|
+
):
|
|
205
|
+
# if a local file link is passed
|
|
206
|
+
if captcha_file:
|
|
207
|
+
self.create_task_payload["task"].update(
|
|
208
|
+
{"body": base64.b64encode(self._local_file_captcha(captcha_file)).decode("utf-8")}
|
|
209
|
+
)
|
|
210
|
+
# if the file is transferred in base64 encoding
|
|
211
|
+
elif captcha_base64:
|
|
212
|
+
self.create_task_payload["task"].update({"body": base64.b64encode(captcha_base64).decode("utf-8")})
|
|
213
|
+
# if a URL is passed
|
|
214
|
+
elif captcha_link:
|
|
215
|
+
try:
|
|
216
|
+
content = await self.aio_url_read(url=captcha_link, **kwargs)
|
|
217
|
+
# according to the value of the passed parameter, select the function to save the image
|
|
218
|
+
if save_format == SaveFormatsEnm.CONST.value:
|
|
219
|
+
self._file_const_saver(content, file_path, file_extension=file_extension)
|
|
220
|
+
self.create_task_payload["task"].update({"body": base64.b64encode(content).decode("utf-8")})
|
|
221
|
+
except Exception as error:
|
|
222
|
+
self.result.errorId = 12
|
|
223
|
+
self.result.errorCode = self.NO_CAPTCHA_ERR
|
|
224
|
+
self.result.errorDescription = str(error)
|
|
225
|
+
|
|
226
|
+
else:
|
|
227
|
+
self.result.errorId = 12
|
|
228
|
+
self.result.errorCode = self.NO_CAPTCHA_ERR
|
|
229
|
+
|
|
230
|
+
def __enter__(self):
|
|
231
|
+
return self
|
|
232
|
+
|
|
233
|
+
def __exit__(self, exc_type, exc_value, traceback):
|
|
234
|
+
if exc_type:
|
|
235
|
+
return False
|
|
236
|
+
return True
|
|
237
|
+
|
|
238
|
+
async def __aenter__(self):
|
|
239
|
+
return self
|
|
240
|
+
|
|
241
|
+
async def __aexit__(self, exc_type, exc_value, traceback):
|
|
242
|
+
if exc_type:
|
|
243
|
+
return False
|
|
244
|
+
return True
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from tenacity import AsyncRetrying, wait_fixed, stop_after_attempt
|
|
2
|
+
from requests.adapters import Retry
|
|
3
|
+
|
|
4
|
+
RETRIES = Retry(total=5, backoff_factor=0.5)
|
|
5
|
+
ASYNC_RETRIES = AsyncRetrying(wait=wait_fixed(5), stop=stop_after_attempt(5), reraise=True)
|
|
6
|
+
# Application key
|
|
7
|
+
APP_KEY = "1899"
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
# Connection retry generator
|
|
11
|
+
def attempts_generator(amount: int = 20):
|
|
12
|
+
"""
|
|
13
|
+
Function generates a generator of length equal to `amount`
|
|
14
|
+
|
|
15
|
+
Args:
|
|
16
|
+
amount: number of attempts generated
|
|
17
|
+
|
|
18
|
+
Returns:
|
|
19
|
+
Attempt number
|
|
20
|
+
"""
|
|
21
|
+
yield from range(1, amount)
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
from enum import Enum
|
|
2
|
+
from typing import List
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class MyEnum(Enum):
|
|
6
|
+
"""
|
|
7
|
+
Base class for work with updated Enums
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
@classmethod
|
|
11
|
+
def list(cls) -> List[Enum]:
|
|
12
|
+
return list(map(lambda c: c, cls))
|
|
13
|
+
|
|
14
|
+
@classmethod
|
|
15
|
+
def list_values(cls) -> List[str]:
|
|
16
|
+
return list(map(lambda c: c.value, cls))
|
|
17
|
+
|
|
18
|
+
@classmethod
|
|
19
|
+
def list_names(cls) -> List[str]:
|
|
20
|
+
return list(map(lambda c: c.name, cls))
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class ServiceEnm(str, MyEnum):
|
|
24
|
+
TWOCAPTCHA = "2captcha"
|
|
25
|
+
RUCAPTCHA = "rucaptcha"
|
|
26
|
+
DEATHBYCAPTCHA = "deathbycaptcha"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class SaveFormatsEnm(str, MyEnum):
|
|
30
|
+
TEMP = "temp"
|
|
31
|
+
CONST = "const"
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class GeetestEnm(str, MyEnum):
|
|
35
|
+
GeeTestTask = "GeeTestTask"
|
|
36
|
+
GeeTestTaskProxyless = "GeeTestTaskProxyless"
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class ImageCaptchaEnm(str, MyEnum):
|
|
40
|
+
ImageToTextTask = "ImageToTextTask"
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class CapyPuzzleEnm(str, MyEnum):
|
|
44
|
+
CapyTask = "CapyTask"
|
|
45
|
+
CapyTaskProxyless = "CapyTaskProxyless"
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class FunCaptchaEnm(str, MyEnum):
|
|
49
|
+
FunCaptchaTaskProxyless = "FunCaptchaTaskProxyless"
|
|
50
|
+
FunCaptchaTask = "FunCaptchaTask"
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class ReCaptchaEnm(str, MyEnum):
|
|
54
|
+
RecaptchaV2TaskProxyless = "RecaptchaV2TaskProxyless"
|
|
55
|
+
RecaptchaV2Task = "RecaptchaV2Task"
|
|
56
|
+
|
|
57
|
+
RecaptchaV2EnterpriseTaskProxyless = "RecaptchaV2EnterpriseTaskProxyless"
|
|
58
|
+
RecaptchaV2EnterpriseTask = "RecaptchaV2EnterpriseTask"
|
|
59
|
+
|
|
60
|
+
RecaptchaV3TaskProxyless = "RecaptchaV3TaskProxyless"
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class LeminCaptchaEnm(str, MyEnum):
|
|
64
|
+
LeminTaskProxyless = "LeminTaskProxyless"
|
|
65
|
+
LeminTask = "LeminTask"
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class HCaptchaEnm(str, MyEnum):
|
|
69
|
+
HCaptchaTask = "HCaptchaTask"
|
|
70
|
+
HCaptchaTaskProxyless = "HCaptchaTaskProxyless"
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class KeyCaptchaEnm(str, MyEnum):
|
|
74
|
+
KeyCaptchaTask = "KeyCaptchaTask"
|
|
75
|
+
KeyCaptchaTaskProxyless = "KeyCaptchaTaskProxyless"
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class RotateCaptchaEnm(str, MyEnum):
|
|
79
|
+
RotateTask = "RotateTask"
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
class TikTokCaptchaEnm(str, MyEnum):
|
|
83
|
+
TIKTOK = "tiktok"
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class ControlEnm(str, MyEnum):
|
|
87
|
+
control = "control"
|
|
88
|
+
# https://rucaptcha.com/api-docs/get-balance
|
|
89
|
+
getBalance = "getBalance"
|
|
90
|
+
# https://rucaptcha.com/api-docs/report-correct
|
|
91
|
+
reportCorrect = "reportCorrect"
|
|
92
|
+
# https://rucaptcha.com/api-docs/report-incorrect
|
|
93
|
+
reportIncorrect = "reportIncorrect"
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class TurnstileCaptchaEnm(str, MyEnum):
|
|
97
|
+
TurnstileTaskProxyless = "TurnstileTaskProxyless"
|
|
98
|
+
TurnstileTask = "TurnstileTask"
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
class AmazonWAFCaptchaEnm(str, MyEnum):
|
|
102
|
+
AmazonTask = "AmazonTask"
|
|
103
|
+
AmazonTaskProxyless = "AmazonTaskProxyless"
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
class TextCaptchaEnm(str, MyEnum):
|
|
107
|
+
TextCaptchaTask = "TextCaptchaTask"
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
class AudioCaptchaEnm(str, MyEnum):
|
|
111
|
+
AudioTask = "AudioTask"
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
class CutCaptchaEnm(str, MyEnum):
|
|
115
|
+
CutCaptchaTask = "CutCaptchaTask"
|
|
116
|
+
CutCaptchaTaskProxyless = "CutCaptchaTaskProxyless"
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
class DataDomeSliderEnm(str, MyEnum):
|
|
120
|
+
DataDomeSliderTask = "DataDomeSliderTask"
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
class CyberSiARAEnm(str, MyEnum):
|
|
124
|
+
AntiCyberSiAraTask = "AntiCyberSiAraTask"
|
|
125
|
+
AntiCyberSiAraTaskProxyless = "AntiCyberSiAraTaskProxyless"
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
class MTCaptchaEnm(str, MyEnum):
|
|
129
|
+
MtCaptchaTask = "MtCaptchaTask"
|
|
130
|
+
MtCaptchaTaskProxyless = "MtCaptchaTaskProxyless"
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
class BoundingBoxCaptchaEnm(str, MyEnum):
|
|
134
|
+
BoundingBoxTask = "BoundingBoxTask"
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
class DrawAroundCaptchaEnm(str, MyEnum):
|
|
138
|
+
DrawAroundTask = "DrawAroundTask"
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
class CoordinatesCaptchaEnm(str, MyEnum):
|
|
142
|
+
CoordinatesTask = "CoordinatesTask"
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
class GridCaptchaEnm(str, MyEnum):
|
|
146
|
+
GridTask = "GridTask"
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import time
|
|
2
|
+
import asyncio
|
|
3
|
+
import logging
|
|
4
|
+
from typing import Union
|
|
5
|
+
|
|
6
|
+
import aiohttp
|
|
7
|
+
import requests
|
|
8
|
+
|
|
9
|
+
from .config import attempts_generator
|
|
10
|
+
from .serializer import GetTaskResultRequestSer, GetTaskResultResponseSer
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def get_sync_result(get_payload: GetTaskResultRequestSer, sleep_time: int, url_response: str) -> Union[dict, Exception]:
|
|
14
|
+
"""
|
|
15
|
+
Function periodically send the SYNC request to service and wait for captcha solving result
|
|
16
|
+
"""
|
|
17
|
+
# generator for repeated attempts to connect to the server
|
|
18
|
+
attempts = attempts_generator()
|
|
19
|
+
for _ in attempts:
|
|
20
|
+
try:
|
|
21
|
+
# send a request for the result of solving the captcha
|
|
22
|
+
captcha_response = GetTaskResultResponseSer(
|
|
23
|
+
**requests.post(url_response, json=get_payload.to_dict()).json(), taskId=get_payload.taskId
|
|
24
|
+
)
|
|
25
|
+
logging.warning(f"{captcha_response = }")
|
|
26
|
+
# if the captcha has not been resolved yet, wait
|
|
27
|
+
if captcha_response.status == "processing":
|
|
28
|
+
time.sleep(sleep_time)
|
|
29
|
+
continue
|
|
30
|
+
elif captcha_response.status == "ready":
|
|
31
|
+
break
|
|
32
|
+
elif captcha_response.errorId != 0:
|
|
33
|
+
return captcha_response.to_dict()
|
|
34
|
+
except Exception as error:
|
|
35
|
+
return error
|
|
36
|
+
return captcha_response.to_dict()
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
async def get_async_result(
|
|
40
|
+
get_payload: GetTaskResultRequestSer, sleep_time: int, url_response: str
|
|
41
|
+
) -> Union[dict, Exception]:
|
|
42
|
+
"""
|
|
43
|
+
Function periodically send the ASYNC request to service and wait for captcha solving result
|
|
44
|
+
"""
|
|
45
|
+
# generator for repeated attempts to connect to the server
|
|
46
|
+
attempts = attempts_generator()
|
|
47
|
+
async with aiohttp.ClientSession() as session:
|
|
48
|
+
for _ in attempts:
|
|
49
|
+
try:
|
|
50
|
+
# send a request for the result of solving the captcha
|
|
51
|
+
async with session.post(url_response, json=get_payload.to_dict(), raise_for_status=True) as resp:
|
|
52
|
+
captcha_response = await resp.json(content_type=None)
|
|
53
|
+
captcha_response = GetTaskResultResponseSer(**captcha_response, taskId=get_payload.taskId)
|
|
54
|
+
|
|
55
|
+
# if the captcha has not been resolved yet, wait
|
|
56
|
+
if captcha_response.status == "processing":
|
|
57
|
+
await asyncio.sleep(sleep_time)
|
|
58
|
+
continue
|
|
59
|
+
elif captcha_response.status == "ready":
|
|
60
|
+
break
|
|
61
|
+
elif captcha_response.errorId != 0:
|
|
62
|
+
return captcha_response.to_dict()
|
|
63
|
+
except Exception as error:
|
|
64
|
+
return error
|
|
65
|
+
return captcha_response.to_dict()
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
from typing import Literal, Optional
|
|
2
|
+
|
|
3
|
+
from msgspec import Struct
|
|
4
|
+
|
|
5
|
+
from . import enums
|
|
6
|
+
from .config import APP_KEY
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class MyBaseModel(Struct):
|
|
10
|
+
def to_dict(self):
|
|
11
|
+
return {f: getattr(self, f) for f in self.__struct_fields__}
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
"""
|
|
15
|
+
HTTP API Serializers
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class TaskSer(MyBaseModel):
|
|
20
|
+
type: str
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class CreateTaskBaseSer(MyBaseModel):
|
|
24
|
+
clientKey: str
|
|
25
|
+
task: TaskSer = {}
|
|
26
|
+
languagePool: str = "en"
|
|
27
|
+
callbackUrl: str = None
|
|
28
|
+
soft_id: Literal[APP_KEY] = APP_KEY
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class GetTaskResultRequestSer(MyBaseModel):
|
|
32
|
+
clientKey: str
|
|
33
|
+
taskId: int = None
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class CaptchaOptionsSer(MyBaseModel):
|
|
37
|
+
sleep_time: int = 10
|
|
38
|
+
service_type: enums.ServiceEnm = enums.ServiceEnm.TWOCAPTCHA.value
|
|
39
|
+
|
|
40
|
+
url_request: Optional[str] = None
|
|
41
|
+
url_response: Optional[str] = None
|
|
42
|
+
|
|
43
|
+
def urls_set(self):
|
|
44
|
+
"""
|
|
45
|
+
Set request/response URLs if they not set previously
|
|
46
|
+
"""
|
|
47
|
+
if self.service_type == enums.ServiceEnm.DEATHBYCAPTCHA:
|
|
48
|
+
self.url_request = f"http://api.{self.service_type}.com/2captcha/in.php"
|
|
49
|
+
self.url_response = f"http://api.{self.service_type}.com/2captcha/res.php"
|
|
50
|
+
else:
|
|
51
|
+
self.url_request = f"https://api.{self.service_type}.com/createTask"
|
|
52
|
+
self.url_response = f"https://api.{self.service_type}.com/getTaskResult"
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
"""
|
|
56
|
+
HTTP API Response
|
|
57
|
+
"""
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class GetTaskResultResponseSer(MyBaseModel):
|
|
61
|
+
status: str = "ready"
|
|
62
|
+
solution: dict = None
|
|
63
|
+
cost: float = None
|
|
64
|
+
ip: str = None
|
|
65
|
+
createTime: int = None
|
|
66
|
+
endTime: int = None
|
|
67
|
+
solveCount: int = None
|
|
68
|
+
taskId: int = None
|
|
69
|
+
# control method params
|
|
70
|
+
balance: float = None
|
|
71
|
+
# error info
|
|
72
|
+
errorId: int = 0
|
|
73
|
+
errorCode: str = None
|
|
74
|
+
errorDescription: str = None
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
from typing import Union
|
|
2
|
+
|
|
3
|
+
from .core.base import BaseCaptcha
|
|
4
|
+
from .core.enums import CutCaptchaEnm
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class CutCaptcha(BaseCaptcha):
|
|
8
|
+
def __init__(
|
|
9
|
+
self,
|
|
10
|
+
websiteURL: str,
|
|
11
|
+
miseryKey: str,
|
|
12
|
+
apiKey: str,
|
|
13
|
+
method: Union[str, CutCaptchaEnm] = CutCaptchaEnm.CutCaptchaTaskProxyless,
|
|
14
|
+
*args,
|
|
15
|
+
**kwargs,
|
|
16
|
+
):
|
|
17
|
+
"""
|
|
18
|
+
The class is used to work with CutCaptcha.
|
|
19
|
+
|
|
20
|
+
Args:
|
|
21
|
+
rucaptcha_key: User API key
|
|
22
|
+
websiteURL: Full URL of the captcha page
|
|
23
|
+
miseryKey: The value of CUTCAPTCHA_MISERY_KEY variable defined on page.
|
|
24
|
+
apiKey: The value of data-apikey attribute of iframe's body.
|
|
25
|
+
Also the name of javascript file included on the page
|
|
26
|
+
method: Captcha type
|
|
27
|
+
kwargs: Not required params for task creation request
|
|
28
|
+
|
|
29
|
+
Examples:
|
|
30
|
+
>>> CutCaptcha(rucaptcha_key="aa9011f31111181111168611f1151122",
|
|
31
|
+
... websiteURL="https://example.cc/foo/bar.html",
|
|
32
|
+
... miseryKey="a1488b66da00bf332a1488993a5443c79047e752",
|
|
33
|
+
... apiKey="SAb83IIB",
|
|
34
|
+
... method=CutCaptchaEnm.CutCaptchaTaskProxyless
|
|
35
|
+
... ).captcha_handler()
|
|
36
|
+
{
|
|
37
|
+
"errorId":0,
|
|
38
|
+
"status":"ready",
|
|
39
|
+
"solution":{
|
|
40
|
+
"token":"P1_eyJ0eXAiOiJKV...1LDq89KyJ5A",
|
|
41
|
+
"respKey":"E0_eyJ0eXAiOiJK...y2w5_YbP8PGuJBBo",
|
|
42
|
+
"userAgent":"Mozilla/5.0 (.......",
|
|
43
|
+
"gRecaptchaResponse":"P1_eyJ0eXAiOiJKV...1LDq89KyJ5A"
|
|
44
|
+
},
|
|
45
|
+
"cost":"0.00299",
|
|
46
|
+
"ip":"1.2.3.4",
|
|
47
|
+
"createTime":1692863536,
|
|
48
|
+
"endTime":1692863556,
|
|
49
|
+
"solveCount":1,
|
|
50
|
+
"taskId": 73243152973,
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
>>> await CutCaptcha(rucaptcha_key="aa9011f31111181111168611f1151122",
|
|
54
|
+
... websiteURL="https://example.cc/foo/bar.html",
|
|
55
|
+
... miseryKey="a1488b66da00bf332a1488993a5443c79047e752",
|
|
56
|
+
... apiKey="SAb83IIB",
|
|
57
|
+
... method=CutCaptchaEnm.CutCaptchaTaskProxyless
|
|
58
|
+
... ).aio_captcha_handler()
|
|
59
|
+
{
|
|
60
|
+
"errorId":0,
|
|
61
|
+
"status":"ready",
|
|
62
|
+
"solution":{
|
|
63
|
+
"token":"P1_eyJ0eXAiOiJKV...1LDq89KyJ5A",
|
|
64
|
+
"respKey":"E0_eyJ0eXAiOiJK...y2w5_YbP8PGuJBBo",
|
|
65
|
+
"userAgent":"Mozilla/5.0 (........",
|
|
66
|
+
"gRecaptchaResponse":"P1_eyJ0eXAiOiJKV...1LDq89KyJ5A"
|
|
67
|
+
},
|
|
68
|
+
"cost":"0.00299",
|
|
69
|
+
"ip":"1.2.3.4",
|
|
70
|
+
"createTime":1692863536,
|
|
71
|
+
"endTime":1692863556,
|
|
72
|
+
"solveCount":1,
|
|
73
|
+
"taskId": 73243152973,
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
Returns:
|
|
77
|
+
Dict with full server response
|
|
78
|
+
|
|
79
|
+
Notes:
|
|
80
|
+
https://2captcha.com/api-docs/cutcaptcha
|
|
81
|
+
"""
|
|
82
|
+
super().__init__(method=method, *args, **kwargs)
|
|
83
|
+
|
|
84
|
+
self.create_task_payload["task"].update({"websiteURL": websiteURL, "miseryKey": miseryKey, "apiKey": apiKey})
|
|
85
|
+
|
|
86
|
+
# check user params
|
|
87
|
+
if method not in CutCaptchaEnm.list_values():
|
|
88
|
+
raise ValueError(f"Invalid method parameter set, available - {CutCaptchaEnm.list_values()}")
|
|
89
|
+
|
|
90
|
+
def captcha_handler(self, **kwargs) -> dict:
|
|
91
|
+
"""
|
|
92
|
+
Sync solving method
|
|
93
|
+
|
|
94
|
+
Args:
|
|
95
|
+
kwargs: Parameters for the `requests` library
|
|
96
|
+
|
|
97
|
+
Returns:
|
|
98
|
+
Dict with full server response
|
|
99
|
+
|
|
100
|
+
Notes:
|
|
101
|
+
Check class docstirng for more info
|
|
102
|
+
"""
|
|
103
|
+
|
|
104
|
+
return self._processing_response(**kwargs)
|
|
105
|
+
|
|
106
|
+
async def aio_captcha_handler(self) -> dict:
|
|
107
|
+
"""
|
|
108
|
+
Async solving method
|
|
109
|
+
|
|
110
|
+
Returns:
|
|
111
|
+
Dict with full server response
|
|
112
|
+
|
|
113
|
+
Notes:
|
|
114
|
+
Check class docstirng for more info
|
|
115
|
+
"""
|
|
116
|
+
return await self._aio_processing_response()
|