From bba945836dc2e7a1c51fd2829c61e7a6ca6f7cdb Mon Sep 17 00:00:00 2001 From: Andrei Date: Thu, 19 Dec 2024 20:29:22 +0300 Subject: [PATCH] added __all__ fields --- src/python3_anticaptcha/control.py | 21 +++---- .../core/aio_captcha_instrument.py | 55 ++++++++++--------- .../core/sio_captcha_instrument.py | 38 ++++++------- src/python3_anticaptcha/custom_task.py | 2 + src/python3_anticaptcha/fun_captcha.py | 2 + src/python3_anticaptcha/gee_test.py | 2 + src/python3_anticaptcha/recaptcha_v2.py | 2 + src/python3_anticaptcha/recaptcha_v3.py | 2 + src/python3_anticaptcha/turnstile.py | 2 + 9 files changed, 66 insertions(+), 60 deletions(-) diff --git a/src/python3_anticaptcha/control.py b/src/python3_anticaptcha/control.py index d8fcfa2..ce3ad2d 100644 --- a/src/python3_anticaptcha/control.py +++ b/src/python3_anticaptcha/control.py @@ -158,8 +158,7 @@ class Control(CaptchaParams): Notes: https://anti-captcha.com/apidoc/methods/getBalance """ - self._captcha_handling_instrument = AIOCaptchaInstrument(captcha_params=self) - return await self._captcha_handling_instrument.send_post_request( + return await AIOCaptchaInstrument.send_post_request( url_postfix=ControlPostfixEnm.GET_BALANCE, payload={"clientKey": self.create_task_payload.clientKey} ) @@ -346,8 +345,7 @@ class Control(CaptchaParams): Notes: https://anti-captcha.com/apidoc/methods/getSpendingStats """ - self._captcha_handling_instrument = AIOCaptchaInstrument(captcha_params=self) - return await self._captcha_handling_instrument.send_post_request( + return await AIOCaptchaInstrument.send_post_request( url_postfix=ControlPostfixEnm.GET_SPENDING_STATS, payload={"clientKey": self.create_task_payload.clientKey, **kwargs}, ) @@ -431,8 +429,7 @@ class Control(CaptchaParams): Notes: https://anti-captcha.com/apidoc/methods/getAppStats """ - self._captcha_handling_instrument = AIOCaptchaInstrument(captcha_params=self) - return await self._captcha_handling_instrument.send_post_request( + return await AIOCaptchaInstrument.send_post_request( url_postfix=ControlPostfixEnm.GET_APP_STATS, payload={"clientKey": self.create_task_payload.clientKey, "softId": softId, "mode": mode}, ) @@ -480,8 +477,7 @@ class Control(CaptchaParams): Notes: https://anti-captcha.com/apidoc/methods/reportIncorrectImageCaptcha """ - self._captcha_handling_instrument = AIOCaptchaInstrument(captcha_params=self) - return await self._captcha_handling_instrument.send_post_request( + return await AIOCaptchaInstrument.send_post_request( url_postfix=ControlPostfixEnm.REPORT_INCORRECT_IMAGE_CAPTCHA, payload={"clientKey": self.create_task_payload.clientKey, "taskId": taskId}, ) @@ -529,8 +525,7 @@ class Control(CaptchaParams): Notes: https://anti-captcha.com/apidoc/methods/reportIncorrectRecaptcha """ - self._captcha_handling_instrument = AIOCaptchaInstrument(captcha_params=self) - return await self._captcha_handling_instrument.send_post_request( + return await AIOCaptchaInstrument.send_post_request( url_postfix=ControlPostfixEnm.REPORT_INCORRECT_RECAPTCHA, payload={"clientKey": self.create_task_payload.clientKey, "taskId": taskId}, ) @@ -578,8 +573,7 @@ class Control(CaptchaParams): Notes: https://anti-captcha.com/apidoc/methods/reportCorrectRecaptcha """ - self._captcha_handling_instrument = AIOCaptchaInstrument(captcha_params=self) - return await self._captcha_handling_instrument.send_post_request( + return await AIOCaptchaInstrument.send_post_request( url_postfix=ControlPostfixEnm.REPORT_CORRECT_RECAPTCHA, payload={"clientKey": self.create_task_payload.clientKey, "taskId": taskId}, ) @@ -627,8 +621,7 @@ class Control(CaptchaParams): Notes: https://anti-captcha.com/apidoc/methods/reportIncorrectHcaptcha """ - self._captcha_handling_instrument = AIOCaptchaInstrument(captcha_params=self) - return await self._captcha_handling_instrument.send_post_request( + return await AIOCaptchaInstrument.send_post_request( url_postfix=ControlPostfixEnm.REPORT_INCORRECT_HCAPTCHA, payload={"clientKey": self.create_task_payload.clientKey, "taskId": taskId}, ) diff --git a/src/python3_anticaptcha/core/aio_captcha_instrument.py b/src/python3_anticaptcha/core/aio_captcha_instrument.py index 8204608..bacab56 100644 --- a/src/python3_anticaptcha/core/aio_captcha_instrument.py +++ b/src/python3_anticaptcha/core/aio_captcha_instrument.py @@ -101,16 +101,6 @@ class AIOCaptchaInstrument(CaptchaInstrument): self.result.errorId = 12 self.result.errorCode = self.NO_CAPTCHA_ERR - async def _url_read(self, url: str, **kwargs) -> bytes: - """ - Async method read bytes from link - """ - async with aiohttp.ClientSession() as session: - async for attempt in ASYNC_RETRIES: - with attempt: - async with session.get(url=url, **kwargs) as resp: - return await resp.content.read() - async def _create_task(self, url_postfix: str = CREATE_TASK_POSTFIX) -> CreateTaskResponseSer: """ Function send SYNC request to service and wait for result @@ -128,23 +118,6 @@ class AIOCaptchaInstrument(CaptchaInstrument): logging.exception(error) raise - @staticmethod - async def send_post_request(payload: Optional[dict] = None, url_postfix: str = CREATE_TASK_POSTFIX) -> dict: - """ - Function send ASYNC request to service and wait for result - """ - - async with aiohttp.ClientSession() as session: - try: - async with session.post(parse.urljoin(BASE_REQUEST_URL, url_postfix), json=payload) as resp: - if resp.status == 200: - return await resp.json() - else: - raise ValueError(resp.reason) - except Exception as error: - logging.exception(error) - raise - async def _get_result(self, url_response: str = GET_RESULT_POSTFIX) -> dict: attempts = attempts_generator() # Send request for status of captcha solution. @@ -166,3 +139,31 @@ class AIOCaptchaInstrument(CaptchaInstrument): else: json_result.update({"taskId": self.captcha_params.get_result_params.taskId}) return json_result + + @staticmethod + async def _url_read(url: str, **kwargs) -> bytes: + """ + Async method read bytes from link + """ + async with aiohttp.ClientSession() as session: + async for attempt in ASYNC_RETRIES: + with attempt: + async with session.get(url=url, **kwargs) as resp: + return await resp.content.read() + + @staticmethod + async def send_post_request(payload: Optional[dict] = None, url_postfix: str = CREATE_TASK_POSTFIX) -> dict: + """ + Function send ASYNC request to service and wait for result + """ + + async with aiohttp.ClientSession() as session: + try: + async with session.post(parse.urljoin(BASE_REQUEST_URL, url_postfix), json=payload) as resp: + if resp.status == 200: + return await resp.json() + else: + raise ValueError(resp.reason) + except Exception as error: + logging.exception(error) + raise diff --git a/src/python3_anticaptcha/core/sio_captcha_instrument.py b/src/python3_anticaptcha/core/sio_captcha_instrument.py index 0f4304f..9ae231c 100644 --- a/src/python3_anticaptcha/core/sio_captcha_instrument.py +++ b/src/python3_anticaptcha/core/sio_captcha_instrument.py @@ -123,25 +123,6 @@ class SIOCaptchaInstrument(CaptchaInstrument): logging.exception(error) raise - @staticmethod - def send_post_request( - payload: Optional[dict] = None, - session: requests.Session = requests.Session(), - url_postfix: str = CREATE_TASK_POSTFIX, - ) -> dict: - """ - Function send SYNC request to service and wait for result - """ - try: - resp = session.post(parse.urljoin(BASE_REQUEST_URL, url_postfix), json=payload) - if resp.status_code == 200: - return resp.json() - else: - raise ValueError(resp.raise_for_status()) - except Exception as error: - logging.exception(error) - raise - def _url_read(self, url: str, **kwargs): """ Method open links @@ -166,3 +147,22 @@ class SIOCaptchaInstrument(CaptchaInstrument): else: self.session.close() return captcha_response.to_dict() + + @staticmethod + def send_post_request( + payload: Optional[dict] = None, + session: requests.Session = requests.Session(), + url_postfix: str = CREATE_TASK_POSTFIX, + ) -> dict: + """ + Function send SYNC request to service and wait for result + """ + try: + resp = session.post(parse.urljoin(BASE_REQUEST_URL, url_postfix), json=payload) + if resp.status_code == 200: + return resp.json() + else: + raise ValueError(resp.raise_for_status()) + except Exception as error: + logging.exception(error) + raise diff --git a/src/python3_anticaptcha/custom_task.py b/src/python3_anticaptcha/custom_task.py index fd7889b..6e7cd9a 100644 --- a/src/python3_anticaptcha/custom_task.py +++ b/src/python3_anticaptcha/custom_task.py @@ -3,6 +3,8 @@ from typing import Optional from .core.base import CaptchaParams from .core.enum import ProxyTypeEnm, CaptchaTypeEnm +__all__ = ("CustomTask",) + class CustomTask(CaptchaParams): def __init__( diff --git a/src/python3_anticaptcha/fun_captcha.py b/src/python3_anticaptcha/fun_captcha.py index c4a2f52..bf08509 100644 --- a/src/python3_anticaptcha/fun_captcha.py +++ b/src/python3_anticaptcha/fun_captcha.py @@ -3,6 +3,8 @@ from typing import Union, Optional from .core.base import CaptchaParams from .core.enum import ProxyTypeEnm, CaptchaTypeEnm +__all__ = ("FunCaptcha",) + class FunCaptcha(CaptchaParams): def __init__( diff --git a/src/python3_anticaptcha/gee_test.py b/src/python3_anticaptcha/gee_test.py index cf4cbe3..16baab2 100644 --- a/src/python3_anticaptcha/gee_test.py +++ b/src/python3_anticaptcha/gee_test.py @@ -3,6 +3,8 @@ from typing import Union, Optional from .core.base import CaptchaParams from .core.enum import ProxyTypeEnm, CaptchaTypeEnm +__all__ = ("GeeTest",) + class GeeTest(CaptchaParams): def __init__( diff --git a/src/python3_anticaptcha/recaptcha_v2.py b/src/python3_anticaptcha/recaptcha_v2.py index cb960c0..f26f671 100644 --- a/src/python3_anticaptcha/recaptcha_v2.py +++ b/src/python3_anticaptcha/recaptcha_v2.py @@ -3,6 +3,8 @@ from typing import Union, Optional from .core.base import CaptchaParams from .core.enum import ProxyTypeEnm, CaptchaTypeEnm +__all__ = ("ReCaptchaV2",) + class ReCaptchaV2(CaptchaParams): def __init__( diff --git a/src/python3_anticaptcha/recaptcha_v3.py b/src/python3_anticaptcha/recaptcha_v3.py index 861dffe..b72fec4 100644 --- a/src/python3_anticaptcha/recaptcha_v3.py +++ b/src/python3_anticaptcha/recaptcha_v3.py @@ -3,6 +3,8 @@ from typing import Optional from .core.base import CaptchaParams from .core.enum import CaptchaTypeEnm +__all__ = ("ReCaptchaV3",) + class ReCaptchaV3(CaptchaParams): def __init__( diff --git a/src/python3_anticaptcha/turnstile.py b/src/python3_anticaptcha/turnstile.py index 7b19f29..a8708d7 100644 --- a/src/python3_anticaptcha/turnstile.py +++ b/src/python3_anticaptcha/turnstile.py @@ -3,6 +3,8 @@ from typing import Union, Optional from .core.base import CaptchaParams from .core.enum import ProxyTypeEnm, CaptchaTypeEnm +__all__ = ("Turnstile",) + class Turnstile(CaptchaParams): def __init__(