Skip to content

Commit

Permalink
Rewrite audio solver
Browse files Browse the repository at this point in the history
  • Loading branch information
iamarkadiypolukhin committed Oct 6, 2023
1 parent 73974b8 commit 3bfd62e
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions twocaptcha/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,27 @@ def audio(self, file, lang, **kwargs):
Optional params:
'''
#"method":"audio", #method
method = "audio"

if not ((not '.' in file and len(file) > 50) or (file.endswith(".mp3") and file.startswith("http"))):
if not file:
raise ValidationException('File is none')
elif not '.' in file and len(file) > 50:
body = file
elif file.endswith(".mp3") and file.startswith("http"):
response = requests.get(file)
if response.status_code != 200:
raise ValidationException(f'File could not be downloaded from url: {file}')
body = b64encode(response.content).decode('utf-8')
elif file.endswith(".mp3"):
with open(file, "rb") as media:
body = b64encode(media.read()).decode('utf-8')
else:
raise ValidationException('File extension is not .mp3 or it is not a base64 string.')

method = self.get_method(file)
method['method'] = "audio"

if not lang or lang not in ("en", "ru", "de", "el", "pt"):
raise ValidationException(f'Lang not in "en", "ru", "de", "el", "pt". You send {lang}')

result = self.solve(**method, **kwargs)
result = self.solve(body=body, method=method, **kwargs)
return result

def text(self, text, **kwargs):
Expand Down

0 comments on commit 3bfd62e

Please sign in to comment.