Skip to content

Commit 310695e

Browse files
kratzkyiamarkadiypolukhin2captcha
authored
Audio support (#64)
* Add audio method for solver. #RC-2041 * add audio example, update text * audio test * update audio test * Delete method arg * Rewrite audio solver * update audio example * add audio example * rm audio test --------- Co-authored-by: Arkadiy Polukhin <polukhin.arkhadiy@gmail.com> Co-authored-by: 2captcha <support@2captcha.com>
1 parent fbf0e02 commit 310695e

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The easiest way to quickly integrate [2Captcha] captcha solving service into you
88
- [Solve captcha](#solve-captcha)
99
- [Captcha options](#captcha-options)
1010
- [Normal Captcha](#normal-captcha)
11+
- [Audio Captcha](#audio-captcha)
1112
- [Text Captcha](#text-captcha)
1213
- [ReCaptcha v2](#recaptcha-v2)
1314
- [ReCaptcha v3](#recaptcha-v3)
@@ -103,6 +104,16 @@ result = solver.normal('path/to/captcha.jpg', param1=..., ...)
103104
result = solver.normal('https://site-with-captcha.com/path/to/captcha.jpg', param1=..., ...)
104105
```
105106

107+
### Audio Captcha
108+
To bypass an audio captcha (mp3 formats only) use the following method.
109+
You must provife the language as `lang = 'en'`. Supported languages are "en", "ru", "de", "el", "pt".
110+
111+
```python
112+
result = solver.audio('path/to/captcha.mp3', lang = 'lang', param1=..., ...)
113+
# OR
114+
result = solver.audio('https://site-with-captcha.com/path/to/captcha.mp3', lang = 'lang', param1=..., ...)
115+
```
116+
106117
### Text Captcha
107118
This method can be used to bypass a captcha that requires to answer a question provided in clear text.
108119
```python

examples/audio.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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', 'YOUR_API_KEY')
15+
16+
solver = TwoCaptcha(api_key)
17+
18+
try:
19+
result = solver.audio('./audio/example.mp3', lang='en')
20+
21+
except Exception as e:
22+
sys.exit(e)
23+
24+
else:
25+
sys.exit('result: ' + str(result))

examples/audio/example.mp3

33.5 KB
Binary file not shown.

twocaptcha/solver.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,39 @@ def normal(self, file, **kwargs):
8181
result = self.solve(**method, **kwargs)
8282
return result
8383

84+
def audio(self, file, lang, **kwargs):
85+
'''
86+
Wrapper for solving audio captcha
87+
88+
Required:
89+
file (base64, or url to mp3 file)
90+
lang ("en", "ru", "de", "el", "pt")
91+
92+
Optional params:
93+
'''
94+
method = "audio"
95+
96+
if not file:
97+
raise ValidationException('File is none')
98+
elif not '.' in file and len(file) > 50:
99+
body = file
100+
elif file.endswith(".mp3") and file.startswith("http"):
101+
response = requests.get(file)
102+
if response.status_code != 200:
103+
raise ValidationException(f'File could not be downloaded from url: {file}')
104+
body = b64encode(response.content).decode('utf-8')
105+
elif file.endswith(".mp3"):
106+
with open(file, "rb") as media:
107+
body = b64encode(media.read()).decode('utf-8')
108+
else:
109+
raise ValidationException('File extension is not .mp3 or it is not a base64 string.')
110+
111+
if not lang or lang not in ("en", "ru", "de", "el", "pt"):
112+
raise ValidationException(f'Lang not in "en", "ru", "de", "el", "pt". You send {lang}')
113+
114+
result = self.solve(body=body, method=method, **kwargs)
115+
return result
116+
84117
def text(self, text, **kwargs):
85118
'''
86119
Wrapper for solving text captcha
@@ -524,7 +557,7 @@ def get_method(self, file):
524557

525558
if not '.' in file and len(file) > 50:
526559
return {'method': 'base64', 'body': file}
527-
560+
528561
if file.startswith('http'):
529562
img_resp = requests.get(file)
530563
if img_resp.status_code != 200:

0 commit comments

Comments
 (0)