Skip to content

Commit 0b6cd57

Browse files
committed
fix: send Yandex image captcha as SmartCaptchaTask
1 parent bc94544 commit 0b6cd57

7 files changed

Lines changed: 92 additions & 23 deletions

File tree

‎README.md‎

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -499,11 +499,9 @@ With proxy, use `GeeTestTask` (same proxy fields as reCAPTCHA v2).
499499
<sup>[API method description.](https://captcha-solver.com/en/docs/captcha-types#yandex-smartcaptcha)</sup>
500500

501501
Use this method to solve the token-based Yandex SmartCaptcha and obtain a token
502-
for the widget on the target page. Use the coordinates method for image challenges.
502+
for the widget on the target page. Use the dedicated image method for image challenges.
503503

504-
`YandexSmartCaptchaTaskProxyless` / `YandexSmartCaptchaTask` -- token-based
505-
challenge. For the image challenge instead, use `CoordinatesTask` as described
506-
in [Coordinates](#coordinates-click-captcha).
504+
`YandexSmartCaptchaTaskProxyless` / `YandexSmartCaptchaTask` -- token-based challenge.
507505

508506
| Parameter | Required | Description |
509507
|---|---|---|
@@ -532,15 +530,45 @@ print(result["token"])
532530
With proxy, use `YandexSmartCaptchaTask` (same proxy fields as reCAPTCHA v2,
533531
plus the `https` option above).
534532

533+
#### Image challenge
534+
535+
Use `YandexSmartCaptchaImageTask` when SmartCaptcha shows a main image and a
536+
separate strip indicating the symbols to click. The SDK sends API v2 type
537+
`SmartCaptchaTask`; both images must be base64-encoded.
538+
539+
| Parameter | Required | Description |
540+
|---|---|---|
541+
| `image` | yes | Main captcha image, base64-encoded. |
542+
| `imgInstructions` | yes | Click-order instruction image, base64-encoded. |
543+
| `comment` | no | Optional text hint for the worker. |
544+
545+
**Response:** `coordinates` -- ordered `{"x": int, "y": int}` click positions.
546+
547+
```python
548+
import base64
549+
from captcha_solver_api import CaptchaClient
550+
from captcha_solver_api.tasks import YandexSmartCaptchaImageTask
551+
552+
with open("captcha.png", "rb") as f:
553+
image = base64.b64encode(f.read()).decode("utf-8")
554+
with open("instructions.png", "rb") as f:
555+
instructions = base64.b64encode(f.read()).decode("utf-8")
556+
557+
task = YandexSmartCaptchaImageTask(
558+
image=image,
559+
imgInstructions=instructions,
560+
)
561+
result = CaptchaClient("your_api_key").solve(task)
562+
print(result["coordinates"])
563+
```
564+
535565
### Coordinates (click captcha)
536566

537567
<sup>[API method description.](https://captcha-solver.com/en/docs/captcha-types#coordinates)</sup>
538568

539-
Use this method to identify points that a worker should click in an image. It
540-
supports generic click captchas and the image version of Yandex SmartCaptcha.
569+
Use this method to identify points that a worker should click in a generic image.
541570

542-
`CoordinatesTask`. Used both for generic "click on X" captchas and for Yandex
543-
SmartCaptcha's image challenge. No proxy variant -- the image is submitted directly.
571+
`CoordinatesTask`. No proxy variant -- the image is submitted directly.
544572

545573
| Parameter | Required | Description |
546574
|---|---|---|

‎captcha_solver_api/tasks.py‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -787,16 +787,13 @@ def __init__(
787787

788788
class CoordinatesTask(BaseTask):
789789
"""Coordinate-based (click) image captcha. No proxy variant -- the image
790-
is submitted directly. Use this task for generic click captchas and the
791-
image-based variant of Yandex SmartCaptcha.
790+
is submitted directly. Use this task for generic click captchas.
792791
793792
Args:
794793
body: The captcha image, base64-encoded (no `data:image/...;base64,` prefix).
795794
comment: Hint for the worker, e.g. `"click on the green apple"`. Recommended
796795
for generic click captchas.
797-
imgInstructions: Optional instruction image, base64-encoded. For Yandex
798-
SmartCaptcha image challenges, use the image showing the required
799-
click order.
796+
imgInstructions: Optional instruction image, base64-encoded.
800797
minClicks: Minimum number of clicks expected (default `1`).
801798
maxClicks: Maximum number of clicks allowed.
802799
@@ -835,6 +832,31 @@ def __init__(
835832
self.maxClicks = maxClicks
836833

837834

835+
class YandexSmartCaptchaImageTask(BaseTask):
836+
"""Image-based Yandex SmartCaptcha using API v2 ``SmartCaptchaTask``.
837+
838+
Args:
839+
image: Main SmartCaptcha challenge image, base64-encoded.
840+
imgInstructions: Click-order instruction image, base64-encoded.
841+
comment: Optional text hint for the worker.
842+
843+
Returns (``solution`` from ``solve()``):
844+
``coordinates`` -- an ordered list of ``{"x": int, "y": int}`` points.
845+
"""
846+
847+
type = "SmartCaptchaTask"
848+
849+
def __init__(
850+
self,
851+
image: str,
852+
imgInstructions: str,
853+
comment: Optional[str] = None,
854+
) -> None:
855+
self.image = image
856+
self.imgInstructions = imgInstructions
857+
self.comment = comment
858+
859+
838860
class TencentTaskProxyless(BaseTask):
839861
"""Tencent captcha without proxy.
840862
-255 KB
Loading
-566 Bytes
Loading

‎examples/async/yandex_smartcaptcha_image.py‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
load_dotenv(os.path.join(repo_root, ".env"))
2424

2525
from captcha_solver_api import AsyncCaptchaClient
26-
from captcha_solver_api.tasks import CoordinatesTask
26+
from captcha_solver_api.tasks import YandexSmartCaptchaImageTask
2727

2828

2929
def encode_image(filename: str) -> str:
@@ -37,11 +37,9 @@ async def main() -> None:
3737
api_key = os.getenv("CAPTCHA_API_KEY", "YOUR_API_KEY")
3838
async with AsyncCaptchaClient(api_key) as solver:
3939
result = await solver.solve(
40-
CoordinatesTask(
41-
body=encode_image("yandex-smartcaptcha-image.png"),
40+
YandexSmartCaptchaImageTask(
41+
image=encode_image("yandex-smartcaptcha-image.png"),
4242
imgInstructions=encode_image("yandex-smartcaptcha-instructions.png"),
43-
minClicks=4,
44-
maxClicks=4,
4543
)
4644
)
4745
# Click the returned points on the captcha image in the listed order.

‎examples/sync/yandex_smartcaptcha_image.py‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
load_dotenv(os.path.join(repo_root, ".env"))
2323

2424
from captcha_solver_api import CaptchaClient
25-
from captcha_solver_api.tasks import CoordinatesTask
25+
from captcha_solver_api.tasks import YandexSmartCaptchaImageTask
2626

2727

2828
def encode_image(filename: str) -> str:
@@ -37,11 +37,9 @@ def encode_image(filename: str) -> str:
3737

3838
try:
3939
result = solver.solve(
40-
CoordinatesTask(
41-
body=encode_image("yandex-smartcaptcha-image.png"),
40+
YandexSmartCaptchaImageTask(
41+
image=encode_image("yandex-smartcaptcha-image.png"),
4242
imgInstructions=encode_image("yandex-smartcaptcha-instructions.png"),
43-
minClicks=4,
44-
maxClicks=4,
4543
)
4644
)
4745
# Click the returned points on the captcha image in the listed order.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""Tests for YandexSmartCaptchaImageTask."""
2+
3+
from captcha_solver_api.tasks import YandexSmartCaptchaImageTask
4+
5+
6+
class TestYandexSmartCaptchaImage:
7+
def test_to_dict_uses_api_v2_task_type(self):
8+
task = YandexSmartCaptchaImageTask(
9+
image="captcha-image",
10+
imgInstructions="instruction-image",
11+
comment="click the symbols in the specified order",
12+
)
13+
14+
assert task.to_dict() == {
15+
"type": "SmartCaptchaTask",
16+
"image": "captcha-image",
17+
"imgInstructions": "instruction-image",
18+
"comment": "click the symbols in the specified order",
19+
}
20+
21+
def test_optional_comment_is_omitted(self):
22+
task = YandexSmartCaptchaImageTask("captcha-image", "instruction-image")
23+
assert "comment" not in task.to_dict()

0 commit comments

Comments
 (0)