| 1 |
from __future__ import absolute_import |
| 2 |
|
| 3 |
import re |
| 4 |
import requests |
| 5 |
|
| 6 |
try: |
| 7 |
import polling |
| 8 |
except ImportError: |
| 9 |
raise ImportError( |
| 10 |
"Please install the python module 'polling' via pip or download it from " |
| 11 |
"https://github.com/justiniso/polling/" |
| 12 |
) |
| 13 |
|
| 14 |
from ..exceptions import ( |
| 15 |
CaptchaException, |
| 16 |
CaptchaServiceUnavailable, |
| 17 |
CaptchaAPIError, |
| 18 |
CaptchaTimeout, |
| 19 |
CaptchaParameter, |
| 20 |
CaptchaBadJobID |
| 21 |
) |
| 22 |
|
| 23 |
from . import Captcha |
| 24 |
|
| 25 |
|
| 26 |
class captchaSolver(Captcha): |
| 27 |
|
| 28 |
def __init__(self): |
| 29 |
super(captchaSolver, self).__init__('9kw') |
| 30 |
self.host = 'https://www.9kw.eu/index.cgi' |
| 31 |
self.maxtimeout = 180 |
| 32 |
self.session = requests.Session() |
| 33 |
self.captchaType = { |
| 34 |
'reCaptcha': 'recaptchav2', |
| 35 |
'hCaptcha': 'hcaptcha' |
| 36 |
} |
| 37 |
|
| 38 |
# ------------------------------------------------------------------------------- # |
| 39 |
|
| 40 |
@staticmethod |
| 41 |
def checkErrorStatus(response): |
| 42 |
if response.status_code in [500, 502]: |
| 43 |
raise CaptchaServiceUnavailable( |
| 44 |
f'9kw: Server Side Error {response.status_code}' |
| 45 |
) |
| 46 |
|
| 47 |
error_codes = { |
| 48 |
1: 'No API Key available.', |
| 49 |
2: 'No API key found.', |
| 50 |
3: 'No active API key found.', |
| 51 |
4: 'API Key has been disabled by the operator. ', |
| 52 |
5: 'No user found.', |
| 53 |
6: 'No data found.', |
| 54 |
7: 'Found No ID.', |
| 55 |
8: 'found No captcha.', |
| 56 |
9: 'No image found.', |
| 57 |
10: 'Image size not allowed.', |
| 58 |
11: 'credit is not sufficient.', |
| 59 |
12: 'what was done.', |
| 60 |
13: 'No answer contain.', |
| 61 |
14: 'Captcha already been answered.', |
| 62 |
15: 'Captcha to quickly filed.', |
| 63 |
16: 'JD check active.', |
| 64 |
17: 'Unknown problem.', |
| 65 |
18: 'Found No ID.', |
| 66 |
19: 'Incorrect answer.', |
| 67 |
20: 'Do not timely filed (Incorrect UserID).', |
| 68 |
21: 'Link not allowed.', |
| 69 |
22: 'Prohibited submit.', |
| 70 |
23: 'Entering prohibited.', |
| 71 |
24: 'Too little credit.', |
| 72 |
25: 'No entry found.', |
| 73 |
26: 'No Conditions accepted.', |
| 74 |
27: 'No coupon code found in the database.', |
| 75 |
28: 'Already unused voucher code.', |
| 76 |
29: 'maxTimeout under 60 seconds.', |
| 77 |
30: 'User not found.', |
| 78 |
31: 'An account is not yet 24 hours in system.', |
| 79 |
32: 'An account does not have the full rights.', |
| 80 |
33: 'Plugin needed a update.', |
| 81 |
34: 'No HTTPS allowed.', |
| 82 |
35: 'No HTTP allowed.', |
| 83 |
36: 'Source not allowed.', |
| 84 |
37: 'Transfer denied.', |
| 85 |
38: 'Incorrect answer without space', |
| 86 |
39: 'Incorrect answer with space', |
| 87 |
40: 'Incorrect answer with not only numbers', |
| 88 |
41: 'Incorrect answer with not only A-Z, a-z', |
| 89 |
42: 'Incorrect answer with not only 0-9, A-Z, a-z', |
| 90 |
43: 'Incorrect answer with not only [0-9,- ]', |
| 91 |
44: 'Incorrect answer with not only [0-9A-Za-z,- ]', |
| 92 |
45: 'Incorrect answer with not only coordinates', |
| 93 |
46: 'Incorrect answer with not only multiple coordinates', |
| 94 |
47: 'Incorrect answer with not only data', |
| 95 |
48: 'Incorrect answer with not only rotate number', |
| 96 |
49: 'Incorrect answer with not only text', |
| 97 |
50: 'Incorrect answer with not only text and too short', |
| 98 |
51: 'Incorrect answer with not enough chars', |
| 99 |
52: 'Incorrect answer with too many chars', |
| 100 |
53: 'Incorrect answer without no or yes', |
| 101 |
54: 'Assignment was not found.' |
| 102 |
} |
| 103 |
|
| 104 |
if response.text.startswith('{'): |
| 105 |
if response.json().get('error'): |
| 106 |
raise CaptchaAPIError(error_codes.get(int(response.json().get('error')))) |
| 107 |
else: |
| 108 |
error_code = int(re.search(r'^00(?P<error_code>\d+)', response.text).groupdict().get('error_code', 0)) |
| 109 |
if error_code: |
| 110 |
raise CaptchaAPIError(error_codes.get(error_code)) |
| 111 |
|
| 112 |
# ------------------------------------------------------------------------------- # |
| 113 |
|
| 114 |
def requestJob(self, jobID): |
| 115 |
if not jobID: |
| 116 |
raise CaptchaBadJobID( |
| 117 |
"9kw: Error bad job id to request against." |
| 118 |
) |
| 119 |
|
| 120 |
def _checkRequest(response): |
| 121 |
if response.ok and response.json().get('answer') != 'NO DATA': |
| 122 |
return response |
| 123 |
|
| 124 |
self.checkErrorStatus(response) |
| 125 |
|
| 126 |
return None |
| 127 |
|
| 128 |
response = polling.poll( |
| 129 |
lambda: self.session.get( |
| 130 |
self.host, |
| 131 |
params={ |
| 132 |
'apikey': self.api_key, |
| 133 |
'action': 'usercaptchacorrectdata', |
| 134 |
'id': jobID, |
| 135 |
'info': 1, |
| 136 |
'json': 1 |
| 137 |
} |
| 138 |
), |
| 139 |
check_success=_checkRequest, |
| 140 |
step=10, |
| 141 |
timeout=(self.maxtimeout + 10) |
| 142 |
) |
| 143 |
|
| 144 |
if response: |
| 145 |
return response.json().get('answer') |
| 146 |
else: |
| 147 |
raise CaptchaTimeout("9kw: Error failed to solve.") |
| 148 |
|
| 149 |
# ------------------------------------------------------------------------------- # |
| 150 |
|
| 151 |
def requestSolve(self, captchaType, url, siteKey): |
| 152 |
def _checkRequest(response): |
| 153 |
if response.ok and response.text.startswith('{') and response.json().get('captchaid'): |
| 154 |
return response |
| 155 |
|
| 156 |
self.checkErrorStatus(response) |
| 157 |
|
| 158 |
return None |
| 159 |
|
| 160 |
response = polling.poll( |
| 161 |
lambda: self.session.post( |
| 162 |
self.host, |
| 163 |
data={ |
| 164 |
'apikey': self.api_key, |
| 165 |
'action': 'usercaptchaupload', |
| 166 |
'interactive': 1, |
| 167 |
'file-upload-01': siteKey, |
| 168 |
'oldsource': self.captchaType[captchaType], |
| 169 |
'pageurl': url, |
| 170 |
'maxtimeout': self.maxtimeout, |
| 171 |
'json': 1 |
| 172 |
}, |
| 173 |
allow_redirects=False |
| 174 |
), |
| 175 |
check_success=_checkRequest, |
| 176 |
step=5, |
| 177 |
timeout=(self.maxtimeout + 10) |
| 178 |
) |
| 179 |
|
| 180 |
if response: |
| 181 |
return response.json().get('captchaid') |
| 182 |
else: |
| 183 |
raise CaptchaBadJobID('9kw: Error no valid job id was returned.') |
| 184 |
|
| 185 |
# ------------------------------------------------------------------------------- # |
| 186 |
def getCaptchaAnswer(self, captchaType, url, siteKey, captchaParams): |
| 187 |
jobID = None |
| 188 |
|
| 189 |
if not captchaParams.get('api_key'): |
| 190 |
raise CaptchaParameter("9kw: Missing api_key parameter.") |
| 191 |
|
| 192 |
self.api_key = captchaParams.get('api_key') |
| 193 |
|
| 194 |
if captchaParams.get('maxtimeout'): |
| 195 |
self.maxtimeout = captchaParams.get('maxtimeout') |
| 196 |
|
| 197 |
if captchaParams.get('proxy'): |
| 198 |
self.session.proxies = captchaParams.get('proxies') |
| 199 |
|
| 200 |
if captchaType not in self.captchaType: |
| 201 |
raise CaptchaException(f'9kw: {captchaType} is not supported by this provider.') |
| 202 |
|
| 203 |
try: |
| 204 |
jobID = self.requestSolve(captchaType, url, siteKey) |
| 205 |
return self.requestJob(jobID) |
| 206 |
except polling.TimeoutException: |
| 207 |
raise CaptchaTimeout( |
| 208 |
f"9kw: solve took to long to execute 'captchaid' {jobID}, aborting." |
| 209 |
) |
| 210 |
|
| 211 |
|
| 212 |
# ------------------------------------------------------------------------------- # |
| 213 |
|
| 214 |
captchaSolver() |