From 3bfd62ecb945989785e3c15066e78d4a55431aa8 Mon Sep 17 00:00:00 2001 From: Arkadiy Polukhin Date: Fri, 6 Oct 2023 18:40:13 +0600 Subject: [PATCH] Rewrite audio solver --- twocaptcha/solver.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/twocaptcha/solver.py b/twocaptcha/solver.py index bf4ca39..e8581f5 100755 --- a/twocaptcha/solver.py +++ b/twocaptcha/solver.py @@ -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):