Skip to content

Commit b59814a

Browse files
committed
added altcha captcha method to solver.py, async_solver.py, both examples, tests, description in readme and fix test_async_temu
1 parent b27454c commit b59814a

File tree

10 files changed

+280
-1
lines changed

10 files changed

+280
-1
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Examples of API requests for different captcha types are available on the [Pytho
4848
- [Prosopo](#prosopo)
4949
- [Temu](#temu)
5050
- [CyberSiARA](#cybersiara)
51+
- [Altcha Captcha](#altcha-Captcha)
5152
- [Other methods](#other-methods)
5253
- [send / get\_result](#send--get_result)
5354
- [balance](#balance)
@@ -524,6 +525,17 @@ result = solver.cybersiara(master_url_id='tpjOCKjjpdzv3d8Ub2E9COEWKt1vl1Mv',
524525
param1=..., ...)
525526
```
526527

528+
### Altcha Captcha
529+
530+
<sup>[API method description.](http://2captcha.com/2captcha-api#altchacaptcha)</sup>
531+
532+
Use this method to solve Altcha Captcha. Returns a token.
533+
```python
534+
result = solver.altcha(pageurl='https://mysite.com/page/with/altcha',
535+
challenge_json='{"algorithm":"SHA-256","challenge":"a4c9d8e7f1b23a6c...",..."signature":"7b3e2a9d5c8f1046e2d91c3a..."}',
536+
# or: challenge_url='https://example.com/altcha-challenge',)
537+
```
538+
527539
## Other methods
528540

529541
### send / get_result

examples/async/async_altcha.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
async def solve_captcha():
20+
try:
21+
return await solver.altcha(
22+
pageurl='https://mysite.com/page/with/altcha',
23+
challenge_json='{"algorithm":"SHA-256","challenge":"a4c9d8e7f1b23a6c...",..."signature":"7b3e2a9d5c8f1046e2d91c3a..."}',
24+
# challenge_url='https://example/altcha',
25+
)
26+
except Exception as e:
27+
sys.exit(e)
28+
29+
if __name__ == '__main__':
30+
result = asyncio.run(solve_captcha())
31+
sys.exit('result: ' + str(result))
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
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(api_key)
27+
28+
async def solve_captcha():
29+
try:
30+
return await solver.altcha(
31+
pageurl='https://mysite.com/page/with/altcha',
32+
challenge_json='{"algorithm":"SHA-256","challenge":"a4c9d8e7f1b23a6c...",..."signature":"7b3e2a9d5c8f1046e2d91c3a..."}',
33+
# challenge_url='https://example/altcha',
34+
)
35+
except Exception as e:
36+
sys.exit(e)
37+
38+
if __name__ == '__main__':
39+
result = asyncio.run(solve_captcha())
40+
sys.exit('result: ' + str(result))

examples/sync/altcha.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import sys
2+
import os
3+
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))
4+
5+
from twocaptcha import TwoCaptcha
6+
7+
# in this example we store the API key inside environment variables that can be set like:
8+
# export APIKEY_2CAPTCHA=1abc234de56fab7c89012d34e56fa7b8 on Linux or macOS
9+
# set APIKEY_2CAPTCHA=1abc234de56fab7c89012d34e56fa7b8 on Windows
10+
# you can just set the API key directly to it's value like:
11+
# api_key="1abc234de56fab7c89012d34e56fa7b8"
12+
13+
api_key = os.getenv('APIKEY_2CAPTCHA', 'YOUR_API_KEY')
14+
15+
solver = TwoCaptcha(api_key)
16+
17+
try:
18+
result = solver.altcha(
19+
pageurl='https://mysite.com/page/with/altcha',
20+
challenge_json='{"algorithm":"SHA-256","challenge":"a4c9d8e7f1b23a6c...",..."signature":"7b3e2a9d5c8f1046e2d91c3a..."}',
21+
# challenge_url='https://example/altcha',
22+
)
23+
24+
except Exception as e:
25+
sys.exit(e)
26+
27+
else:
28+
sys.exit('result: ' + str(result))

examples/sync/altcha_options.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import sys
2+
import os
3+
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))
4+
5+
from twocaptcha import TwoCaptcha
6+
7+
# in this example we store the API key inside environment variables that can be set like:
8+
# export APIKEY_2CAPTCHA=1abc234de56fab7c89012d34e56fa7b8 on Linux or macOS
9+
# set APIKEY_2CAPTCHA=1abc234de56fab7c89012d34e56fa7b8 on Windows
10+
# you can just set the API key directly to it's value like:
11+
# api_key="1abc234de56fab7c89012d34e56fa7b8"
12+
13+
api_key = os.getenv('APIKEY_2CAPTCHA', 'YOUR_API_KEY')
14+
15+
config = {
16+
'server': '2captcha.com', # can be also set to 'rucaptcha.com'
17+
'apiKey': api_key,
18+
'softId': 123,
19+
'defaultTimeout': 120,
20+
'recaptchaTimeout': 600,
21+
'pollingInterval': 10,
22+
}
23+
24+
solver = TwoCaptcha(api_key)
25+
26+
try:
27+
result = solver.altcha(
28+
pageurl='https://mysite.com/page/with/altcha',
29+
challenge_json='{"algorithm":"SHA-256","challenge":"a4c9d8e7f1b23a6c...",..."signature":"7b3e2a9d5c8f1046e2d91c3a..."}',
30+
# challenge_url='https://example/altcha',
31+
)
32+
33+
except Exception as e:
34+
sys.exit(e)
35+
36+
else:
37+
sys.exit('result: ' + str(result))

tests/async/test_async_altcha.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 AsyncAltchaTest(AsyncAbstractTest):
12+
def test_all_params(self):
13+
params = {
14+
'pageurl': 'https://mysite.com/page/with/altcha',
15+
'challenge_json': '{"algorithm":"SHA-256","challenge":"a4c9d8e7f1b23a6c...",..."signature":"7b3e2a9d5c8f1046e2d91c3a..."}',
16+
}
17+
18+
sends = {
19+
'method': 'altcha',
20+
'pageurl': 'https://mysite.com/page/with/altcha',
21+
'challenge_json': '{"algorithm":"SHA-256","challenge":"a4c9d8e7f1b23a6c...",..."signature":"7b3e2a9d5c8f1046e2d91c3a..."}',
22+
}
23+
24+
self.send_return(sends, self.solver.altcha, **params)
25+
26+
27+
if __name__ == '__main__':
28+
unittest.main()

tests/async/test_async_temu.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_all_params(self):
2525
'part3': 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
2626
}
2727

28-
self.send_return(sends, self.solver.lemin, **params)
28+
self.send_return(sends, self.solver.temu, **params)
2929

3030

3131
if __name__ == '__main__':

tests/sync/test_altcha.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 AltchaTest(AbstractTest):
12+
13+
def test_all_params(self):
14+
params = {
15+
'pageurl': 'https://mysite.com/page/with/altcha',
16+
'challenge_json': '{"algorithm":"SHA-256","challenge":"a4c9d8e7f1b23a6c...",..."signature":"7b3e2a9d5c8f1046e2d91c3a..."}',
17+
}
18+
19+
sends = {
20+
'method': 'altcha',
21+
'pageurl': 'https://mysite.com/page/with/altcha',
22+
'challenge_json': '{"algorithm":"SHA-256","challenge":"a4c9d8e7f1b23a6c...",..."signature":"7b3e2a9d5c8f1046e2d91c3a..."}',
23+
}
24+
25+
return self.send_return(sends, self.solver.altcha, **params)
26+
27+
28+
if __name__ == '__main__':
29+
unittest.main()

twocaptcha/async_solver.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,41 @@ async def yandex_smart(self, sitekey, url, **kwargs):
998998
**kwargs)
999999
return result
10001000

1001+
async def altcha(self, pageurl, challenge_url=None, challenge_json=None, **kwargs):
1002+
'''Wrapper for solving Altcha Captcha.
1003+
1004+
Parameters
1005+
__________
1006+
pageurl : str
1007+
Full URL of the page where you solve the captcha.
1008+
challenge_url : str
1009+
The value of the 'challenge_url' parameter for the 'altcha-widget' element containing the captcha on the page.
1010+
You can send either challenge_url or challenge_json parameter, but not two of it simultaneously.
1011+
challenge_json : str
1012+
The contents of the file from the 'challenge_url' parameter. You can send either challenge_url or challenge_json
1013+
parameter, but not two of it simultaneously.
1014+
proxy : dict, optional
1015+
{'type': 'HTTPS', 'uri': 'login:password@IP_address:PORT'}.
1016+
1017+
'''
1018+
1019+
if (challenge_url is None) == (challenge_json is None):
1020+
raise ValidationException(
1021+
'You must provide exactly one of challenge_url or challenge_json'
1022+
)
1023+
1024+
params = {
1025+
'pageurl': pageurl,
1026+
'method': 'altcha',
1027+
**kwargs,}
1028+
1029+
if challenge_url is not None:
1030+
params['challenge_url'] = challenge_url
1031+
if challenge_json is not None:
1032+
params['challenge_json'] = challenge_json
1033+
1034+
return await self.solve(**params)
1035+
10011036
async def solve(self, timeout=0, polling_interval=0, **kwargs):
10021037
'''Sends captcha, receives result.
10031038

twocaptcha/solver.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ class TwoCaptcha():
105105
Use this method to solve DataDome captcha.
106106
cybersiara(master_url_id, pageurl, userAgent, **kwargs)
107107
Use this method to solve CyberSiARA. Returns a token.
108+
yandex_smart(self, sitekey, url, **kwargs)
109+
Wrapper for solving Yandex Smart.
110+
altcha(self, pageurl, challenge_url=None, challenge_json=None, **kwargs)
111+
Wrapper for solving Altcha Captcha.
108112
solve(timeout=0, polling_interval=0, **kwargs)
109113
Sends CAPTCHA data and retrieves the result.
110114
balance()
@@ -1128,6 +1132,41 @@ def yandex_smart(self, sitekey, url, **kwargs):
11281132
**kwargs)
11291133
return result
11301134

1135+
def altcha(self, pageurl, challenge_url=None, challenge_json=None, **kwargs):
1136+
'''Wrapper for solving Altcha Captcha.
1137+
1138+
Parameters
1139+
__________
1140+
pageurl : str
1141+
Full URL of the page where you solve the captcha.
1142+
challenge_url : str
1143+
The value of the 'challenge_url' parameter for the 'altcha-widget' element containing the captcha on the page.
1144+
You can send either challenge_url or challenge_json parameter, but not two of it simultaneously.
1145+
challenge_json : str
1146+
The contents of the file from the 'challenge_url' parameter. You can send either challenge_url or challenge_json
1147+
parameter, but not two of it simultaneously.
1148+
proxy : dict, optional
1149+
{'type': 'HTTPS', 'uri': 'login:password@IP_address:PORT'}.
1150+
1151+
'''
1152+
1153+
if (challenge_url is None) == (challenge_json is None):
1154+
raise ValidationException(
1155+
'You must provide exactly one of challenge_url or challenge_json'
1156+
)
1157+
params = {
1158+
'pageurl': pageurl,
1159+
'method': "altcha",
1160+
**kwargs,
1161+
}
1162+
if challenge_url is not None:
1163+
params['challenge_url'] = challenge_url
1164+
if challenge_json is not None:
1165+
params['challenge_json'] = challenge_json
1166+
1167+
return self.solve(**params)
1168+
1169+
11311170
def solve(self, timeout=0, polling_interval=0, **kwargs):
11321171
'''Sends captcha, receives result.
11331172

0 commit comments

Comments
 (0)