| 1 |
from __future__ import absolute_import |
| 2 |
|
| 3 |
import requests |
| 4 |
|
| 5 |
try: |
| 6 |
from urlparse import urlparse |
| 7 |
except ImportError: |
| 8 |
from urllib.parse import urlparse |
| 9 |
|
| 10 |
from ..exceptions import ( |
| 11 |
CaptchaServiceUnavailable, |
| 12 |
CaptchaAPIError, |
| 13 |
CaptchaTimeout, |
| 14 |
CaptchaParameter, |
| 15 |
CaptchaBadJobID |
| 16 |
) |
| 17 |
|
| 18 |
try: |
| 19 |
import polling2 |
| 20 |
except ImportError: |
| 21 |
raise ImportError("Please install the python module 'polling2' via pip") |
| 22 |
|
| 23 |
from . import Captcha |
| 24 |
|
| 25 |
|
| 26 |
class captchaSolver(Captcha): |
| 27 |
|
| 28 |
def __init__(self): |
| 29 |
super(captchaSolver, self).__init__('capmonster') |
| 30 |
self.host = 'https://api.capmonster.cloud' |
| 31 |
self.session = requests.Session() |
| 32 |
self.captchaType = { |
| 33 |
'reCaptcha': 'NoCaptchaTask', |
| 34 |
'hCaptcha': 'HCaptchaTask', |
| 35 |
'turnstile': 'TurnstileTask' |
| 36 |
} |
| 37 |
|
| 38 |
# ------------------------------------------------------------------------------- # |
| 39 |
|
| 40 |
@staticmethod |
| 41 |
def checkErrorStatus(response): |
| 42 |
if response.status_code in [500, 502]: |
| 43 |
raise CaptchaServiceUnavailable( |
| 44 |
f'CapMonster: Server Side Error {response.status_code}' |
| 45 |
) |
| 46 |
|
| 47 |
payload = response.json() |
| 48 |
if payload['errorId'] == 1: |
| 49 |
if 'errorDescription' in payload: |
| 50 |
raise CaptchaAPIError( |
| 51 |
payload['errorDescription'] |
| 52 |
) |
| 53 |
else: |
| 54 |
raise CaptchaAPIError(payload['errorCode']) |
| 55 |
|
| 56 |
# ------------------------------------------------------------------------------- # |
| 57 |
|
| 58 |
def requestJob(self, taskID): |
| 59 |
if not taskID: |
| 60 |
raise CaptchaBadJobID( |
| 61 |
'CapMonster: Error bad task id to request Captcha.' |
| 62 |
) |
| 63 |
|
| 64 |
def _checkRequest(response): |
| 65 |
self.checkErrorStatus(response) |
| 66 |
|
| 67 |
if response.ok and response.json()['status'] == 'ready': |
| 68 |
return True |
| 69 |
|
| 70 |
return None |
| 71 |
|
| 72 |
response = polling2.poll( |
| 73 |
lambda: self.session.post( |
| 74 |
f'{self.host}/getTaskResult', |
| 75 |
json={ |
| 76 |
'clientKey': self.clientKey, |
| 77 |
'taskId': taskID |
| 78 |
}, |
| 79 |
timeout=30 |
| 80 |
), |
| 81 |
check_success=_checkRequest, |
| 82 |
step=5, |
| 83 |
timeout=180 |
| 84 |
) |
| 85 |
|
| 86 |
if response: |
| 87 |
payload = response.json()['solution'] |
| 88 |
if 'token' in payload: |
| 89 |
return payload['token'] |
| 90 |
else: |
| 91 |
return payload['gRecaptchaResponse'] |
| 92 |
else: |
| 93 |
raise CaptchaTimeout( |
| 94 |
"CapMonster: Error failed to solve Captcha." |
| 95 |
) |
| 96 |
|
| 97 |
# ------------------------------------------------------------------------------- # |
| 98 |
|
| 99 |
def requestSolve(self, captchaType, url, siteKey): |
| 100 |
def _checkRequest(response): |
| 101 |
self.checkErrorStatus(response) |
| 102 |
|
| 103 |
if response.ok and response.json()['taskId']: |
| 104 |
return True |
| 105 |
|
| 106 |
return None |
| 107 |
|
| 108 |
data = { |
| 109 |
'clientKey': self.clientKey, |
| 110 |
'task': { |
| 111 |
'websiteURL': url, |
| 112 |
'websiteKey': siteKey, |
| 113 |
'type': self.captchaType[captchaType] |
| 114 |
}, |
| 115 |
'softId': 37 |
| 116 |
} |
| 117 |
|
| 118 |
if self.proxy: |
| 119 |
data['task'].update(self.proxy) |
| 120 |
else: |
| 121 |
data['task']['type'] = f"{data['task']['type']}Proxyless" |
| 122 |
|
| 123 |
response = polling2.poll( |
| 124 |
lambda: self.session.post( |
| 125 |
f'{self.host}/createTask', |
| 126 |
json=data, |
| 127 |
allow_redirects=False, |
| 128 |
timeout=30 |
| 129 |
), |
| 130 |
check_success=_checkRequest, |
| 131 |
step=5, |
| 132 |
timeout=180 |
| 133 |
) |
| 134 |
|
| 135 |
if response: |
| 136 |
return response.json()['taskId'] |
| 137 |
else: |
| 138 |
raise CaptchaBadJobID( |
| 139 |
'CapMonster: Error no task id was returned.' |
| 140 |
) |
| 141 |
|
| 142 |
# ------------------------------------------------------------------------------- # |
| 143 |
|
| 144 |
def getCaptchaAnswer(self, captchaType, url, siteKey, captchaParams): |
| 145 |
taskID = None |
| 146 |
|
| 147 |
if not captchaParams.get('clientKey'): |
| 148 |
raise CaptchaParameter( |
| 149 |
"CapMonster: Missing clientKey parameter." |
| 150 |
) |
| 151 |
|
| 152 |
self.clientKey = captchaParams.get('clientKey') |
| 153 |
|
| 154 |
if captchaParams.get('proxy') and not captchaParams.get('no_proxy'): |
| 155 |
hostParsed = urlparse(captchaParams.get('proxy', {}).get('https')) |
| 156 |
|
| 157 |
if not hostParsed.scheme: |
| 158 |
raise CaptchaParameter('Cannot parse proxy correctly, bad scheme') |
| 159 |
|
| 160 |
if not hostParsed.netloc: |
| 161 |
raise CaptchaParameter('Cannot parse proxy correctly, bad netloc') |
| 162 |
|
| 163 |
ports = { |
| 164 |
'http': 80, |
| 165 |
'https': 443 |
| 166 |
} |
| 167 |
|
| 168 |
self.proxy = { |
| 169 |
'proxyType': hostParsed.scheme, |
| 170 |
'proxyAddress': hostParsed.hostname, |
| 171 |
'proxyPort': hostParsed.port if hostParsed.port else ports[self.proxy['proxyType']], |
| 172 |
'proxyLogin': hostParsed.username, |
| 173 |
'proxyPassword': hostParsed.password, |
| 174 |
} |
| 175 |
else: |
| 176 |
self.proxy = None |
| 177 |
|
| 178 |
try: |
| 179 |
taskID = self.requestSolve(captchaType, url, siteKey) |
| 180 |
return self.requestJob(taskID) |
| 181 |
except polling2.TimeoutException: |
| 182 |
try: |
| 183 |
if taskID: |
| 184 |
self.reportJob(taskID) |
| 185 |
except polling2.TimeoutException: |
| 186 |
raise CaptchaTimeout( |
| 187 |
"CapMonster: Captcha solve took to long and also failed " |
| 188 |
f"reporting the task with task id {taskID}." |
| 189 |
) |
| 190 |
|
| 191 |
raise CaptchaTimeout( |
| 192 |
"CapMonster: Captcha solve took to long to execute " |
| 193 |
f"task id {taskID}, aborting." |
| 194 |
) |
| 195 |
|
| 196 |
|
| 197 |
# ------------------------------------------------------------------------------- # |
| 198 |
|
| 199 |
captchaSolver() |