Skip to content

Commit f596f77

Browse files
authored
Add Alibaba captcha support with tests and examples (#143)
1 parent d981352 commit f596f77

10 files changed

Lines changed: 344 additions & 1 deletion

File tree

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Examples of API requests for different captcha types are available on the [Pytho
5252
- [Binance](#binance)
5353
- [Yidun](#yidun)
5454
- [Hunt](#hunt)
55+
- [Alibaba](#alibaba)
5556
- [Other methods](#other-methods)
5657
- [send / get\_result](#send--get_result)
5758
- [balance](#balance)
@@ -572,6 +573,18 @@ result = solver.hunt(pageurl='https://example.com/page-with-hunt',
572573
)
573574
```
574575

576+
### Alibaba
577+
578+
<sup>[API method description.](https://2captcha.com/2captcha-api#alibaba)</sup>
579+
580+
Use this method to solve Alibaba captcha. Returns a token.
581+
```python
582+
result = solver.alibaba(pageurl='https://www.example.com',
583+
scene_id='abc123xyz4',
584+
prefix='dlw3kug',
585+
)
586+
```
587+
575588
## Other methods
576589

577590
### send / get_result

examples/async/async_alibaba.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import asyncio
2+
import sys
3+
import os
4+
5+
sys.path.append(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+
async def solve_captcha():
20+
try:
21+
return await solver.alibaba(
22+
pageurl='https://www.example.com',
23+
scene_id='abc123xyz4',
24+
prefix='dlw3kug',
25+
)
26+
27+
except Exception as e:
28+
sys.exit(e)
29+
30+
if __name__ == '__main__':
31+
result = asyncio.run(solve_captcha())
32+
sys.exit('result: ' + str(result))
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import asyncio
2+
import sys
3+
import os
4+
5+
sys.path.append(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+
config = {
18+
'server': '2captcha.com', # can be also set to 'rucaptcha.com'
19+
'apiKey': api_key,
20+
'softId': 123,
21+
'defaultTimeout': 120,
22+
'recaptchaTimeout': 600,
23+
'pollingInterval': 10,
24+
}
25+
26+
solver = AsyncTwoCaptcha(**config)
27+
28+
async def solve_captcha():
29+
try:
30+
return await solver.alibaba(
31+
pageurl='https://www.example.com',
32+
scene_id='abc123xyz4',
33+
prefix='dlw3kug',
34+
user_id='Abc123Def456Ghi789Jkl012Mno345Pqr678Stu901=',
35+
user_user_id='Xyz987Abc654Def321Ghi098Jkl765Mno432Pqr109=',
36+
verify_type='1.0',
37+
region='sgp',
38+
user_certify_id='abc123def456ghi789jkl012mno345pq',
39+
api_get_lib='https://o.example.com/captcha-frontend/aliyunCaptcha/AliyunCaptcha.js?t=2041',
40+
useragent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36',
41+
# proxy={'type': 'HTTP',
42+
# 'uri': 'login:password@IP_address:PORT'}
43+
)
44+
except Exception as e:
45+
sys.exit(e)
46+
47+
if __name__ == '__main__':
48+
result = asyncio.run(solve_captcha())
49+
sys.exit('result: ' + str(result))

examples/sync/alibaba.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import sys
2+
import os
3+
4+
sys.path.append(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.alibaba(
20+
pageurl='https://www.example.com',
21+
scene_id='abc123xyz4',
22+
prefix='dlw3kug',
23+
)
24+
25+
except Exception as e:
26+
sys.exit(e)
27+
28+
else:
29+
sys.exit('result: ' + str(result))

examples/sync/alibaba_options.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import sys
2+
import os
3+
4+
sys.path.append(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+
config = {
17+
'server': '2captcha.com', # can be also set to 'rucaptcha.com'
18+
'apiKey': api_key,
19+
'softId': 123,
20+
'defaultTimeout': 120,
21+
'recaptchaTimeout': 600,
22+
'pollingInterval': 10,
23+
}
24+
25+
solver = TwoCaptcha(**config)
26+
27+
try:
28+
result = solver.alibaba(
29+
pageurl='https://www.example.com',
30+
scene_id='abc123xyz4',
31+
prefix='dlw3kug',
32+
user_id='Abc123Def456Ghi789Jkl012Mno345Pqr678Stu901=',
33+
user_user_id='Xyz987Abc654Def321Ghi098Jkl765Mno432Pqr109=',
34+
verify_type='1.0',
35+
region='sgp',
36+
user_certify_id='abc123def456ghi789jkl012mno345pq',
37+
api_get_lib='https://o.example.com/captcha-frontend/aliyunCaptcha/AliyunCaptcha.js?t=2041',
38+
useragent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36',
39+
# proxy={'type': 'HTTP',
40+
# 'uri': 'login:password@IP_address:PORT'}
41+
)
42+
43+
except Exception as e:
44+
sys.exit(e)
45+
46+
else:
47+
sys.exit('result: ' + str(result))

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ def get_version():
3636
'2captcha', 'captcha', 'api', 'captcha solver', 'reCAPTCHA',
3737
'FunCaptcha', 'Geetest', 'image captcha', 'Coordinates', 'Click Captcha',
3838
'Geetest V4', 'Lemin captcha', 'Amazon WAF', 'Cloudflare Turnstile',
39-
'Capy Puzzle', 'MTCaptcha', 'Friendly Captcha', 'Tencent', 'Cutcaptcha', 'DataDome', 'VK Captcha', 'CaptchaFox', 'Prosopo', 'cybersiara', 'Hunt'],
39+
'Capy Puzzle', 'MTCaptcha', 'Friendly Captcha', 'Tencent', 'Cutcaptcha', 'DataDome', 'VK Captcha', 'CaptchaFox', 'Prosopo', 'cybersiara', 'Hunt', 'Alibaba'],
4040
python_requires='>=3.8',
4141
test_suite='tests')

tests/async/test_async_alibaba.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python3
2+
3+
import unittest
4+
5+
try:
6+
from .abstract_async import AsyncAbstractTest
7+
except ImportError:
8+
from abstract_async import AsyncAbstractTest
9+
10+
11+
class AsyncAlibaba(AsyncAbstractTest):
12+
def test_all_params(self):
13+
params = {
14+
'pageurl': 'https://www.example.com',
15+
'scene_id': 'abc123xyz4',
16+
'prefix': 'dlw3kug',
17+
'user_id': 'Abc123Def456Ghi789Jkl012Mno345Pqr678Stu901=',
18+
'user_user_id': 'Xyz987Abc654Def321Ghi098Jkl765Mno432Pqr109=',
19+
'verify_type': '1.0',
20+
'region': 'sgp',
21+
'user_certify_id': 'abc123def456ghi789jkl012mno345pq',
22+
'api_get_lib': 'https://o.example.com/captcha-frontend/aliyunCaptcha/AliyunCaptcha.js?t=2041',
23+
'useragent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36',
24+
'proxy': {'type': 'HTTP',
25+
'uri': 'login:password@IP_address:PORT'}
26+
}
27+
28+
sends = {
29+
'method': 'alibaba',
30+
'pageurl': 'https://www.example.com',
31+
'scene_id': 'abc123xyz4',
32+
'prefix': 'dlw3kug',
33+
'user_id': 'Abc123Def456Ghi789Jkl012Mno345Pqr678Stu901=',
34+
'user_user_id': 'Xyz987Abc654Def321Ghi098Jkl765Mno432Pqr109=',
35+
'verify_type': '1.0',
36+
'region': 'sgp',
37+
'user_certify_id': 'abc123def456ghi789jkl012mno345pq',
38+
'api_get_lib': 'https://o.example.com/captcha-frontend/aliyunCaptcha/AliyunCaptcha.js?t=2041',
39+
'useragent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36',
40+
'proxytype': 'HTTP',
41+
'proxy': 'login:password@IP_address:PORT'
42+
}
43+
44+
self.send_return(sends, self.solver.alibaba, **params)
45+
46+
47+
if __name__ == '__main__':
48+
unittest.main()

tests/sync/test_alibaba.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python3
2+
3+
import unittest
4+
5+
try:
6+
from .abstract import AbstractTest
7+
except ImportError:
8+
from abstract import AbstractTest
9+
10+
11+
class CaptchaAlibaba(AbstractTest):
12+
13+
def test_all_params(self):
14+
params = {
15+
'pageurl': 'https://www.example.com',
16+
'scene_id': 'abc123xyz4',
17+
'prefix': 'dlw3kug',
18+
'user_id': 'Abc123Def456Ghi789Jkl012Mno345Pqr678Stu901=',
19+
'user_user_id': 'Xyz987Abc654Def321Ghi098Jkl765Mno432Pqr109=',
20+
'verify_type': '1.0',
21+
'region': 'sgp',
22+
'user_certify_id': 'abc123def456ghi789jkl012mno345pq',
23+
'api_get_lib': 'https://o.example.com/captcha-frontend/aliyunCaptcha/AliyunCaptcha.js?t=2041',
24+
'useragent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36',
25+
'proxy': {'type': 'HTTP',
26+
'uri': 'login:password@IP_address:PORT'}
27+
}
28+
29+
sends = {
30+
'method': 'alibaba',
31+
'pageurl': 'https://www.example.com',
32+
'scene_id': 'abc123xyz4',
33+
'prefix': 'dlw3kug',
34+
'user_id': 'Abc123Def456Ghi789Jkl012Mno345Pqr678Stu901=',
35+
'user_user_id': 'Xyz987Abc654Def321Ghi098Jkl765Mno432Pqr109=',
36+
'verify_type': '1.0',
37+
'region': 'sgp',
38+
'user_certify_id': 'abc123def456ghi789jkl012mno345pq',
39+
'api_get_lib': 'https://o.example.com/captcha-frontend/aliyunCaptcha/AliyunCaptcha.js?t=2041',
40+
'useragent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36',
41+
'proxytype': 'HTTP',
42+
'proxy': 'login:password@IP_address:PORT'
43+
}
44+
45+
return self.send_return(sends, self.solver.alibaba, **params)
46+
47+
48+
if __name__ == '__main__':
49+
unittest.main()

twocaptcha/async_solver.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,43 @@ async def hunt(self, pageurl, api_get_lib, **kwargs):
11021102

11031103
return await result
11041104

1105+
async def alibaba(self, pageurl, scene_id, prefix, **kwargs):
1106+
'''Wrapper for solving Alibaba captcha.
1107+
1108+
Parameters
1109+
__________
1110+
pageurl : str
1111+
Full URL of the page with the captcha.
1112+
scene_id : str
1113+
Captcha scenario identifier.
1114+
prefix : str
1115+
Prefix from the captcha loading request subdomain.
1116+
user_id : str, optional
1117+
User or session identifier on the website.
1118+
user_user_id : str, optional
1119+
Additional user identifier.
1120+
verify_type : str, optional
1121+
Verification mechanism version or type.
1122+
region : str, optional
1123+
Captcha processing region.
1124+
user_certify_id : str, optional
1125+
Verification ID for the current captcha session.
1126+
api_get_lib : str, optional
1127+
URL of the Alibaba Captcha JS library.
1128+
useragent : str, optional
1129+
Browser User-Agent used to open the page.
1130+
proxy : dict, optional
1131+
{'type': 'HTTPS', 'uri': 'login:password@IP_address:PORT'}.
1132+
'''
1133+
result = self.solve(
1134+
method="alibaba",
1135+
pageurl=pageurl,
1136+
scene_id=scene_id,
1137+
prefix=prefix,
1138+
**kwargs)
1139+
1140+
return await result
1141+
11051142
async def solve(self, timeout=0, polling_interval=0, **kwargs):
11061143
'''Sends captcha, receives result.
11071144

twocaptcha/solver.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ class TwoCaptcha():
115115
Wrapper for solving Yidun captcha.
116116
hunt(self, pageurl, api_get_lib, **kwargs)
117117
Wrapper for solving Hunt captcha.
118+
alibaba(self, pageurl, scene_id, prefix, **kwargs)
119+
Wrapper for solving Alibaba captcha.
118120
solve(timeout=0, polling_interval=0, **kwargs)
119121
Sends CAPTCHA data and retrieves the result.
120122
balance()
@@ -1246,6 +1248,43 @@ def hunt(self, pageurl, api_get_lib, **kwargs):
12461248

12471249
return result
12481250

1251+
def alibaba(self, pageurl, scene_id, prefix, **kwargs):
1252+
'''Wrapper for solving Alibaba captcha.
1253+
1254+
Parameters
1255+
__________
1256+
pageurl : str
1257+
Full URL of the page with the captcha.
1258+
scene_id : str
1259+
Captcha scenario identifier.
1260+
prefix : str
1261+
Prefix from the captcha loading request subdomain.
1262+
user_id : str, optional
1263+
User or session identifier on the website.
1264+
user_user_id : str, optional
1265+
Additional user identifier.
1266+
verify_type : str, optional
1267+
Verification mechanism version or type.
1268+
region : str, optional
1269+
Captcha processing region.
1270+
user_certify_id : str, optional
1271+
Verification ID for the current captcha session.
1272+
api_get_lib : str, optional
1273+
URL of the Alibaba Captcha JS library.
1274+
useragent : str, optional
1275+
Browser User-Agent used to open the page.
1276+
proxy : dict, optional
1277+
{'type': 'HTTPS', 'uri': 'login:password@IP_address:PORT'}.
1278+
'''
1279+
result = self.solve(
1280+
method="alibaba",
1281+
pageurl=pageurl,
1282+
scene_id=scene_id,
1283+
prefix=prefix,
1284+
**kwargs)
1285+
1286+
return result
1287+
12491288

12501289
def solve(self, timeout=0, polling_interval=0, **kwargs):
12511290
'''Sends captcha, receives result.

0 commit comments

Comments
 (0)