From 72c5e84a48592a399d6f57c3cda6e3e6dd7e6174 Mon Sep 17 00:00:00 2001 From: Andrei Date: Mon, 25 Sep 2023 08:12:59 +0300 Subject: [PATCH] upd --- docs/requirements.txt | 2 +- pyproject.toml | 2 +- src/python_rucaptcha/core/base.py | 31 +++++++-------------- src/python_rucaptcha/core/result_handler.py | 21 ++++++-------- 4 files changed, 20 insertions(+), 36 deletions(-) diff --git a/docs/requirements.txt b/docs/requirements.txt index 29268972..1da77a43 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,3 +1,3 @@ pallets_sphinx_themes==2.* myst-parser==2.0.* -autodoc_pydantic==2.0.* +autodoc_pydantic==1.9.0 diff --git a/pyproject.toml b/pyproject.toml index 9ac9eea7..41eab815 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.black] line-length = 120 -target-version = ['py310'] +target-version = ['py311'] exclude = ''' /( \.git diff --git a/src/python_rucaptcha/core/base.py b/src/python_rucaptcha/core/base.py index 036385bf..c2175544 100644 --- a/src/python_rucaptcha/core/base.py +++ b/src/python_rucaptcha/core/base.py @@ -94,49 +94,38 @@ async def aio_url_read(self, url: str, **kwargs) -> bytes: async with session.get(url=url, **kwargs) as resp: return await resp.content.read() - async def _aio_processing_response(self) -> dict: + async def _aio_processing_response(self) -> Union[dict, Exception]: """ Method processing async captcha solving task creation result """ try: # make async or sync request - response = await self.__aio_make_post_request() + response = await self.__aio_create_task() # check response status - if response.status == 1: - self.result.taskId = response.request + if response.errorId == 0: + self.get_task_payload.taskId = response.taskId else: - self.result.error = True - self.result.errorBody = response.request + return response.dict() except Exception as error: - self.result.error = True - self.result.errorBody = str(error) - - # check for errors while make request to server - if self.result.error: - return self.result.dict() - - # if all is ok - send captcha to service and wait solution - # update payload - add captcha taskId - self.get_payload.taskId = self.result.taskId + return error # wait captcha solving await asyncio.sleep(self.params.sleep_time) return await get_async_result( - get_payload=self.get_payload, + get_payload=self.get_task_payload, sleep_time=self.params.sleep_time, url_response=self.params.url_response, - result=self.result, ) - async def __aio_make_post_request(self): + async def __aio_create_task(self) -> CreateTaskResponseSer: async with aiohttp.ClientSession() as session: async for attempt in ASYNC_RETRIES: with attempt: async with session.post( - self.params.url_request, data=self.post_payload, raise_for_status=True + self.params.url_request, data=self.create_task_payload, raise_for_status=True ) as resp: response_json = await resp.json(content_type=None) - return GetTaskResultSer(**response_json) + return CreateTaskResponseSer(**response_json) # Working with images methods diff --git a/src/python_rucaptcha/core/result_handler.py b/src/python_rucaptcha/core/result_handler.py index a5a67593..071ce9d6 100644 --- a/src/python_rucaptcha/core/result_handler.py +++ b/src/python_rucaptcha/core/result_handler.py @@ -1,5 +1,4 @@ import time -import asyncio from typing import Union import aiohttp @@ -29,7 +28,9 @@ def get_sync_result(get_payload: GetTaskResultRequestSer, sleep_time: int, url_r return error -async def get_async_result(get_payload: GetTaskResultRequestSer, sleep_time: int, url_response: str) -> dict: +async def get_async_result( + get_payload: GetTaskResultRequestSer, sleep_time: int, url_response: str +) -> Union[dict, Exception]: """ Function periodically send the ASYNC request to service and wait for captcha solving result """ @@ -41,18 +42,12 @@ async def get_async_result(get_payload: GetTaskResultRequestSer, sleep_time: int # send a request for the result of solving the captcha async with session.get(url_response, params=get_payload, raise_for_status=True) as resp: captcha_response = await resp.json(content_type=None) - captcha_response = ServiceGetResponseSer(**captcha_response) + captcha_response = GetTaskResultResponseSer(**captcha_response) # if the captcha has not been resolved yet, wait - if captcha_response.request == "CAPCHA_NOT_READY": - await asyncio.sleep(sleep_time) - result.error = True - result.errorBody = "ERROR_CAPTCHA_UNSOLVABLE" - + if captcha_response.status == "processing": + time.sleep(sleep_time) else: - return result_processing(captcha_response, result) - + return captcha_response.dict() except Exception as error: - result.error = True - result.errorBody = str(error) - return result.dict() + return error