| 1 |
import abc |
| 2 |
import logging |
| 3 |
import sys |
| 4 |
|
| 5 |
if sys.version_info >= (3, 4): |
| 6 |
ABC = abc.ABC # noqa |
| 7 |
else: |
| 8 |
ABC = abc.ABCMeta('ABC', (), {}) |
| 9 |
|
| 10 |
# ------------------------------------------------------------------------------- # |
| 11 |
|
| 12 |
captchaSolvers = {} |
| 13 |
|
| 14 |
# ------------------------------------------------------------------------------- # |
| 15 |
|
| 16 |
|
| 17 |
class Captcha(ABC): |
| 18 |
@abc.abstractmethod |
| 19 |
def __init__(self, name): |
| 20 |
captchaSolvers[name] = self |
| 21 |
|
| 22 |
# ------------------------------------------------------------------------------- # |
| 23 |
|
| 24 |
@classmethod |
| 25 |
def dynamicImport(cls, name): |
| 26 |
if name not in captchaSolvers: |
| 27 |
try: |
| 28 |
__import__(f'{cls.__module__}.{name}') |
| 29 |
if not isinstance(captchaSolvers.get(name), Captcha): |
| 30 |
raise ImportError('The anti captcha provider was not initialized.') |
| 31 |
except ImportError as e: |
| 32 |
sys.tracebacklimit = 0 |
| 33 |
logging.error(f'Unable to load {name} anti captcha provider -> {e}') |
| 34 |
raise |
| 35 |
|
| 36 |
return captchaSolvers[name] |
| 37 |
|
| 38 |
# ------------------------------------------------------------------------------- # |
| 39 |
|
| 40 |
@abc.abstractmethod |
| 41 |
def getCaptchaAnswer(self, captchaType, url, siteKey, captchaParams): |
| 42 |
pass |
| 43 |
|
| 44 |
# ------------------------------------------------------------------------------- # |
| 45 |
|
| 46 |
def solveCaptcha(self, captchaType, url, siteKey, captchaParams): |
| 47 |
return self.getCaptchaAnswer(captchaType, url, siteKey, captchaParams) |