Skip to content

Commit 5ce5560

Browse files
authored
Merge pull request #19 from captcha-solver-api/feat/yandex-smartcaptcha-image
feat: add Yandex SmartCaptcha image example
2 parents f0add26 + 0b6cd57 commit 5ce5560

10 files changed

Lines changed: 198 additions & 11 deletions

File tree

‎.github/workflows/tests.yml‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ jobs:
3838
# to catch packaging errors early. Skipped if lint fails to save CI minutes.
3939
test:
4040
needs: lint
41-
runs-on: ubuntu-latest
41+
# Ubuntu 26.04 (the current ubuntu-latest) does not provide Python 3.9
42+
# through actions/setup-python. Pin the matrix runner while Python 3.9 is
43+
# part of the SDK's supported version range.
44+
runs-on: ubuntu-24.04
4245
strategy:
4346
matrix:
4447
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]

‎README.md‎

Lines changed: 39 additions & 9 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` with
506-
[Coordinates](#coordinates-click-captcha) is available for generic click captchas.
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
|---|---|---|
@@ -718,7 +746,8 @@ Avoid importing it by name: it shadows Python's built-in `TimeoutError`.
718746
See the dedicated [examples documentation](https://github.com/captcha-solver-api/python-sdk/blob/main/examples/README.md) for the full
719747
sync/async example list, setup steps, expected results, and placeholder guidance.
720748

721-
- **Image/click captchas** (`image_to_text.py`, `coordinates.py`) run after installing the dependencies and setting a valid
749+
- **Image/click captchas** (`image_to_text.py`, `coordinates.py`,
750+
`yandex_smartcaptcha_image.py`) run after installing the dependencies and setting a valid
722751
`CAPTCHA_API_KEY` -- they read sample images bundled in
723752
[examples/assets](https://github.com/captcha-solver-api/python-sdk/tree/main/examples/assets), no target page needed.
724753
`python-dotenv` is optional: install it only if you want the scripts to load a `.env` file.
@@ -743,6 +772,7 @@ export CAPTCHA_API_KEY=your_api_key
743772
python examples/sync/balance.py
744773
python examples/sync/image_to_text.py
745774
python examples/sync/coordinates.py
775+
python examples/sync/yandex_smartcaptcha_image.py
746776
```
747777

748778
Live checks with real parameters have exercised all supported task types.

