Skip to content

Commit 60ee87c

Browse files
authored
Add Drag & Drop captcha support with sync/async solvers, tests, examples, and documentation (#148)
1 parent 259a025 commit 60ee87c

15 files changed

Lines changed: 641 additions & 1 deletion

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Examples of API requests for different captcha types are available on the [Pytho
5555
- [Alibaba](#alibaba)
5656
- [TSPD](#tspd)
5757
- [Basilisk](#basilisk)
58+
- [Drag & Drop](#drag--drop)
5859
- [Other methods](#other-methods)
5960
- [send / get\_result](#send--get_result)
6061
- [balance](#balance)
@@ -611,6 +612,26 @@ result = solver.basilisk(pageurl='https://example.com/login',
611612
)
612613
```
613614

615+
### Drag & Drop
616+
617+
<sup>[API method description.](https://2captcha.com/2captcha-api#drag-and-drop-captcha)</sup>
618+
619+
Use this method to solve a captcha where one or more images need to be dragged onto specific positions on a background image.
620+
`body` and each item in `images` can be a file path, a URL, or a Base64-encoded string.
621+
622+
Returns the standard result dictionary `{'captchaId': 'TASK_ID', 'code': 'COORDINATES'}`. The coordinates string in
623+
`result['code']` contains one entry per image from `images`, in the same order, separated by `|`. An image that
624+
wasn't moved may be returned by the API as `null`.
625+
626+
```python
627+
result = solver.drag_and_drop(
628+
body='path/to/background.jpg',
629+
images=['path/to/image1.jpg', 'path/to/image2.jpg'],
630+
)
631+
632+
coordinates = result['code']
633+
```
634+
614635
## Other methods
615636

616637
### send / get_result
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import asyncio
2+
import os
3+
import sys
4+
5+
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))
6+
7+
from twocaptcha import AsyncTwoCaptcha
8+
9+
# in this example we store the API key inside environment variables that can be set like:
10+
# export APIKEY_2CAPTCHA=1abc234de56fab7c89012d34e56fa7b8 on Linux or macOS
11+
# set APIKEY_2CAPTCHA=1abc234de56fab7c89012d34e56fa7b8 on Windows
12+
# you can just set the API key directly to it's value like:
13+
# api_key="1abc234de56fab7c89012d34e56fa7b8"
14+
15+
api_key = os.getenv('APIKEY_2CAPTCHA', 'YOUR_API_KEY')
16+
17+
solver = AsyncTwoCaptcha(api_key)
18+
19+
20+
async def solve_captcha():
21+
try:
22+
return await solver.drag_and_drop(body='../images/drag_drop_main.jpeg',
23+
images=['../images/drag_drop_image1.jpeg', '../images/drag_drop_image2.jpeg'])
24+
except Exception as e:
25+
sys.exit(e)
26+
27+
28+
if __name__ == '__main__':
29+
result = asyncio.run(solve_captcha())
30+
sys.exit('result: ' + str(result))
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import asyncio
2+
import os
3+
import sys
4+
from base64 import b64encode
5+
6+
import aiofiles
7+
8+
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))
9+
10+
from twocaptcha import AsyncTwoCaptcha
11+
12+
# in this example we store the API key inside environment variables that can be set like:
13+
# export APIKEY_2CAPTCHA=1abc234de56fab7c89012d34e56fa7b8 on Linux or macOS
14+
# set APIKEY_2CAPTCHA=1abc234de56fab7c89012d34e56fa7b8 on Windows
15+
# you can just set the API key directly to it's value like:
16+
# api_key="1abc234de56fab7c89012d34e56fa7b8"
17+
18+
api_key = os.getenv('APIKEY_2CAPTCHA', 'YOUR_API_KEY')
19+
20+
solver = AsyncTwoCaptcha(api_key)
21+
22+
23+
async def solve_captcha():
24+
async with aiofiles.open('../images/drag_drop_main.jpeg', 'rb') as f:
25+
body_b64 = b64encode(await f.read()).decode('utf-8')
26+
27+
async with aiofiles.open('../images/drag_drop_image1.jpeg', 'rb') as f:
28+
image1_b64 = b64encode(await f.read()).decode('utf-8')
29+
30+
async with aiofiles.open('../images/drag_drop_image2.jpeg', 'rb') as f:
31+
image2_b64 = b64encode(await f.read()).decode('utf-8')
32+
33+
try:
34+
return await solver.drag_and_drop(body=body_b64,
35+
images=[image1_b64, image2_b64])
36+
except Exception as e:
37+
sys.exit(e)
38+
39+
40+
if __name__ == '__main__':
41+
result = asyncio.run(solve_captcha())
42+
sys.exit('result: ' + str(result))
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import asyncio
2+
import os
3+
import sys
4+
5+
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))
6+
7+
from twocaptcha import AsyncTwoCaptcha
8+
9+
# in this example we store the API key inside environment variables that can be set like:
10+
# export APIKEY_2CAPTCHA=1abc234de56fab7c89012d34e56fa7b8 on Linux or macOS
11+
# set APIKEY_2CAPTCHA=1abc234de56fab7c89012d34e56fa7b8 on Windows
12+
# you can just set the API key directly to it's value like:
13+
# api_key="1abc234de56fab7c89012d34e56fa7b8"
14+
15+
api_key = os.getenv('APIKEY_2CAPTCHA', 'YOUR_API_KEY')
16+
17+
solver = AsyncTwoCaptcha(api_key, defaultTimeout=120, pollingInterval=10)
18+
19+
20+
async def solve_captcha():
21+
try:
22+
return await solver.drag_and_drop(
23+
body='../images/drag_drop_main.jpeg',
24+
images=['../images/drag_drop_image1.jpeg', '../images/drag_drop_image2.jpeg'],
25+
hintText='Drag the images to proper position',
26+
language=0,
27+
lang='en',
28+
header_acao=0,
29+
)
30+
except Exception as e:
31+
sys.exit(e)
32+
33+
34+
if __name__ == '__main__':
35+
result = asyncio.run(solve_captcha())
36+
sys.exit('result: ' + str(result))
7.29 KB
Loading
9.11 KB
Loading
82.8 KB
Loading

examples/sync/drag_and_drop.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import sys
2+
import os
3+
4+
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))
5+
6+
from twocaptcha import TwoCaptcha
7+
8+
# in this example we store the API key inside environment variables that can be set like:
9+
# export APIKEY_2CAPTCHA=1abc234de56fab7c89012d34e56fa7b8 on Linux or macOS
10+
# set APIKEY_2CAPTCHA=1abc234de56fab7c89012d34e56fa7b8 on Windows
11+
# you can just set the API key directly to it's value like:
12+
# api_key="1abc234de56fab7c89012d34e56fa7b8"
13+
14+
api_key = os.getenv('APIKEY_2CAPTCHA', 'YOUR_API_KEY')
15+
16+
solver = TwoCaptcha(api_key)
17+
18+
try:
19+
result = solver.drag_and_drop(body='../images/drag_drop_main.jpeg',
20+
images=['../images/drag_drop_image1.jpeg', '../images/drag_drop_image2.jpeg'])
21+
22+
except Exception as e:
23+
sys.exit(str(e))
24+
25+
else:
26+
sys.exit('result: ' + str(result))
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import sys
2+
import os
3+
from base64 import b64encode
4+
5+
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))
6+
7+
from twocaptcha import TwoCaptcha
8+
9+
# in this example we store the API key inside environment variables that can be set like:
10+
# export APIKEY_2CAPTCHA=1abc234de56fab7c89012d34e56fa7b8 on Linux or macOS
11+
# set APIKEY_2CAPTCHA=1abc234de56fab7c89012d34e56fa7b8 on Windows
12+
# you can just set the API key directly to it's value like:
13+
# api_key="1abc234de56fab7c89012d34e56fa7b8"
14+
15+
api_key = os.getenv('APIKEY_2CAPTCHA', 'YOUR_API_KEY')
16+
17+
solver = TwoCaptcha(api_key)
18+
19+
with open('../images/drag_drop_main.jpeg', 'rb') as f:
20+
body_b64 = b64encode(f.read()).decode('utf-8')
21+
22+
with open('../images/drag_drop_image1.jpeg', 'rb') as f:
23+
image1_b64 = b64encode(f.read()).decode('utf-8')
24+
25+
with open('../images/drag_drop_image2.jpeg', 'rb') as f:
26+
image2_b64 = b64encode(f.read()).decode('utf-8')
27+
28+
try:
29+
result = solver.drag_and_drop(body=body_b64,
30+
images=[image1_b64, image2_b64])
31+
32+
except Exception as e:
33+
sys.exit(str(e))
34+
35+
else:
36+
sys.exit('result: ' + str(result))
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import sys
2+
import os
3+
4+
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))
5+
6+
from twocaptcha import TwoCaptcha
7+
8+
# in this example we store the API key inside environment variables that can be set like:
9+
# export APIKEY_2CAPTCHA=1abc234de56fab7c89012d34e56fa7b8 on Linux or macOS
10+
# set APIKEY_2CAPTCHA=1abc234de56fab7c89012d34e56fa7b8 on Windows
11+
# you can just set the API key directly to it's value like:
12+
# api_key="1abc234de56fab7c89012d34e56fa7b8"
13+
14+
api_key = os.getenv('APIKEY_2CAPTCHA', 'YOUR_API_KEY')
15+
16+
solver = TwoCaptcha(api_key, defaultTimeout=120, pollingInterval=10)
17+
18+
try:
19+
result = solver.drag_and_drop(
20+
body='../images/drag_drop_main.jpeg',
21+
images=['../images/drag_drop_image1.jpeg', '../images/drag_drop_image2.jpeg'],
22+
hintText='Drag the images to proper position',
23+
language=0,
24+
lang='en',
25+
header_acao=0,
26+
)
27+
28+
except Exception as e:
29+
sys.exit(str(e))
30+
31+
else:
32+
sys.exit('result: ' + str(result))

0 commit comments

Comments
 (0)