Skip to content

Commit

Permalink
refactored images processing
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiDrang committed Dec 18, 2024
1 parent 4fd66bd commit 15e5faf
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 39 deletions.
30 changes: 27 additions & 3 deletions src/python3_anticaptcha/core/aio_captcha_instrument.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
15 changes: 12 additions & 3 deletions src/python3_anticaptcha/core/captcha_instrument.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import uuid
import shutil
from pathlib import Path

from .serializer import GetTaskResultResponseSer
Expand All @@ -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):
Expand Down
30 changes: 27 additions & 3 deletions src/python3_anticaptcha/core/sio_captcha_instrument.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
21 changes: 6 additions & 15 deletions src/python3_anticaptcha/image_to_coordinates.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import shutil
from typing import Union, Optional

from .core.base import CaptchaParams
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
21 changes: 6 additions & 15 deletions src/python3_anticaptcha/image_to_text.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import shutil
from typing import Union, Optional

from .core.base import CaptchaParams
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)

0 comments on commit 15e5faf

Please sign in to comment.