|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import os |
| 4 | +from typing import Any |
| 5 | +from urllib.parse import urlparse |
| 6 | + |
| 7 | +from .exceptions import AnticaptchaException |
| 8 | +from .tasks import BaseTask |
| 9 | + |
| 10 | +SOFT_ID = 847 |
| 11 | +SLEEP_EVERY_CHECK_FINISHED = 3 |
| 12 | +MAXIMUM_JOIN_TIME = 60 * 5 |
| 13 | + |
| 14 | + |
| 15 | +class _BaseClientMixin: |
| 16 | + CREATE_TASK_URL = "/createTask" |
| 17 | + TASK_RESULT_URL = "/getTaskResult" |
| 18 | + BALANCE_URL = "/getBalance" |
| 19 | + REPORT_IMAGE_URL = "/reportIncorrectImageCaptcha" |
| 20 | + REPORT_RECAPTCHA_URL = "/reportIncorrectRecaptcha" |
| 21 | + APP_STAT_URL = "/getAppStats" |
| 22 | + SOFT_ID = SOFT_ID |
| 23 | + language_pool = "en" |
| 24 | + response_timeout = 5 |
| 25 | + |
| 26 | + def _init_client( |
| 27 | + self, client_key: str | None, language_pool: str, host: str, use_ssl: bool, |
| 28 | + ) -> None: |
| 29 | + self.client_key = client_key or os.environ.get("ANTICAPTCHA_API_KEY") |
| 30 | + if not self.client_key: |
| 31 | + raise AnticaptchaException( |
| 32 | + None, |
| 33 | + "CONFIG_ERROR", |
| 34 | + "API key required. Pass client_key or set ANTICAPTCHA_API_KEY env var.", |
| 35 | + ) |
| 36 | + self.language_pool = language_pool |
| 37 | + self.base_url = "{proto}://{host}/".format( |
| 38 | + proto="https" if use_ssl else "http", host=host |
| 39 | + ) |
| 40 | + |
| 41 | + def _build_create_task_request(self, task: BaseTask) -> dict[str, Any]: |
| 42 | + return { |
| 43 | + "clientKey": self.client_key, |
| 44 | + "task": task.serialize(), |
| 45 | + "softId": self.SOFT_ID, |
| 46 | + "languagePool": self.language_pool, |
| 47 | + } |
| 48 | + |
| 49 | + def _build_key_request(self, **extra: Any) -> dict[str, Any]: |
| 50 | + return {"clientKey": self.client_key, **extra} |
| 51 | + |
| 52 | + def _process_check_response( |
| 53 | + self, response: dict[str, Any], client_ip: str | None = None, |
| 54 | + ) -> None: |
| 55 | + if response.get("errorId", False) == 11 and client_ip: |
| 56 | + response[ |
| 57 | + "errorDescription" |
| 58 | + ] = "{} Your missing IP address is probably {}.".format( |
| 59 | + response["errorDescription"], client_ip |
| 60 | + ) |
| 61 | + if response.get("errorId", False): |
| 62 | + raise AnticaptchaException( |
| 63 | + response["errorId"], response["errorCode"], response["errorDescription"] |
| 64 | + ) |
| 65 | + |
| 66 | + def _repr_client(self, class_name: str) -> str: |
| 67 | + host = urlparse(self.base_url).hostname or self.base_url |
| 68 | + return f"<{class_name} host={host!r}>" |
| 69 | + |
| 70 | + |
| 71 | +class _BaseJobMixin: |
| 72 | + client = None |
| 73 | + task_id = None |
| 74 | + _last_result = None |
| 75 | + |
| 76 | + def get_solution_response(self) -> str: # Recaptcha |
| 77 | + return self._last_result["solution"]["gRecaptchaResponse"] |
| 78 | + |
| 79 | + def get_solution(self) -> dict[str, Any]: |
| 80 | + return self._last_result["solution"] |
| 81 | + |
| 82 | + def get_token_response(self) -> str: # Funcaptcha |
| 83 | + return self._last_result["solution"]["token"] |
| 84 | + |
| 85 | + def get_answers(self) -> dict[str, str]: |
| 86 | + return self._last_result["solution"]["answers"] |
| 87 | + |
| 88 | + def get_captcha_text(self) -> str: # Image |
| 89 | + return self._last_result["solution"]["text"] |
| 90 | + |
| 91 | + def get_cells_numbers(self) -> list[int]: |
| 92 | + return self._last_result["solution"]["cellNumbers"] |
| 93 | + |
| 94 | + def _repr_job(self, class_name: str) -> str: |
| 95 | + status = self._last_result.get("status") if self._last_result else None |
| 96 | + if status: |
| 97 | + return f"<{class_name} task_id={self.task_id} status={status!r}>" |
| 98 | + return f"<{class_name} task_id={self.task_id}>" |
0 commit comments