Skip to content

Commit 26efc15

Browse files
Adam Dobrawyclaude
andcommitted
Add documentation for context manager, on_check callback, and ImageToTextTask inputs
Document three recently added features that were missing usage examples: - Context manager support on AnticaptchaClient - on_check callback parameter on Job.join() - ImageToTextTask accepting file paths and raw bytes Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 76d98bd commit 26efc15

2 files changed

Lines changed: 61 additions & 4 deletions

File tree

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ client = AnticaptchaClient("my-api-key")
3434
client = AnticaptchaClient()
3535
```
3636

37+
The client can be used as a context manager to ensure the underlying session is closed:
38+
39+
```python
40+
with AnticaptchaClient(api_key) as client:
41+
job = client.create_task(task)
42+
job.join()
43+
```
44+
3745
### Solve recaptcha
3846

3947
Example snippet for Recaptcha:
@@ -81,9 +89,17 @@ Example snippet for text captcha:
8189
from python_anticaptcha import AnticaptchaClient, ImageToTextTask
8290

8391
api_key = '174faff8fbc769e94a5862391ecfd010'
84-
captcha_fp = open('examples/captcha_ms.jpeg', 'rb')
8592
client = AnticaptchaClient(api_key)
86-
task = ImageToTextTask(captcha_fp)
93+
94+
# From a file path:
95+
task = ImageToTextTask('examples/captcha_ms.jpeg')
96+
97+
# Or from raw bytes:
98+
# task = ImageToTextTask(open('examples/captcha_ms.jpeg', 'rb').read())
99+
100+
# Or from a file object:
101+
# task = ImageToTextTask(open('examples/captcha_ms.jpeg', 'rb'))
102+
87103
job = client.create_task(task)
88104
job.join()
89105
print(job.get_captcha_text())
@@ -110,6 +126,18 @@ job.join()
110126
print(job.get_token_response())
111127
```
112128

129+
### Monitor solve progress
130+
131+
You can pass an `on_check` callback to `job.join()` to monitor progress:
132+
133+
```python
134+
def progress(elapsed, status):
135+
print(f"Elapsed: {elapsed}s, status: {status}")
136+
137+
job = client.create_task(task)
138+
job.join(on_check=progress)
139+
```
140+
113141
### Report incorrect image
114142

115143
Example snippet for reporting an incorrect image task:

docs/usage.rst

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ You can pass the key explicitly or set the ``ANTICAPTCHA_API_KEY`` environment v
1313
# Or set ANTICAPTCHA_API_KEY environment variable
1414
client = AnticaptchaClient()
1515
16+
The client can be used as a context manager to ensure the underlying session is closed:
17+
18+
.. code:: python
19+
20+
with AnticaptchaClient(api_key) as client:
21+
job = client.create_task(task)
22+
job.join()
23+
1624
Solve recaptcha
1725
###############
1826

@@ -64,9 +72,17 @@ Example snippet for text captcha:
6472
from python_anticaptcha import AnticaptchaClient, ImageToTextTask
6573
6674
api_key = '174faff8fbc769e94a5862391ecfd010'
67-
captcha_fp = open('examples/captcha_ms.jpeg', 'rb')
6875
client = AnticaptchaClient(api_key)
69-
task = ImageToTextTask(captcha_fp)
76+
77+
# From a file path:
78+
task = ImageToTextTask('examples/captcha_ms.jpeg')
79+
80+
# Or from raw bytes:
81+
# task = ImageToTextTask(open('examples/captcha_ms.jpeg', 'rb').read())
82+
83+
# Or from a file object:
84+
# task = ImageToTextTask(open('examples/captcha_ms.jpeg', 'rb'))
85+
7086
job = client.create_task(task)
7187
job.join()
7288
print(job.get_captcha_text())
@@ -93,6 +109,19 @@ Example snippet for funcaptcha:
93109
job.join()
94110
print(job.get_token_response())
95111
112+
Monitor solve progress
113+
######################
114+
115+
You can pass an ``on_check`` callback to ``job.join()`` to monitor progress:
116+
117+
.. code:: python
118+
119+
def progress(elapsed, status):
120+
print(f"Elapsed: {elapsed}s, status: {status}")
121+
122+
job = client.create_task(task)
123+
job.join(on_check=progress)
124+
96125
Report incorrect image
97126
######################
98127

0 commit comments

Comments
 (0)