‎captcha_solver_api/tasks.py‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ def __init__(
787787

788788
class CoordinatesTask(BaseTask):
789789
"""Coordinate-based (click) image captcha. No proxy variant -- the image
790-
is submitted directly.
790+
is submitted directly. Use this task for generic click captchas.
791791
792792
Args:
793793
body: The captcha image, base64-encoded (no `data:image/...;base64,` prefix).
@@ -832,6 +832,31 @@ def __init__(
832832
self.maxClicks = maxClicks
833833

834834

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+
835860
class TencentTaskProxyless(BaseTask):
836861
"""Tencent captcha without proxy.
837862

‎examples/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Each supported CAPTCHA type has a matching sync and async script:
6161
| GeeTest v3 | [geetest_v3.py](sync/geetest_v3.py) | [geetest_v3.py](async/geetest_v3.py) | `challenge`, `validate`, `seccode` |
6262
| GeeTest v4 | [geetest_v4.py](sync/geetest_v4.py) | [geetest_v4.py](async/geetest_v4.py) | `captcha_output` and related fields |
6363
| Yandex SmartCaptcha token | [yandex_smartcaptcha.py](sync/yandex_smartcaptcha.py) | [yandex_smartcaptcha.py](async/yandex_smartcaptcha.py) | `token` |
64+
| Yandex SmartCaptcha image | [yandex_smartcaptcha_image.py](sync/yandex_smartcaptcha_image.py) | [yandex_smartcaptcha_image.py](async/yandex_smartcaptcha_image.py) | `coordinates` |
6465
| Coordinates / click captcha | [coordinates.py](sync/coordinates.py) | [coordinates.py](async/coordinates.py) | `coordinates` |
6566
| Tencent | [tencent.py](sync/tencent.py) | [tencent.py](async/tencent.py) | `appid`, `ret`, `ticket`, `randstr` |
6667

11.9 KB
Loading
14.6 KB
Loading
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""Example: Solve an image-based Yandex SmartCaptcha asynchronously.
2+
3+
Prerequisites:
4+
Set the CAPTCHA_API_KEY environment variable.
5+
The sample captcha and instruction images are bundled in examples/assets/.
6+
"""
7+
8+
import asyncio
9+
import os
10+
import sys
11+
from base64 import b64encode
12+
13+
try:
14+
from dotenv import load_dotenv
15+
except ModuleNotFoundError as exc:
16+
if exc.name != "dotenv":
17+
raise
18+
load_dotenv = None
19+
20+
repo_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
21+
sys.path.append(repo_root)
22+
if load_dotenv is not None:
23+
load_dotenv(os.path.join(repo_root, ".env"))
24+
25+
from captcha_solver_api import AsyncCaptchaClient
26+
from captcha_solver_api.tasks import YandexSmartCaptchaImageTask
27+
28+
29+
def encode_image(filename: str) -> str:
30+
"""Return an image from examples/assets as a Base64 string."""
31+
path = os.path.join(repo_root, "examples", "assets", filename)
32+
with open(path, "rb") as image_file:
33+
return b64encode(image_file.read()).decode("utf-8")
34+
35+
36+
async def main() -> None:
37+
api_key = os.getenv("CAPTCHA_API_KEY", "YOUR_API_KEY")
38+
async with AsyncCaptchaClient(api_key) as solver:
39+
result = await solver.solve(
40+
YandexSmartCaptchaImageTask(
41+
image=encode_image("yandex-smartcaptcha-image.png"),
42+
imgInstructions=encode_image("yandex-smartcaptcha-instructions.png"),
43+
)
44+
)
45+
# Click the returned points on the captcha image in the listed order.
46+
# Example: {"coordinates": [{"x": 73, "y": 92}, ...]}
47+
print("result: " + str(result))
48+
49+
50+
if __name__ == "__main__":
51+
try:
52+
asyncio.run(main())
53+
except Exception as exc:
54+
sys.exit(exc)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""Example: Solve an image-based Yandex SmartCaptcha.
2+
3+
Prerequisites:
4+
Set the CAPTCHA_API_KEY environment variable.
5+
The sample captcha and instruction images are bundled in examples/assets/.
6+
"""
7+
8+
import os
9+
import sys
10+
from base64 import b64encode
11+
12+
try:
13+
from dotenv import load_dotenv
14+
except ModuleNotFoundError as exc:
15+
if exc.name != "dotenv":
16+
raise
17+
load_dotenv = None
18+
19+
repo_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
20+
sys.path.append(repo_root)
21+
if load_dotenv is not None:
22+
load_dotenv(os.path.join(repo_root, ".env"))
23+
24+
from captcha_solver_api import CaptchaClient
25+
from captcha_solver_api.tasks import YandexSmartCaptchaImageTask
26+
27+
28+
def encode_image(filename: str) -> str:
29+
"""Return an image from examples/assets as a Base64 string."""
30+
path = os.path.join(repo_root, "examples", "assets", filename)
31+
with open(path, "rb") as image_file:
32+
return b64encode(image_file.read()).decode("utf-8")
33+
34+
35+
api_key = os.getenv("CAPTCHA_API_KEY", "YOUR_API_KEY")
36+
solver = CaptchaClient(api_key)
37+
38+
try:
39+
result = solver.solve(
40+
YandexSmartCaptchaImageTask(
41+
image=encode_image("yandex-smartcaptcha-image.png"),
42+
imgInstructions=encode_image("yandex-smartcaptcha-instructions.png"),
43+
)
44+
)
45+
# Click the returned points on the captcha image in the listed order.
46+
# Example: {"coordinates": [{"x": 73, "y": 92}, ...]}
47+
print("result: " + str(result))
48+
except Exception as exc:
49+
sys.exit(exc)

‎tests/sync/test_coordinates.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ def test_to_dict(self):
2323
def test_with_click_limits(self):
2424
task = CoordinatesTask(
2525
body="base64string",
26+
imgInstructions="base64instructions",
2627
minClicks=1,
2728
maxClicks=5,
2829
)
2930
result = task.to_dict()
3031

32+
assert result["imgInstructions"] == "base64instructions"
3133
assert result["minClicks"] == 1
3234
assert result["maxClicks"] == 5
3335

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)