Skip to content

Commit

Permalink
added __all__ fields
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiDrang committed Dec 19, 2024
1 parent 9dffd3b commit bba9458
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 60 deletions.
21 changes: 7 additions & 14 deletions src/python3_anticaptcha/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ async def aio_get_balance(self) -> dict:
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}
)

Expand Down Expand Up @@ -346,8 +345,7 @@ async def aio_get_spending_stats(self, **kwargs) -> dict:
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},
)
Expand Down Expand Up @@ -431,8 +429,7 @@ async def aio_get_app_stats(self, softId: int, mode: Optional[str] = None) -> di
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},
)
Expand Down Expand Up @@ -480,8 +477,7 @@ async def aio_report_incorrect_image(self, taskId: int) -> dict:
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},
)
Expand Down Expand Up @@ -529,8 +525,7 @@ async def aio_report_incorrect_recaptcha(self, taskId: int) -> dict:
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},
)
Expand Down Expand Up @@ -578,8 +573,7 @@ async def aio_report_correct_recaptcha(self, taskId: int) -> dict:
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},
)
Expand Down Expand Up @@ -627,8 +621,7 @@ async def aio_report_incorrect_hcaptcha(self, taskId: int) -> dict:
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},
)
55 changes: 28 additions & 27 deletions src/python3_anticaptcha/core/aio_captcha_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,6 @@ async def __body_file_processing(
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
Expand All @@ -128,23 +118,6 @@ async def _create_task(self, url_postfix: str = CREATE_TASK_POSTFIX) -> CreateTa
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.
Expand All @@ -166,3 +139,31 @@ async def _get_result(self, url_response: str = GET_RESULT_POSTFIX) -> dict:
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
38 changes: 19 additions & 19 deletions src/python3_anticaptcha/core/sio_captcha_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,25 +123,6 @@ def _create_task(self, url_postfix: str = CREATE_TASK_POSTFIX) -> CreateTaskResp
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
Expand All @@ -166,3 +147,22 @@ def _get_result(self, url_response: str = GET_RESULT_POSTFIX) -> dict:
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
2 changes: 2 additions & 0 deletions src/python3_anticaptcha/custom_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from .core.base import CaptchaParams
from .core.enum import ProxyTypeEnm, CaptchaTypeEnm

__all__ = ("CustomTask",)


class CustomTask(CaptchaParams):
def __init__(
Expand Down
2 changes: 2 additions & 0 deletions src/python3_anticaptcha/fun_captcha.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from .core.base import CaptchaParams
from .core.enum import ProxyTypeEnm, CaptchaTypeEnm

__all__ = ("FunCaptcha",)


class FunCaptcha(CaptchaParams):
def __init__(
Expand Down
2 changes: 2 additions & 0 deletions src/python3_anticaptcha/gee_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from .core.base import CaptchaParams
from .core.enum import ProxyTypeEnm, CaptchaTypeEnm

__all__ = ("GeeTest",)


class GeeTest(CaptchaParams):
def __init__(
Expand Down
2 changes: 2 additions & 0 deletions src/python3_anticaptcha/recaptcha_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from .core.base import CaptchaParams
from .core.enum import ProxyTypeEnm, CaptchaTypeEnm

__all__ = ("ReCaptchaV2",)


class ReCaptchaV2(CaptchaParams):
def __init__(
Expand Down
2 changes: 2 additions & 0 deletions src/python3_anticaptcha/recaptcha_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from .core.base import CaptchaParams
from .core.enum import CaptchaTypeEnm

__all__ = ("ReCaptchaV3",)


class ReCaptchaV3(CaptchaParams):
def __init__(
Expand Down
2 changes: 2 additions & 0 deletions src/python3_anticaptcha/turnstile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from .core.base import CaptchaParams
from .core.enum import ProxyTypeEnm, CaptchaTypeEnm

__all__ = ("Turnstile",)


class Turnstile(CaptchaParams):
def __init__(
Expand Down

0 comments on commit bba9458

Please sign in to comment.