diff --git a/src/python3_anticaptcha/core/aio_captcha_instrument.py b/src/python3_anticaptcha/core/aio_captcha_instrument.py index 31b52e7..f841699 100644 --- a/src/python3_anticaptcha/core/aio_captcha_instrument.py +++ b/src/python3_anticaptcha/core/aio_captcha_instrument.py @@ -1,7 +1,7 @@ import base64 import asyncio import logging -from typing import Optional +from typing import Union, Optional from urllib import parse from urllib.parse import urljoin @@ -41,9 +41,31 @@ async def processing_captcha(self) -> dict: return await self._get_result() - async def body_file_processing( + async def processing_image_captcha( + self, + save_format: Union[str, SaveFormatsEnm], + img_clearing: bool, + captcha_link: str, + captcha_file: str, + captcha_base64: bytes, + img_path: str, + ) -> dict: + await self.__body_file_processing( + save_format=save_format, + img_clearing=img_clearing, + file_path=img_path, + captcha_link=captcha_link, + captcha_file=captcha_file, + captcha_base64=captcha_base64, + ) + if not self.result.errorId: + return await self.processing_captcha() + return self.result.to_dict() + + async def __body_file_processing( self, save_format: SaveFormatsEnm, + img_clearing: bool, file_path: str, file_extension: str = "png", captcha_link: Optional[str] = None, @@ -67,7 +89,9 @@ async def body_file_processing( content = await self._url_read(url=captcha_link, **kwargs) # according to the value of the passed parameter, select the function to save the image if save_format == SaveFormatsEnm.CONST.value: - self._file_const_saver(content, file_path, file_extension=file_extension) + full_file_path = self._file_const_saver(content, file_path, file_extension=file_extension) + if img_clearing: + self._file_clean(full_file_path=full_file_path) self.captcha_params.create_task_payload.task.update({"body": base64.b64encode(content).decode("utf-8")}) except Exception as error: self.result.errorId = 12 diff --git a/src/python3_anticaptcha/core/captcha_instrument.py b/src/python3_anticaptcha/core/captcha_instrument.py index 7a0c27d..9fd3314 100644 --- a/src/python3_anticaptcha/core/captcha_instrument.py +++ b/src/python3_anticaptcha/core/captcha_instrument.py @@ -1,5 +1,6 @@ import os import uuid +import shutil from pathlib import Path from .serializer import GetTaskResultResponseSer @@ -16,18 +17,26 @@ def _local_file_captcha(captcha_file: str): with open(captcha_file, "rb") as file: return file.read() - def _file_const_saver(self, content: bytes, file_path: str, file_extension: str = "png"): + @staticmethod + def _file_const_saver(content: bytes, file_path: str, file_extension: str = "png") -> str: """ Method create and save file in folder """ Path(file_path).mkdir(parents=True, exist_ok=True) # generate image name - self.file_name = f"file-{uuid.uuid4()}.{file_extension}" + file_name = f"file-{uuid.uuid4()}.{file_extension}" + + full_file_path = os.path.join(file_path, file_name) # save image to folder - with open(os.path.join(file_path, self.file_name), "wb") as out_image: + with open(full_file_path, "wb") as out_image: out_image.write(content) + return full_file_path + + @staticmethod + def _file_clean(full_file_path: str): + shutil.rmtree(full_file_path, ignore_errors=True) class CaptchaInstrument(FileInstrument): diff --git a/src/python3_anticaptcha/core/sio_captcha_instrument.py b/src/python3_anticaptcha/core/sio_captcha_instrument.py index 442d637..ca79f1b 100644 --- a/src/python3_anticaptcha/core/sio_captcha_instrument.py +++ b/src/python3_anticaptcha/core/sio_captcha_instrument.py @@ -1,7 +1,7 @@ import time import base64 import logging -from typing import Optional +from typing import Union, Optional from urllib import parse from urllib.parse import urljoin @@ -47,9 +47,31 @@ def processing_captcha(self) -> dict: return self._get_result() - def body_file_processing( + def processing_image_captcha( + self, + save_format: Union[str, SaveFormatsEnm], + img_clearing: bool, + captcha_link: str, + captcha_file: str, + captcha_base64: bytes, + img_path: str, + ) -> dict: + self.__body_file_processing( + save_format=save_format, + img_clearing=img_clearing, + file_path=img_path, + captcha_link=captcha_link, + captcha_file=captcha_file, + captcha_base64=captcha_base64, + ) + if not self.result.errorId: + return self.processing_captcha() + return self.result.to_dict() + + def __body_file_processing( self, save_format: SaveFormatsEnm, + img_clearing: bool, file_path: str, file_extension: str = "png", captcha_link: Optional[str] = None, @@ -73,7 +95,9 @@ def body_file_processing( content = self._url_read(url=captcha_link, **kwargs).content # according to the value of the passed parameter, select the function to save the image if save_format == SaveFormatsEnm.CONST.value: - self._file_const_saver(content, file_path, file_extension=file_extension) + full_file_path = self._file_const_saver(content, file_path, file_extension=file_extension) + if img_clearing: + self._file_clean(full_file_path=full_file_path) self.captcha_params.create_task_payload.task.update({"body": base64.b64encode(content).decode("utf-8")}) except Exception as error: self.result.errorId = 12 diff --git a/src/python3_anticaptcha/image_to_coordinates.py b/src/python3_anticaptcha/image_to_coordinates.py index a8d38cf..473a325 100644 --- a/src/python3_anticaptcha/image_to_coordinates.py +++ b/src/python3_anticaptcha/image_to_coordinates.py @@ -1,4 +1,3 @@ -import shutil from typing import Union, Optional from .core.base import CaptchaParams @@ -159,16 +158,14 @@ def captcha_handler( self.task_params.update({**additional_params}) self._captcha_handling_instrument = SIOCaptchaInstrument(captcha_params=self) - self._captcha_handling_instrument.body_file_processing( + return self._captcha_handling_instrument.processing_image_captcha( save_format=self.save_format, - file_path=self.img_path, + img_clearing=self.img_clearing, + img_path=self.img_path, captcha_link=captcha_link, captcha_file=captcha_file, captcha_base64=captcha_base64, ) - if not self._captcha_handling_instrument.result.errorId: - return self._captcha_handling_instrument.processing_captcha() - return self._captcha_handling_instrument.result.to_dict() async def aio_captcha_handler( self, @@ -197,17 +194,11 @@ async def aio_captcha_handler( self.task_params.update({**additional_params}) self._captcha_handling_instrument = AIOCaptchaInstrument(captcha_params=self) - await self._captcha_handling_instrument.body_file_processing( + return await self._captcha_handling_instrument.processing_image_captcha( save_format=self.save_format, - file_path=self.img_path, + img_clearing=self.img_clearing, + img_path=self.img_path, captcha_link=captcha_link, captcha_file=captcha_file, captcha_base64=captcha_base64, ) - if not self._captcha_handling_instrument.result.errorId: - return await self._captcha_handling_instrument.processing_captcha() - return self._captcha_handling_instrument.result.to_dict() - - def __del__(self): - if self.save_format == SaveFormatsEnm.CONST.value and self.img_clearing: - shutil.rmtree(self.img_path, ignore_errors=True) diff --git a/src/python3_anticaptcha/image_to_text.py b/src/python3_anticaptcha/image_to_text.py index 8e3d8c4..80e5f49 100644 --- a/src/python3_anticaptcha/image_to_text.py +++ b/src/python3_anticaptcha/image_to_text.py @@ -1,4 +1,3 @@ -import shutil from typing import Union, Optional from .core.base import CaptchaParams @@ -132,16 +131,14 @@ def captcha_handler( self.task_params.update({**additional_params}) self._captcha_handling_instrument = SIOCaptchaInstrument(captcha_params=self) - self._captcha_handling_instrument.body_file_processing( + return self._captcha_handling_instrument.processing_image_captcha( save_format=self.save_format, - file_path=self.img_path, + img_clearing=self.img_clearing, + img_path=self.img_path, captcha_link=captcha_link, captcha_file=captcha_file, captcha_base64=captcha_base64, ) - if not self._captcha_handling_instrument.result.errorId: - return self._captcha_handling_instrument.processing_captcha() - return self._captcha_handling_instrument.result.to_dict() async def aio_captcha_handler( self, @@ -170,17 +167,11 @@ async def aio_captcha_handler( self.task_params.update({**additional_params}) self._captcha_handling_instrument = AIOCaptchaInstrument(captcha_params=self) - await self._captcha_handling_instrument.body_file_processing( + return await self._captcha_handling_instrument.processing_image_captcha( save_format=self.save_format, - file_path=self.img_path, + img_clearing=self.img_clearing, + img_path=self.img_path, captcha_link=captcha_link, captcha_file=captcha_file, captcha_base64=captcha_base64, ) - if not self._captcha_handling_instrument.result.errorId: - return await self._captcha_handling_instrument.processing_captcha() - return self._captcha_handling_instrument.result.to_dict() - - def __del__(self): - if self.save_format == SaveFormatsEnm.CONST.value and self.img_clearing: - shutil.rmtree(self.img_path, ignore_errors=True)