Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiDrang committed Sep 25, 2023
1 parent d52226c commit 72c5e84
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 36 deletions.
2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pallets_sphinx_themes==2.*
myst-parser==2.0.*
autodoc_pydantic==2.0.*
autodoc_pydantic==1.9.0
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.black]
line-length = 120
target-version = ['py310']
target-version = ['py311']
exclude = '''
/(
\.git
Expand Down
31 changes: 10 additions & 21 deletions src/python_rucaptcha/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 8 additions & 13 deletions src/python_rucaptcha/core/result_handler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import time
import asyncio
from typing import Union

import aiohttp
Expand Down Expand Up @@ -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
"""
Expand All @@ -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

0 comments on commit 72c5e84

Please sign in to comment.