Skip to content

Commit ad521a5

Browse files
committed
feat(examples): add large multi-language example dataset
- add hundreds of examples - cover 10 programming languages
1 parent 1a60962 commit ad521a5

6,164 files changed

Lines changed: 677149 additions & 6 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 315 additions & 6 deletions
Large diffs are not rendered by default.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# CaptchaAI API credentials
2+
# Get your API key from https://captchaai.com/dashboard
3+
CAPTCHAAI_API_KEY=YOUR_API_KEY
4+
5+
# Target page configuration
6+
# googlekey
7+
CAPTCHA_GOOGLEKEY=CAPTCHA_SITEKEY
8+
# Full URL of the page where the CAPTCHA loads
9+
CAPTCHA_PAGEURL=CAPTCHA_PAGEURL
10+
11+
# Polling configuration (optional)
12+
POLL_INTERVAL=5
13+
MAX_TIMEOUT=120
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.env
2+
__pycache__/
3+
node_modules/
4+
vendor/
5+
target/
6+
*.class
7+
bin/
8+
obj/
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Abstract CAPTCHA Solver Interface: Provider-Agnostic Design
2+
3+
Full working example for solving reCAPTCHA v2 using the CaptchaAI API.
4+
5+
**Languages:** Python, Node.js, PHP, Go, Java, C#, Ruby, Rust, Kotlin, Bash
6+
7+
## Related article
8+
9+
This example accompanies the blog article:
10+
**[Abstract CAPTCHA Solver Interface: Provider-Agnostic Design](https://blog.captchaai.com/abstract-captcha-solver-provider-agnostic)**
11+
12+
## Prerequisites
13+
14+
- A CaptchaAI account with API key ([get one here](https://captchaai.com))
15+
- A target page with reCAPTCHA v2 for testing
16+
17+
## Quick start
18+
19+
1. Clone the repository:
20+
```bash
21+
git clone https://github.com/CaptchaAI/CaptchaAI-Examples.git
22+
cd CaptchaAI-Examples/articles/abstract-captcha-solver-provider-agnostic
23+
```
24+
25+
2. Copy and edit the environment file:
26+
```bash
27+
cp .env.example .env
28+
```
29+
30+
3. Run any language example:
31+
32+
### Python
33+
```bash
34+
cd python && pip install -r requirements.txt && python solve.py
35+
```
36+
37+
### Node.js
38+
```bash
39+
cd node && npm install && node solve.js
40+
```
41+
42+
### PHP
43+
```bash
44+
cd php && composer install && php solve.php
45+
```
46+
47+
### Go
48+
```bash
49+
cd go && go run solve.go
50+
```
51+
52+
### Java
53+
```bash
54+
cd java && javac Solve.java && java Solve
55+
```
56+
57+
### C#
58+
```bash
59+
cd csharp && dotnet run
60+
```
61+
62+
### Ruby
63+
```bash
64+
cd ruby && ruby solve.rb
65+
```
66+
67+
### Rust
68+
```bash
69+
cd rust && cargo run
70+
```
71+
72+
### Kotlin
73+
```bash
74+
cd kotlin && kotlinc solve.kt -include-runtime -d solve.jar && java -jar solve.jar
75+
```
76+
77+
### Bash
78+
```bash
79+
cd bash && chmod +x solve.sh && ./solve.sh
80+
```
81+
82+
## How it works
83+
84+
1. **Submit** — Sends the CAPTCHA parameters to `https://ocr.captchaai.com/in.php` using method `userrecaptcha`
85+
2. **Poll** — Checks `https://ocr.captchaai.com/res.php` every 5 seconds for the result
86+
3. **Result** — Returns the solved token/answer ready for use
87+
88+
## API documentation
89+
90+
- [CaptchaAI API Docs](https://captchaai.com/api-docs)
91+
92+
## Related examples
93+
94+
Browse more examples in the [CaptchaAI-Examples repository](https://github.com/CaptchaAI/CaptchaAI-Examples).
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#!/usr/bin/env bash
2+
# Solve reCAPTCHA v2 using the CaptchaAI API.
3+
#
4+
# Usage:
5+
# chmod +x solve.sh
6+
# ./solve.sh
7+
#
8+
# Requires .env file in parent directory with:
9+
# CAPTCHAAI_API_KEY and CAPTCHA-specific variables
10+
#
11+
# Dependencies: curl, jq
12+
13+
set -euo pipefail
14+
15+
SUBMIT_URL="https://ocr.captchaai.com/in.php"
16+
RESULT_URL="https://ocr.captchaai.com/res.php"
17+
18+
# Load .env file
19+
ENV_FILE="$(dirname "$0")/../.env"
20+
if [[ -f "$ENV_FILE" ]]; then
21+
while IFS='=' read -r key value; do
22+
key=$(echo "$key" | xargs)
23+
[[ -z "$key" || "$key" == \#* ]] && continue
24+
value=$(echo "$value" | xargs)
25+
export "$key=$value"
26+
done < "$ENV_FILE"
27+
fi
28+
29+
CAPTCHAAI_API_KEY="${CAPTCHAAI_API_KEY:-}"
30+
CAPTCHA_GOOGLEKEY="${CAPTCHA_GOOGLEKEY:-}"
31+
CAPTCHA_PAGEURL="${CAPTCHA_PAGEURL:-}"
32+
POLL_INTERVAL="${POLL_INTERVAL:-5}"
33+
MAX_TIMEOUT="${MAX_TIMEOUT:-120}"
34+
35+
AUTH_ERRORS="ERROR_WRONG_USER_KEY ERROR_KEY_DOES_NOT_EXIST IP_BANNED"
36+
BALANCE_ERRORS="ERROR_ZERO_BALANCE"
37+
INPUT_ERRORS="ERROR_PAGEURL ERROR_WRONG_GOOGLEKEY ERROR_BAD_PARAMETERS ERROR_BAD_TOKEN_OR_PAGEURL"
38+
TRANSIENT_ERRORS="ERROR_SERVER_ERROR ERROR_INTERNAL_SERVER_ERROR"
39+
SOLVE_ERRORS="ERROR_CAPTCHA_UNSOLVABLE"
40+
PROXY_ERRORS="ERROR_BAD_PROXY ERROR_PROXY_CONNECTION_FAILED"
41+
42+
contains() { [[ " $1 " == *" $2 "* ]]; }
43+
44+
classify_error() {
45+
local error="$1"
46+
if contains "$AUTH_ERRORS" "$error"; then
47+
echo "[!] Authentication error: $error"
48+
echo " Check your API key at https://captchaai.com/dashboard"
49+
elif contains "$BALANCE_ERRORS" "$error"; then
50+
echo "[!] Balance error: $error"
51+
echo " Top up your account at https://captchaai.com"
52+
elif contains "$INPUT_ERRORS" "$error"; then
53+
echo "[!] Input error: $error"
54+
echo " Verify your sitekey and page URL are correct."
55+
elif contains "$PROXY_ERRORS" "$error"; then
56+
echo "[!] Proxy error: $error"
57+
echo " Check your proxy configuration or try a different proxy."
58+
else
59+
echo "[!] Submission failed: $error"
60+
fi
61+
}
62+
63+
# Validate
64+
if [[ -z "$CAPTCHAAI_API_KEY" || "$CAPTCHAAI_API_KEY" == "YOUR_API_KEY" ]]; then
65+
echo "[!] ERROR: CAPTCHAAI_API_KEY is not set."
66+
echo " Copy .env.example to .env and add your real API key."
67+
exit 1
68+
fi
69+
70+
# Check dependencies
71+
if ! command -v jq &> /dev/null; then
72+
echo "[!] ERROR: jq is required but not installed."
73+
echo " Install it: apt-get install jq / brew install jq / choco install jq"
74+
exit 1
75+
fi
76+
77+
# Submit
78+
echo "[*] Submitting reCAPTCHA v2 task..."
79+
SUBMIT_RESPONSE=$(curl -s --max-time 30 \
80+
"${SUBMIT_URL}?key=${CAPTCHAAI_API_KEY}&method=userrecaptcha&googlekey=${CAPTCHA_GOOGLEKEY}&pageurl=${CAPTCHA_PAGEURL}&json=1")
81+
82+
if [[ -z "$SUBMIT_RESPONSE" ]]; then
83+
echo "[!] Network error during submission."
84+
exit 1
85+
fi
86+
87+
SUBMIT_STATUS=$(echo "$SUBMIT_RESPONSE" | jq -r '.status // 0')
88+
SUBMIT_REQUEST=$(echo "$SUBMIT_RESPONSE" | jq -r '.request // "UNKNOWN_ERROR"')
89+
90+
if [[ "$SUBMIT_STATUS" != "1" ]]; then
91+
classify_error "$SUBMIT_REQUEST"
92+
exit 1
93+
fi
94+
95+
TASK_ID="$SUBMIT_REQUEST"
96+
echo "[+] Task submitted. ID: $TASK_ID"
97+
98+
# Poll
99+
echo "[*] Waiting 15s before first poll..."
100+
sleep 15
101+
102+
ELAPSED=15
103+
ATTEMPT=0
104+
BACKOFF=$POLL_INTERVAL
105+
106+
while [[ $ELAPSED -lt $MAX_TIMEOUT ]]; do
107+
ATTEMPT=$((ATTEMPT + 1))
108+
echo "[*] Polling for result (attempt $ATTEMPT)..."
109+
110+
POLL_RESPONSE=$(curl -s --max-time 30 \
111+
"${RESULT_URL}?key=${CAPTCHAAI_API_KEY}&action=get&id=${TASK_ID}&json=1" 2>/dev/null || echo "")
112+
113+
if [[ -z "$POLL_RESPONSE" ]]; then
114+
echo "[!] Network error during polling."
115+
sleep "$BACKOFF"
116+
ELAPSED=$((ELAPSED + BACKOFF))
117+
BACKOFF=$((BACKOFF * 2))
118+
[[ $BACKOFF -gt 30 ]] && BACKOFF=30
119+
continue
120+
fi
121+
122+
POLL_STATUS=$(echo "$POLL_RESPONSE" | jq -r '.status // 0')
123+
POLL_REQUEST=$(echo "$POLL_RESPONSE" | jq -r '.request // ""')
124+
125+
if [[ "$POLL_STATUS" == "1" ]]; then
126+
TOKEN="$POLL_REQUEST"
127+
TRUNCATED="${TOKEN:0:50}"
128+
echo "[+] Solved! Token: ${TRUNCATED}..."
129+
echo "[+] Full token length: ${#TOKEN} characters"
130+
exit 0
131+
fi
132+
133+
if [[ "$POLL_REQUEST" == "CAPCHA_NOT_READY" ]]; then
134+
echo "[*] Not ready yet, waiting ${POLL_INTERVAL}s..."
135+
sleep "$POLL_INTERVAL"
136+
ELAPSED=$((ELAPSED + POLL_INTERVAL))
137+
BACKOFF=$POLL_INTERVAL
138+
continue
139+
fi
140+
if contains "$TRANSIENT_ERRORS" "$POLL_REQUEST"; then
141+
echo "[!] Transient error: $POLL_REQUEST, retrying in ${BACKOFF}s..."
142+
sleep "$BACKOFF"
143+
ELAPSED=$((ELAPSED + BACKOFF))
144+
BACKOFF=$((BACKOFF * 2))
145+
[[ $BACKOFF -gt 30 ]] && BACKOFF=30
146+
continue
147+
fi
148+
if contains "$SOLVE_ERRORS" "$POLL_REQUEST"; then
149+
echo "[!] Solve error: $POLL_REQUEST"
150+
echo " The CAPTCHA could not be solved. Verify parameters and retry."
151+
exit 1
152+
fi
153+
if contains "$PROXY_ERRORS" "$POLL_REQUEST"; then
154+
echo "[!] Proxy error: $POLL_REQUEST"
155+
echo " Check your proxy configuration or try a different proxy."
156+
exit 1
157+
fi
158+
159+
echo "[!] Unexpected error: $POLL_REQUEST"
160+
exit 1
161+
done
162+
163+
echo "[!] Timeout: no solution received within ${MAX_TIMEOUT} seconds."
164+
exit 1

0 commit comments

Comments
 (0)