-
Notifications
You must be signed in to change notification settings - Fork 69
/
anycaptcha.py
273 lines (228 loc) · 8.92 KB
/
anycaptcha.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import warnings
from json import loads
from six import PY3
from six.moves.urllib_parse import urljoin
from requests import Session
import time
SLEEP_EVERY_CHECK_FINISHED = 3
MAXIMUM_JOIN_TIME = 200
if PY3:
def split(value, sep, maxsplit):
return value.split(sep, maxsplit=maxsplit)
else:
def split(value, sep, maxsplit):
parts = value.split(sep)
return parts[:maxsplit] + [sep.join(parts[maxsplit:]), ]
class Job(object):
client = None
task_id = None
_last_result = None
def __init__(self, client, task_id, time_sleep=2, typecaptcha=None):
self.client = client
self.task_id = task_id
self.time_sleep = time_sleep
self.typecaptcha = typecaptcha
def _update(self):
self._last_result = self.client.getTaskResult(self.task_id)
def check_is_ready(self):
self._update()
if self._last_result['errorId'] != 0:
return 2
if self._last_result["status"] == "ready":
return 1
return 0
def get_solution_response(self): # Recaptcha
if self._last_result['errorId'] != 0:
return "ERROR|" + self._last_result['errorDescription']
if self.typecaptcha == "funcaptcha":
return self._last_result["solution"]["token"]
if self.typecaptcha == "text":
return self._last_result["solution"]["text"]
return self._last_result["solution"]["gRecaptchaResponse"]
def get_token_response(self): # Funcaptcha
return self._last_result["solution"]["token"]
def get_answers(self):
return self._last_result["solution"]["answers"]
def get_captcha_text(self): # Image
return self._last_result["solution"]["text"]
def get_cells_numbers(self):
return self._last_result["solution"]["cellNumbers"]
def report_incorrect(self):
warnings.warn(
"report_incorrect is deprecated, use report_incorrect_image instead",
DeprecationWarning,
)
return self.client.reportIncorrectImage()
def report_incorrect_image(self):
return self.client.reportIncorrectImage(self.task_id)
def report_incorrect_recaptcha(self):
return self.client.reportIncorrectRecaptcha(self.task_id)
def join(self, maximum_time=200):
elapsed_time = 0
maximum_time = maximum_time or MAXIMUM_JOIN_TIME
while True:
time.sleep(self.time_sleep)
sts = self.check_is_ready()
if sts == 0:
continue
elif sts == 1:
return
else:
return "ERROR"
elapsed_time += SLEEP_EVERY_CHECK_FINISHED
if elapsed_time is not None and elapsed_time > maximum_time:
return "ERROR|TIMEOUT"
while not self.check_is_ready():
time.sleep(self.time_sleep)
elapsed_time += SLEEP_EVERY_CHECK_FINISHED
if elapsed_time is not None and elapsed_time > maximum_time:
return "ERROR|TIMEOUT"
class AnycaptchaClient(object):
client_key = None
CREATE_TASK_URL = "/createTask"
TASK_RESULT_URL = "/getTaskResult"
BALANCE_URL = "/getBalance"
REPORT_IMAGE_URL = "/reportIncorrectImageCaptcha"
REPORT_RECAPTCHA_URL = "/reportIncorrectRecaptcha"
APP_STAT_URL = "/getAppStats"
SOFT_ID = 847
language_pool = "en"
response_timeout = 5
def __init__(
self, client_key, language_pool="en", host="api.anycaptcha.com", use_ssl=True
):
self._client_ip = None
self.client_key = client_key
self.language_pool = language_pool
self.base_url = "{proto}://{host}/".format(
proto="https" if use_ssl else "http", host=host
)
self.session = Session()
@property
def client_ip(self):
if not hasattr(self, "_client_ip"):
self._client_ip = self.session.get(
"https://api.myip.com", timeout=self.response_timeout
).json()["ip"]
return self._client_ip
def _check_response(self, response):
pass
def createTask(self, task, typecaptcha=None):
request = {
"clientKey": self.client_key,
"task": task.serialize(),
"softId": self.SOFT_ID,
"languagePool": self.language_pool,
}
response = self.session.post(
urljoin(self.base_url, self.CREATE_TASK_URL),
json=request,
timeout=self.response_timeout,
).json()
self._check_response(response)
return Job(self, response["taskId"], time_sleep=task.time_sleep, typecaptcha=typecaptcha)
def createTaskSmee(self, task, timeout=MAXIMUM_JOIN_TIME):
"""
Beta method to stream response from smee.io
"""
response = self.session.head(
"https://smee.io/new", timeout=self.response_timeout
)
smee_url = response.headers["Location"]
task = task.serialize()
request = {
"clientKey": self.client_key,
"task": task,
"softId": self.SOFT_ID,
"languagePool": self.language_pool,
"callbackUrl": smee_url,
}
r = self.session.get(
url=smee_url,
headers={"Accept": "text/event-stream"},
stream=True,
timeout=(self.response_timeout, timeout),
)
response = self.session.post(
url=urljoin(self.base_url, self.CREATE_TASK_URL),
json=request,
timeout=self.response_timeout,
).json()
self._check_response(response)
for line in r.iter_lines():
content = line.decode("utf-8")
if '"host":"smee.io"' not in content:
continue
payload = loads(split(content, ":", 1)[1].strip())
if "taskId" not in payload["body"] or str(payload["body"]["taskId"]) != str(
response["taskId"]
):
continue
r.close()
if task["type"] == "CustomCaptchaTask":
payload["body"]["solution"] = payload["body"]["data"][0]
job = Job(client=self, task_id=response["taskId"])
job._last_result = payload["body"]
return job
def getTaskResult(self, task_id):
request = {"clientKey": self.client_key, "taskId": task_id}
response = self.session.post(
urljoin(self.base_url, self.TASK_RESULT_URL), json=request
).json()
self._check_response(response)
return response
def getBalance(self):
request = {
"clientKey": self.client_key,
"softId": self.SOFT_ID,
}
response: dict = self.session.post(
urljoin(self.base_url, self.BALANCE_URL), json=request
).json()
self._check_response(response)
if "errorDescription" in response:
exit("Error while Checking Balance %s" % response["errorDescription"])
return response.get("balance", 0)
def getAppStats(self, soft_id, mode):
request = {"clientKey": self.client_key, "softId": soft_id, "mode": mode}
response = self.session.post(
urljoin(self.base_url, self.APP_STAT_URL), json=request
).json()
self._check_response(response)
return response
def reportIncorrectImage(self, task_id):
request = {"clientKey": self.client_key, "taskId": task_id}
response = self.session.post(
urljoin(self.base_url, self.REPORT_IMAGE_URL), json=request
).json()
self._check_response(response)
return response.get("status", False) != False
def reportIncorrectRecaptcha(self, task_id):
request = {"clientKey": self.client_key, "taskId": task_id}
response = self.session.post(
urljoin(self.base_url, self.REPORT_RECAPTCHA_URL), json=request
).json()
self._check_response(response)
return response["status"] == "success"
class BaseTask(object):
def serialize(self, **result):
return result
class FunCaptchaProxylessTask(BaseTask):
type = "FunCaptchaTaskProxyless"
websiteURL = None
websiteKey = None
time_sleep = 0.1
def __init__(self, website_url, website_key, *args, **kwargs):
self.websiteURL = website_url
self.websiteKey = website_key
super(FunCaptchaProxylessTask, self).__init__(*args, **kwargs)
def serialize(self, **result):
result = super(FunCaptchaProxylessTask, self).serialize(**result)
result.update(
{
"type": self.type,
"websiteURL": self.websiteURL,
"websitePublicKey": self.websiteKey,
}
)
return result