| 1 |
from __future__ import absolute_import |
| 2 |
|
| 3 |
import json |
| 4 |
import requests |
| 5 |
try: |
| 6 |
from urlparse import urlparse |
| 7 |
except ImportError: |
| 8 |
from urllib.parse import urlparse |
| 9 |
|
| 10 |
try: |
| 11 |
import polling2 |
| 12 |
except ImportError: |
| 13 |
raise ImportError("Please install the python module 'polling2' via pip") |
| 14 |
|
| 15 |
from ..exceptions import ( |
| 16 |
CaptchaException, |
| 17 |
CaptchaServiceUnavailable, |
| 18 |
CaptchaTimeout, |
| 19 |
CaptchaParameter, |
| 20 |
CaptchaBadJobID, |
| 21 |
CaptchaReportError |
| 22 |
) |
| 23 |
|
| 24 |
from . import Captcha |
| 25 |
|
| 26 |
|
| 27 |
class captchaSolver(Captcha): |
| 28 |
|
| 29 |
def __init__(self): |
| 30 |
super(captchaSolver, self).__init__('deathbycaptcha') |
| 31 |
self.host = 'http://api.dbcapi.me/api' |
| 32 |
self.session = requests.Session() |
| 33 |
self.captchaType = { |
| 34 |
'reCaptcha': '4', |
| 35 |
'hCaptcha': '7' |
| 36 |
} |
| 37 |
|
| 38 |
# ------------------------------------------------------------------------------- # |
| 39 |
|
| 40 |
@staticmethod |
| 41 |
def checkErrorStatus(response): |
| 42 |
errors = dict( |
| 43 |
[ |
| 44 |
(400, "DeathByCaptcha: 400 Bad Request"), |
| 45 |
(403, "DeathByCaptcha: 403 Forbidden - Invalid credentails or insufficient credits."), |
| 46 |
# (500, "DeathByCaptcha: 500 Internal Server Error."), |
| 47 |
(503, "DeathByCaptcha: 503 Service Temporarily Unavailable.") |
| 48 |
] |
| 49 |
) |
| 50 |
|
| 51 |
if response.status_code in errors: |
| 52 |
raise CaptchaServiceUnavailable(errors.get(response.status_code)) |
| 53 |
|
| 54 |
# ------------------------------------------------------------------------------- # |
| 55 |
|
| 56 |
def login(self, username, password): |
| 57 |
self.username = username |
| 58 |
self.password = password |
| 59 |
|
| 60 |
def _checkRequest(response): |
| 61 |
if response.ok: |
| 62 |
if response.json().get('is_banned'): |
| 63 |
raise CaptchaServiceUnavailable('DeathByCaptcha: Your account is banned.') |
| 64 |
|
| 65 |
if response.json().get('balanace') == 0: |
| 66 |
raise CaptchaServiceUnavailable('DeathByCaptcha: insufficient credits.') |
| 67 |
|
| 68 |
return response |
| 69 |
|
| 70 |
self.checkErrorStatus(response) |
| 71 |
|
| 72 |
return None |
| 73 |
|
| 74 |
response = polling2.poll( |
| 75 |
lambda: self.session.post( |
| 76 |
f'{self.host}/user', |
| 77 |
headers={'Accept': 'application/json'}, |
| 78 |
data={ |
| 79 |
'username': self.username, |
| 80 |
'password': self.password |
| 81 |
} |
| 82 |
), |
| 83 |
check_success=_checkRequest, |
| 84 |
step=10, |
| 85 |
timeout=120 |
| 86 |
) |
| 87 |
|
| 88 |
self.debugRequest(response) |
| 89 |
|
| 90 |
# ------------------------------------------------------------------------------- # |
| 91 |
|
| 92 |
def reportJob(self, jobID): |
| 93 |
if not jobID: |
| 94 |
raise CaptchaBadJobID( |
| 95 |
"DeathByCaptcha: Error bad job id to report failed reCaptcha." |
| 96 |
) |
| 97 |
|
| 98 |
def _checkRequest(response): |
| 99 |
if response.status_code == 200: |
| 100 |
return response |
| 101 |
|
| 102 |
self.checkErrorStatus(response) |
| 103 |
|
| 104 |
return None |
| 105 |
|
| 106 |
response = polling2.poll( |
| 107 |
lambda: self.session.post( |
| 108 |
f'{self.host}/captcha/{jobID}/report', |
| 109 |
headers={'Accept': 'application/json'}, |
| 110 |
data={ |
| 111 |
'username': self.username, |
| 112 |
'password': self.password |
| 113 |
} |
| 114 |
), |
| 115 |
check_success=_checkRequest, |
| 116 |
step=10, |
| 117 |
timeout=180 |
| 118 |
) |
| 119 |
|
| 120 |
if response: |
| 121 |
return True |
| 122 |
else: |
| 123 |
raise CaptchaReportError( |
| 124 |
"DeathByCaptcha: Error report failed reCaptcha." |
| 125 |
) |
| 126 |
|
| 127 |
# ------------------------------------------------------------------------------- # |
| 128 |
|
| 129 |
def requestJob(self, jobID): |
| 130 |
if not jobID: |
| 131 |
raise CaptchaBadJobID( |
| 132 |
"DeathByCaptcha: Error bad job id to request reCaptcha." |
| 133 |
) |
| 134 |
|
| 135 |
def _checkRequest(response): |
| 136 |
if response.ok and response.json().get('text'): |
| 137 |
return response |
| 138 |
|
| 139 |
self.checkErrorStatus(response) |
| 140 |
|
| 141 |
return None |
| 142 |
|
| 143 |
response = polling2.poll( |
| 144 |
lambda: self.session.get( |
| 145 |
f'{self.host}/captcha/{jobID}', |
| 146 |
headers={'Accept': 'application/json'} |
| 147 |
), |
| 148 |
check_success=_checkRequest, |
| 149 |
step=10, |
| 150 |
timeout=180 |
| 151 |
) |
| 152 |
|
| 153 |
if response: |
| 154 |
return response.json().get('text') |
| 155 |
else: |
| 156 |
raise CaptchaTimeout( |
| 157 |
"DeathByCaptcha: Error failed to solve reCaptcha." |
| 158 |
) |
| 159 |
|
| 160 |
# ------------------------------------------------------------------------------- # |
| 161 |
|
| 162 |
def requestSolve(self, captchaType, url, siteKey): |
| 163 |
def _checkRequest(response): |
| 164 |
if response.ok and response.json().get("is_correct") and response.json().get('captcha'): |
| 165 |
return response |
| 166 |
|
| 167 |
self.checkErrorStatus(response) |
| 168 |
|
| 169 |
return None |
| 170 |
|
| 171 |
data = { |
| 172 |
'username': self.username, |
| 173 |
'password': self.password, |
| 174 |
} |
| 175 |
|
| 176 |
if captchaType == 'reCaptcha': |
| 177 |
jPayload = { |
| 178 |
'googlekey': siteKey, |
| 179 |
'pageurl': url |
| 180 |
} |
| 181 |
|
| 182 |
if self.proxy: |
| 183 |
jPayload.update({ |
| 184 |
'proxy': self.proxy, |
| 185 |
'proxytype': self.proxyType |
| 186 |
}) |
| 187 |
|
| 188 |
data.update({ |
| 189 |
'type': self.captchaType[captchaType], |
| 190 |
'token_params': json.dumps(jPayload) |
| 191 |
}) |
| 192 |
else: |
| 193 |
jPayload = { |
| 194 |
'sitekey': siteKey, |
| 195 |
'pageurl': url |
| 196 |
} |
| 197 |
|
| 198 |
if self.proxy: |
| 199 |
jPayload.update({ |
| 200 |
'proxy': self.proxy, |
| 201 |
'proxytype': self.proxyType |
| 202 |
}) |
| 203 |
|
| 204 |
data.update({ |
| 205 |
'type': self.captchaType[captchaType], |
| 206 |
'hcaptcha_params': json.dumps(jPayload) |
| 207 |
}) |
| 208 |
|
| 209 |
response = polling2.poll( |
| 210 |
lambda: self.session.post( |
| 211 |
f'{self.host}/captcha', |
| 212 |
headers={'Accept': 'application/json'}, |
| 213 |
data=data, |
| 214 |
allow_redirects=False |
| 215 |
), |
| 216 |
check_success=_checkRequest, |
| 217 |
step=10, |
| 218 |
timeout=180 |
| 219 |
) |
| 220 |
|
| 221 |
if response: |
| 222 |
return response.json().get('captcha') |
| 223 |
else: |
| 224 |
raise CaptchaBadJobID( |
| 225 |
'DeathByCaptcha: Error no job id was returned.' |
| 226 |
) |
| 227 |
|
| 228 |
# ------------------------------------------------------------------------------- # |
| 229 |
|
| 230 |
def getCaptchaAnswer(self, captchaType, url, siteKey, captchaParams): |
| 231 |
jobID = None |
| 232 |
|
| 233 |
for param in ['username', 'password']: |
| 234 |
if not captchaParams.get(param): |
| 235 |
raise CaptchaParameter( |
| 236 |
f"DeathByCaptcha: Missing '{param}' parameter." |
| 237 |
) |
| 238 |
setattr(self, param, captchaParams.get(param)) |
| 239 |
|
| 240 |
if captchaParams.get('proxy') and not captchaParams.get('no_proxy'): |
| 241 |
hostParsed = urlparse(captchaParams.get('proxy', {}).get('https')) |
| 242 |
|
| 243 |
if not hostParsed.scheme: |
| 244 |
raise CaptchaParameter('Cannot parse proxy correctly, bad scheme') |
| 245 |
|
| 246 |
if not hostParsed.netloc: |
| 247 |
raise CaptchaParameter('Cannot parse proxy correctly, bad netloc') |
| 248 |
|
| 249 |
self.proxyType = hostParsed.scheme.upper() |
| 250 |
self.proxy = captchaParams.get('proxy', {}).get('https') |
| 251 |
else: |
| 252 |
self.proxy = None |
| 253 |
|
| 254 |
if captchaType not in self.captchaType: |
| 255 |
raise CaptchaException(f'DeathByCaptcha: {captchaType} is not supported by this provider.') |
| 256 |
|
| 257 |
try: |
| 258 |
jobID = self.requestSolve(captchaType, url, siteKey) |
| 259 |
return self.requestJob(jobID) |
| 260 |
except polling2.TimeoutException: |
| 261 |
try: |
| 262 |
if jobID: |
| 263 |
self.reportJob(jobID) |
| 264 |
except polling2.TimeoutException: |
| 265 |
raise CaptchaTimeout( |
| 266 |
f"DeathByCaptcha: Captcha solve took to long and also failed reporting the job id {jobID}." |
| 267 |
) |
| 268 |
|
| 269 |
raise CaptchaTimeout( |
| 270 |
f"DeathByCaptcha: Captcha solve took to long to execute job id {jobID}, aborting." |
| 271 |
) |
| 272 |
|
| 273 |
# ------------------------------------------------------------------------------- # |
| 274 |
|
| 275 |
|
| 276 |
captchaSolver() |