| 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 |
def __init__(self): |
| 28 |
super(captchaSolver, self).__init__('capsolver') |
| 29 |
self.host = 'https://api.capsolver.com' |
| 30 |
self.session = requests.Session() |
| 31 |
self.captchaType = { |
| 32 |
'reCaptcha': 'ReCaptchaV2Task', |
| 33 |
'hCaptcha': 'HCaptchaTask', |
| 34 |
'turnstile': 'AntiCloudflareTask' |
| 35 |
} |
| 36 |
|
| 37 |
# ------------------------------------------------------------------------------- # |
| 38 |
|
| 39 |
@staticmethod |
| 40 |
def checkErrorStatus(response, fnct): |
| 41 |
if response.status_code in [500, 502]: |
| 42 |
raise CaptchaServiceUnavailable(f'CapSolver: Server Side Error {response.status_code}') |
| 43 |
|
| 44 |
try: |
| 45 |
rPayload = response.json() |
| 46 |
except Exception: |
| 47 |
return |
| 48 |
|
| 49 |
if rPayload.get('errorDescription', False) and 'Current system busy' not in rPayload['errorDescription']: |
| 50 |
raise CaptchaAPIError( |
| 51 |
f"CapSolver -> {fnct} -> {rPayload.get('errorDescription')}" |
| 52 |
) |
| 53 |
|
| 54 |
# ------------------------------------------------------------------------------- # |
| 55 |
|
| 56 |
def requestJob(self, jobID): |
| 57 |
if not jobID: |
| 58 |
raise CaptchaBadJobID("CapSolver: Error bad job id to request task result.") |
| 59 |
|
| 60 |
def _checkRequest(response): |
| 61 |
self.checkErrorStatus(response, 'requestJob') |
| 62 |
try: |
| 63 |
if response.ok and response.json()['status'] == 'ready': |
| 64 |
return True |
| 65 |
except Exception: |
| 66 |
pass |
| 67 |
return None |
| 68 |
|
| 69 |
response = polling2.poll( |
| 70 |
lambda: self.session.post( |
| 71 |
f'{self.host}/getTaskResult', |
| 72 |
json={ |
| 73 |
'clientKey': self.api_key, |
| 74 |
'taskId': jobID |
| 75 |
}, |
| 76 |
timeout=30 |
| 77 |
), |
| 78 |
check_success=_checkRequest, |
| 79 |
step=5, |
| 80 |
timeout=180 |
| 81 |
) |
| 82 |
|
| 83 |
if response: |
| 84 |
try: |
| 85 |
rPayload = response.json()['solution'] |
| 86 |
if 'token' in rPayload: |
| 87 |
return rPayload['token'] |
| 88 |
else: |
| 89 |
return rPayload['gRecaptchaResponse'] |
| 90 |
except Exception: |
| 91 |
pass |
| 92 |
|
| 93 |
raise CaptchaTimeout( |
| 94 |
"CapSolver: Error failed to solve Captcha." |
| 95 |
) |
| 96 |
|
| 97 |
# ------------------------------------------------------------------------------- # |
| 98 |
|
| 99 |
def requestSolve(self, captchaType, url, siteKey): |
| 100 |
|
| 101 |
# ------------------------------------------------------------------------------- # |
| 102 |
|
| 103 |
def _checkRequest(response): |
| 104 |
self.checkErrorStatus(response, 'createTask') |
| 105 |
try: |
| 106 |
rPayload = response.json() |
| 107 |
if response.ok: |
| 108 |
if rPayload.get("taskId", False): |
| 109 |
return True |
| 110 |
except Exception: |
| 111 |
pass |
| 112 |
return None |
| 113 |
|
| 114 |
# ------------------------------------------------------------------------------- # |
| 115 |
|
| 116 |
payload = { |
| 117 |
'clientKey': self.api_key, |
| 118 |
'appId': '9E717405-8C70-49B3-B277-7C2F2196484B', |
| 119 |
'task': { |
| 120 |
'type': self.captchaType[captchaType], |
| 121 |
'websiteURL': url, |
| 122 |
'websiteKey': siteKey |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
if captchaType == 'turnstile': |
| 127 |
payload['task']['metadata'] = {'type': 'turnstile'} |
| 128 |
|
| 129 |
if self.proxy: |
| 130 |
payload['task']['proxy'] = self.proxy |
| 131 |
else: |
| 132 |
payload['task']['type'] = f"{self.captchaType[captchaType]}Proxyless" |
| 133 |
|
| 134 |
response = polling2.poll( |
| 135 |
lambda: self.session.post( |
| 136 |
f'{self.host}/createTask', |
| 137 |
json=payload, |
| 138 |
allow_redirects=False, |
| 139 |
timeout=30 |
| 140 |
), |
| 141 |
check_success=_checkRequest, |
| 142 |
step=5, |
| 143 |
timeout=180 |
| 144 |
) |
| 145 |
|
| 146 |
if response: |
| 147 |
rPayload = response.json() |
| 148 |
if rPayload.get('taskId'): |
| 149 |
return rPayload['taskId'] |
| 150 |
|
| 151 |
raise CaptchaBadJobID( |
| 152 |
'CapSolver: Error no job id was returned.' |
| 153 |
) |
| 154 |
|
| 155 |
# ------------------------------------------------------------------------------- # |
| 156 |
|
| 157 |
def getCaptchaAnswer(self, captchaType, url, siteKey, captchaParams): |
| 158 |
if not captchaParams.get('api_key'): |
| 159 |
raise CaptchaParameter("CapSolver: Missing api_key parameter.") |
| 160 |
self.api_key = captchaParams.get('api_key') |
| 161 |
|
| 162 |
if captchaParams.get('proxy') and not captchaParams.get('no_proxy'): |
| 163 |
hostParsed = urlparse(captchaParams.get('proxy', {}).get('https')) |
| 164 |
|
| 165 |
if not hostParsed.scheme: |
| 166 |
raise CaptchaParameter('Cannot parse proxy correctly, bad scheme') |
| 167 |
|
| 168 |
if not hostParsed.netloc: |
| 169 |
raise CaptchaParameter('Cannot parse proxy correctly, bad netloc') |
| 170 |
|
| 171 |
self.proxy = captchaParams['proxy']['https'] |
| 172 |
else: |
| 173 |
self.proxy = None |
| 174 |
|
| 175 |
try: |
| 176 |
jobID = self.requestSolve(captchaType, url, siteKey) |
| 177 |
return self.requestJob(jobID) |
| 178 |
except polling2.TimeoutException: |
| 179 |
raise CaptchaTimeout( |
| 180 |
f"CapSolver: Captcha solve (task ID: {jobID}) took to long." |
| 181 |
) |
| 182 |
|
| 183 |
raise CaptchaAPIError('CapSolver: Job Failure.') |
| 184 |
|
| 185 |
|
| 186 |
# ------------------------------------------------------------------------------- # |
| 187 |
|
| 188 |
captchaSolver() |