Skip to content

Commit 9d3966c

Browse files
committed
feat: add Yandex SmartCaptcha image example
1 parent f0add26 commit 9d3966c

8 files changed

Lines changed: 120 additions & 5 deletions

File tree

‎README.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,8 @@ Use this method to solve the token-based Yandex SmartCaptcha and obtain a token
502502
for the widget on the target page. Use the coordinates method for image challenges.
503503

504504
`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.
505+
challenge. For the image challenge instead, use `CoordinatesTask` as described
506+
in [Coordinates](#coordinates-click-captcha).
507507

508508
| Parameter | Required | Description |
509509
|---|---|---|
@@ -718,7 +718,8 @@ Avoid importing it by name: it shadows Python's built-in `TimeoutError`.
718718
See the dedicated [examples documentation](https://github.com/captcha-solver-api/python-sdk/blob/main/examples/README.md) for the full
719719
sync/async example list, setup steps, expected results, and placeholder guidance.
720720

721-
- **Image/click captchas** (`image_to_text.py`, `coordinates.py`) run after installing the dependencies and setting a valid
721+
- **Image/click captchas** (`image_to_text.py`, `coordinates.py`,
722+
`yandex_smartcaptcha_image.py`) run after installing the dependencies and setting a valid
722723
`CAPTCHA_API_KEY` -- they read sample images bundled in
723724
[examples/assets](https://github.com/captcha-solver-api/python-sdk/tree/main/examples/assets), no target page needed.
724725
`python-dotenv` is optional: install it only if you want the scripts to load a `.env` file.
@@ -743,6 +744,7 @@ export CAPTCHA_API_KEY=your_api_key
743744
python examples/sync/balance.py
744745
python examples/sync/image_to_text.py
745746
python examples/sync/coordinates.py
747+
python examples/sync/yandex_smartcaptcha_image.py
746748
```
747749

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

‎captcha_solver_api/tasks.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -787,13 +787,16 @@ 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 and the
791+
image-based variant of Yandex SmartCaptcha.
791792
792793
Args:
793794
body: The captcha image, base64-encoded (no `data:image/...;base64,` prefix).
794795
comment: Hint for the worker, e.g. `"click on the green apple"`. Recommended
795796
for generic click captchas.
796-
imgInstructions: Optional instruction image, base64-encoded.
797+
imgInstructions: Optional instruction image, base64-encoded. For Yandex
798+
SmartCaptcha image challenges, use the image showing the required
799+
click order.
797800
minClicks: Minimum number of clicks expected (default `1`).
798801
maxClicks: Maximum number of clicks allowed.
799802

‎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

266 KB
Loading
15.1 KB
Loading
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 CoordinatesTask
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+
CoordinatesTask(
41+
body=encode_image("yandex-smartcaptcha-image.png"),
42+
imgInstructions=encode_image("yandex-smartcaptcha-instructions.png"),
43+
minClicks=4,
44+
maxClicks=4,
45+
)
46+
)
47+
# Click the returned points on the captcha image in the listed order.
48+
# Example: {"coordinates": [{"x": 73, "y": 92}, ...]}
49+
print("result: " + str(result))
50+
51+
52+
if __name__ == "__main__":
53+
try:
54+
asyncio.run(main())
55+
except Exception as exc:
56+
sys.exit(exc)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 CoordinatesTask
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+
CoordinatesTask(
41+
body=encode_image("yandex-smartcaptcha-image.png"),
42+
imgInstructions=encode_image("yandex-smartcaptcha-instructions.png"),
43+
minClicks=4,
44+
maxClicks=4,
45+
)
46+
)
47+
# Click the returned points on the captcha image in the listed order.
48+
# Example: {"coordinates": [{"x": 73, "y": 92}, ...]}
49+
print("result: " + str(result))
50+
except Exception as exc:
51+
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

0 commit comments

Comments
 (0)