Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions python_anticaptcha/tasks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import annotations

import base64
from typing import Any, BinaryIO
from pathlib import Path
from typing import Any, BinaryIO, Union


class BaseTask:
Expand Down Expand Up @@ -139,7 +140,7 @@ class FunCaptchaTask(ProxyMixin, UserAgentMixin, CookieMixin, FunCaptchaProxyles

class ImageToTextTask(BaseTask):
type = "ImageToTextTask"
fp = None
_body = None
phrase = None
case = None
numeric = None
Expand All @@ -151,7 +152,7 @@ class ImageToTextTask(BaseTask):

def __init__(
self,
fp: BinaryIO,
image: Union[str, Path, bytes, BinaryIO],
phrase: bool | None = None,
case: bool | None = None,
numeric: int | None = None,
Expand All @@ -163,7 +164,13 @@ def __init__(
*args: Any,
**kwargs: Any,
) -> None:
self.fp = fp
if isinstance(image, (str, Path)):
with open(image, "rb") as f:
self._body = base64.b64encode(f.read()).decode("utf-8")
elif isinstance(image, bytes):
self._body = base64.b64encode(image).decode("utf-8")
else:
self._body = base64.b64encode(image.read()).decode("utf-8")
self.phrase = phrase
self.case = case
self.numeric = numeric
Expand All @@ -176,7 +183,7 @@ def __init__(

def serialize(self, **result: Any) -> dict[str, Any]:
data = super().serialize(**result)
data["body"] = base64.b64encode(self.fp.read()).decode("utf-8")
data["body"] = self._body
if self.phrase is not None:
data["phrase"] = self.phrase
if self.case is not None:
Expand Down
28 changes: 23 additions & 5 deletions tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,40 @@ def test_type(self):
class TestImageToTextTask:
def test_serialize_base64(self):
fp = io.BytesIO(b"fake image data")
task = ImageToTextTask(fp=fp)
task = ImageToTextTask(fp)
data = task.serialize()
assert data["type"] == "ImageToTextTask"
assert data["body"] == "ZmFrZSBpbWFnZSBkYXRh"

def test_from_bytes(self):
task = ImageToTextTask(b"fake image data")
data = task.serialize()
assert data["type"] == "ImageToTextTask"
assert data["body"] == "ZmFrZSBpbWFnZSBkYXRh"

def test_from_file_path(self, tmp_path):
img = tmp_path / "captcha.jpeg"
img.write_bytes(b"fake image data")
task = ImageToTextTask(str(img))
data = task.serialize()
assert data["body"] == "ZmFrZSBpbWFnZSBkYXRh"

def test_from_pathlib_path(self, tmp_path):
img = tmp_path / "captcha.jpeg"
img.write_bytes(b"fake image data")
task = ImageToTextTask(img)
data = task.serialize()
assert data["body"] == "ZmFrZSBpbWFnZSBkYXRh"

def test_optional_fields_omitted(self):
fp = io.BytesIO(b"data")
task = ImageToTextTask(fp=fp)
task = ImageToTextTask(b"data")
data = task.serialize()
for key in ["phrase", "case", "numeric", "math", "minLength", "maxLength", "comment", "websiteUrl"]:
assert key not in data

def test_optional_fields_included(self):
fp = io.BytesIO(b"data")
task = ImageToTextTask(
fp=fp, phrase=True, case=True, numeric=1,
b"data", phrase=True, case=True, numeric=1,
math=False, min_length=3, max_length=10,
comment="solve this", website_url="https://example.com",
)
Expand Down
Loading