From 67b5a8084ab38d1c4fe6972a264a53b86e02ed67 Mon Sep 17 00:00:00 2001 From: Animenosekai <40539549+Animenosekai@users.noreply.github.com> Date: Sun, 25 Apr 2021 19:21:37 +0200 Subject: [PATCH] [add] Adding multiple endpoints for Google [add] Officially adding text to speech --- playground/translate | 2 + playground/tts | 4 +- translatepy/translate.py | 227 +++++++++++++----------------- translatepy/translators/google.py | 57 ++++++-- translatepy/translators/yandex.py | 5 +- 5 files changed, 152 insertions(+), 143 deletions(-) diff --git a/playground/translate b/playground/translate index e0d7ecf..69ab11c 100644 --- a/playground/translate +++ b/playground/translate @@ -5,6 +5,8 @@ data: { sl: sl, tl: tl }, + + https://api.cognitive.microsofttranslator.com/translate?api-version=3.0 $.ajax({ url: BING_TRANSLATION_URL + '&to=' + to + bing_from, diff --git a/playground/tts b/playground/tts index 0ad4ca5..f74f6a1 100644 --- a/playground/tts +++ b/playground/tts @@ -1,4 +1,3 @@ -https://translate.googleapis.com/translate_tts?client=gtx&ie=UTF-8&tl=&q= return "https://www.bing.com/tspeak?&format=audio%2Fmp3&language={{lang}}&IG=D5DFBE5EEA97455182D4DEA272551DCD&IID=translator.5036.43&text={{text}}" .replace('{{text}}', encodeURIComponent(text)) .replace('{{lang}}', lang); @@ -7,5 +6,8 @@ Mate Translate chrome-extension://jifpbeccnghkjeaalbbjmodiffmgedin/crxviewer.html?crx=https%3A%2F%2Fclients2.google.com%2Fservice%2Fupdate2%2Fcrx%3Fresponse%3Dredirect%26os%3Dmac%26arch%3Darm64%26os_arch%3Darm64%26nacl_arch%3Darm%26prod%3Dchromecrx%26prodchannel%3Dunknown%26prodversion%3D91.0.4464.5%26acceptformat%3Dcrx2%2Ccrx3%26x%3Did%253Dihmgiclibbndffejedjimfjmfoabpcke%2526uc&zipname=ihmgiclibbndffejedjimfjmfoabpcke.zip +https://translate.googleapis.com/translate_tts?client=gtx&ie=UTF-8&tl=&q= https://translate.google.com/translate_tts?client=tw-ob&q=${encodeURIComponent(e)}&tl=${t}` + + https://translate.google.com/translate_tts?ie=UTF-8&q=[SPEAK_TEXT]&tl=[SRC_LANG]&total=[TOTAL]&idx=[INDEX]&textlen=[TEXT_LENGTH]&client=t&prev=input" \ No newline at end of file diff --git a/translatepy/translate.py b/translatepy/translate.py index b612907..232fc21 100644 --- a/translatepy/translate.py +++ b/translatepy/translate.py @@ -1,5 +1,5 @@ """ -translatepy v1.5.2 (Stable) +translatepy v1.6 (Beta) © Anime no Sekai — 2021 """ @@ -82,26 +82,11 @@ def translate(self, text, destination_language, source_language=None) -> Union[T if _cache_key in TRANSLATION_CACHES: return TRANSLATION_CACHES[_cache_key] - lang, response = self.google_translate.translate(text, destination_language, source_language) - if response is None: - lang, response = self.bing_translate.translate(text, destination_language, source_language) - if response is None: - lang, response = self.deepl_translate.translate(text, destination_language, source_language) - if response is None: - lang, response = self.reverso_translate.translate(text, destination_language, source_language) - if response is None: - lang, response = self.yandex_translate.translate(text, destination_language, source_language) - if response is None and isinstance(self.yandex_translate, Unselected): - return None - try: - lang = Language(lang) - except Exception: pass - result = TranslationResult(source=text, result=response, source_language=lang, destination_language=destination_language, service=self.yandex_translate) - TRANSLATION_CACHES[str({"t": str(text), "d": str(destination_language), "s": str(source_language)})] = result - TRANSLATION_CACHES[str({"t": str(text), "d": str(destination_language), "s": str(lang)})] = result - if response is None: - return None - return result + services = [self.google_translate, self.bing_translate, self.deepl_translate, self.reverso_translate, self.yandex_translate] + for service in services: + if not isinstance(service, Unselected): + lang, response = service.translate(text, destination_language, source_language) + if response is not None: try: lang = Language(lang) except Exception: pass @@ -109,27 +94,7 @@ def translate(self, text, destination_language, source_language=None) -> Union[T TRANSLATION_CACHES[str({"t": str(text), "d": str(destination_language), "s": str(source_language)})] = result TRANSLATION_CACHES[str({"t": str(text), "d": str(destination_language), "s": str(lang)})] = result return result - try: - lang = Language(lang) - except Exception: pass - result = TranslationResult(source=text, result=response, source_language=lang, destination_language=destination_language, service=self.deepl_translate) - TRANSLATION_CACHES[str({"t": str(text), "d": str(destination_language), "s": str(source_language)})] = result - TRANSLATION_CACHES[str({"t": str(text), "d": str(destination_language), "s": str(lang)})] = result - return result - try: - lang = Language(lang) - except Exception: pass - result = TranslationResult(source=text, result=response, source_language=lang, destination_language=destination_language, service=self.bing_translate) - TRANSLATION_CACHES[str({"t": str(text), "d": str(destination_language), "s": str(source_language)})] = result - TRANSLATION_CACHES[str({"t": str(text), "d": str(destination_language), "s": str(lang)})] = result - return result - try: - lang = Language(lang) - except Exception: pass - result = TranslationResult(source=text, result=response, source_language=lang, destination_language=destination_language, service=self.google_translate) - TRANSLATION_CACHES[str({"t": str(text), "d": str(destination_language), "s": str(source_language)})] = result - TRANSLATION_CACHES[str({"t": str(text), "d": str(destination_language), "s": str(lang)})] = result - return result + return None def transliterate(self, text, source_language=None) -> Union[str, None]: """ @@ -138,32 +103,31 @@ def transliterate(self, text, source_language=None) -> Union[str, None]: i.e おはよう --> Ohayou """ global TRANSLITERATION_CACHES + + + if str(text).replace(" ", "").replace("\n", "") == "": + return None + + ## language handling if source_language is not None and not isinstance(source_language, Language): source_language = Language(source_language) _cache_key = str({"t": str(text), "s": str(source_language)}) if _cache_key in TRANSLITERATION_CACHES: return TRANSLITERATION_CACHES[_cache_key] + + services = [self.google_translate, self.yandex_translate] + for service in services: + if not isinstance(service, Unselected): + lang, response = service.transliterate(text, source_language) + if response is not None: + try: + lang = Language(lang) + except Exception: pass - lang, response = self.google_translate.transliterate(text, source_language) - if response is None: - lang, response = self.yandex_translate.transliterate(text, source_language) - if response is None and isinstance(self.yandex_translate, Unselected): - return None - try: - lang = Language(lang) - except Exception: pass - - TRANSLITERATION_CACHES[str({"t": str(text), "s": str(source_language)})] = response - TRANSLITERATION_CACHES[str({"t": str(text), "s": str(lang)})] = response - return response - try: - lang = Language(lang) - except Exception: pass - - TRANSLITERATION_CACHES[str({"t": str(text), "s": str(source_language)})] = response - TRANSLITERATION_CACHES[str({"t": str(text), "s": str(lang)})] = response - return response + TRANSLITERATION_CACHES[str({"t": str(text), "s": str(source_language)})] = response + TRANSLITERATION_CACHES[str({"t": str(text), "s": str(lang)})] = response + return None def spellcheck(self, text, source_language=None) -> Union[str, None]: """ @@ -173,43 +137,32 @@ def spellcheck(self, text, source_language=None) -> Union[str, None]: """ global SPELLCHECK_CACHES - _cache_key = str({"t": str(text), "s": str(source_language)}) - if _cache_key in SPELLCHECK_CACHES: - return SPELLCHECK_CACHES[_cache_key] + if str(text).replace(" ", "").replace("\n", "") == "": + return None + + ## language handling if source_language is not None and not isinstance(source_language, Language): source_language = Language(source_language) + _cache_key = str({"t": str(text), "s": str(source_language)}) + if _cache_key in SPELLCHECK_CACHES: + return SPELLCHECK_CACHES[_cache_key] - lang, response = self.bing_translate.spellcheck(text, source_language) - if response is None: - lang, response = self.reverso_translate.spellcheck(text, source_language) - if response is None: - lang, response = self.yandex_translate.spellcheck(text, source_language) - try: - lang = Language(lang) - except Exception: pass - if response is None and isinstance(self.yandex_translate, Unselected): - return None - - SPELLCHECK_CACHES[str({"t": str(text), "s": str(source_language)})] = response - SPELLCHECK_CACHES[str({"t": str(text), "s": str(lang)})] = response - return response - try: - lang = Language(lang) - except Exception: pass - - SPELLCHECK_CACHES[str({"t": str(text), "s": str(source_language)})] = response - SPELLCHECK_CACHES[str({"t": str(text), "s": str(lang)})] = response - return response - try: - lang = Language(lang) - except Exception: pass - - SPELLCHECK_CACHES[str({"t": str(text), "s": str(source_language)})] = response - SPELLCHECK_CACHES[str({"t": str(text), "s": str(lang)})] = response - return response + services = [self.bing_translate, self.reverso_translate, self.yandex_translate] + for service in services: + if not isinstance(service, Unselected): + lang, response = self.bing_translate.spellcheck(text, source_language) + if response is not None: + try: + lang = Language(lang) + except Exception: pass + SPELLCHECK_CACHES[str({"t": str(text), "s": str(source_language)})] = response + SPELLCHECK_CACHES[str({"t": str(text), "s": str(lang)})] = response + return response + return None + def language(self, text) -> Union[Language, str, None]: """ Returns the language of the given text @@ -217,51 +170,28 @@ def language(self, text) -> Union[Language, str, None]: i.e 皆さんおはようございます! --> Japanese """ global LANGUAGE_CACHES + + + if str(text).replace(" ", "").replace("\n", "") == "": + return None + text = str(text) if text in LANGUAGE_CACHES: return LANGUAGE_CACHES[text] - response = self.google_translate.language(text) - if response is None: - response = self.bing_translate.language(text) - if response is None: - response = self.deepl_translate.language(text) - if response is None: - response = self.reverso_translate.language(text) - if response is None: - response = self.yandex_translate.language(text) - if response is None and isinstance(self.yandex_translate, Unselected): - return None - try: - response = Language(response) - except Exception: pass - - LANGUAGE_CACHES[text] = response - return response + services = [self.google_translate, self.bing_translate, self.deepl_translate, self.reverso_translate, self.yandex_translate] + for service in services: + if not isinstance(service, Unselected): + response = service.language(text) + if response is not None: try: response = Language(response) except Exception: pass LANGUAGE_CACHES[text] = response return response - try: - response = Language(response) - except Exception: pass - - LANGUAGE_CACHES[text] = response - return response - try: - response = Language(response) - except Exception: pass - - LANGUAGE_CACHES[text] = response - return response - try: - response = Language(response) - except Exception: pass + return None - LANGUAGE_CACHES[text] = response - return response def example(self, text, destination_language, source_language=None) -> Union[List, None]: """ @@ -270,6 +200,11 @@ def example(self, text, destination_language, source_language=None) -> Union[Lis i.e Hello --> ['Hello friends how are you?', 'Hello im back again.'] """ global EXAMPLE_CACHES + + if str(text).replace(" ", "").replace("\n", "") == "": + return None + + ## language handling if not isinstance(destination_language, Language): destination_language = Language(destination_language) if source_language is not None and not isinstance(source_language, Language): @@ -300,6 +235,11 @@ def dictionary(self, text, destination_language, source_language=None) -> Union[ _html and _response are also provided if you want to parse the HTML response (by DeepL/Linguee) by yourself """ global DICTIONARY_CACHES + + if str(text).replace(" ", "").replace("\n", "") == "": + return None + + ## language handling if not isinstance(destination_language, Language): destination_language = Language(destination_language) if source_language is not None and not isinstance(source_language, Language): @@ -320,6 +260,41 @@ def dictionary(self, text, destination_language, source_language=None) -> Union[ DICTIONARY_CACHES[str({"t": str(text), "d": str(destination_language), "s": str(lang)})] = response return response + def text_to_speech(self, text, source_language=None): + """ + Gives back the text to speech result for the given text + + Args: + text: the given text + source_language: the source language (Defaut value = None) + + Returns: + bytes --> the mp3 file as bytes + None --> when an error occurs + + Example: + >>> from translatepy import Translator + >>> t = Translator() + >>> result = t.text_to_speech("Hello, how are you?", "English") + >>> if result is not None: + ... with open("output.mp3", "wb") as output: # open a binary (b) file to write (w) + ... output.write(result) + ... print("Output of Text to Speech is available in output.mp3!") + ... else: + ... print("Couldn't get text to speech result...") + + # the result is an MP3 file with the text to speech output + """ + + if str(text).replace(" ", "").replace("\n", "") == "": + return None + + ## language handling + if source_language is not None and not isinstance(source_language, Language): + source_language = Language(source_language) + + return self.google_translate.text_to_speech(text, source_language) + def clean_cache(self) -> None: """ Cleans translatepy's global caches diff --git a/translatepy/translators/google.py b/translatepy/translators/google.py index a9e1614..47dcc01 100644 --- a/translatepy/translators/google.py +++ b/translatepy/translators/google.py @@ -186,9 +186,25 @@ def translate(self, text, destination_language, source_language="auto") -> Union try: return data['ld_result']["srclangs"][0], "".join((sentence["trans"] if "trans" in sentence else "") for sentence in data["sentences"]) except Exception: - return data[0][0][2], "".join(sentence for sentence in data[0][0][0][0]) - else: - return None, None + try: + return data[0][0][2], "".join(sentence for sentence in data[0][0][0][0]) + except Exception: + pass + + request = get("https://translate.googleapis.com/translate_a/single?dt=t&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&client=gtx&q=" + text + "&hl=" + str(destination_language) + "&sl=" + str(source_language) + "&tl=" + str(destination_language) + "&dj=1&source=bubble") + if request.status_code < 400: + data = loads(request.text) + src = data.get("src", None) + if src is None: + src = data.get("ld_result", {}).get("srclangs", [None])[0] + if src is None: + src = data.get("ld_result", {}).get("extended_srclangs", [None])[0] + return src, " ".join([sentence["trans"] for sentence in data["sentences"] if "trans" in sentence]) + request = get("https://translate.googleapis.com/translate_a/single?client=gtx&dt=t&dt=bd&dj=1&source=input&q=" + text + "&sl=" + str(source_language) + "&tl=" + str(destination_language)) + if request.status_code < 400: + data = loads(request.text) + return data["src"], " ".join([sentence["trans"] for sentence in data["sentences"] if "trans" in sentence]) + return None, None except Exception: return None, None @@ -231,7 +247,18 @@ def transliterate(self, text, source_language=None) -> Union[Tuple[str, str], Tu origin_pronunciation = parsed[0][0] except Exception: pass - return source_language, origin_pronunciation + if origin_pronunciation is not None: + return source_language, origin_pronunciation + request = get("https://translate.googleapis.com/translate_a/single?dt=t&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&client=gtx&q=" + text + "&hl=" + str(destination_language) + "&sl=" + str(source_language) + "&tl=" + str(destination_language) + "&dj=1&source=bubble") + if request.status_code < 400: + data = loads(request.text) + src = data.get("src", None) + if src is None: + src = data.get("ld_result", {}).get("srclangs", [None])[0] + if src is None: + src = data.get("ld_result", {}).get("extended_srclangs", [None])[0] + return src, " ".join([sentence["src_translit"] for sentence in data["sentences"] if "src_translit" in sentence]) + return None, None except Exception: return None, None @@ -239,7 +266,7 @@ def define(self): """Returns the definition of the given word""" raise NotImplementedError - def text_to_speech(self, text, source_language=None, speed=1): + def text_to_speech(self, text, source_language=None): """ Gives back the text to speech result for the given text @@ -249,23 +276,25 @@ def text_to_speech(self, text, source_language=None, speed=1): Returns: bytes --> the mp3 file as bytes None --> when an error occurs - - !! Currently doesn't seem to work well because of the Token Generation methods. - > Please refer to #234@ssut/py-googletrans if you have any problem """ try: + text = quote(str(text), safe='') + if source_language is None: + source_language = self.language(text) + if source_language is None: + return None + request = get("https://translate.googleapis.com/translate_tts?client=gtx&ie=UTF-8&tl=" + str(source_language) + "&q=" + text) + if request.status_code == 200: + return request.content + request = get("https://translate.google.com/translate_tts?client=tw-ob&q=" + text + "&tl=" + str(source_language)) + if request.status_code == 200: + return request.content if self.token_acquirer is None: return None - text = str(text) textlen = len(text) token = self.token_acquirer.do(text) if token is None: return None - if source_language is None: - source_language = self.language(text) - if source_language is None: - return None - text = quote(str(text), safe='') request = get("https://translate.google.com/translate_tts?ie=UTF-8&q=" + text + "&tl=" + source_language + "&total=1&idx=0&textlen=" + textlen + "&tk=" + str(token) + "&client=webapp&prev=input&ttsspeed=" + str(convert_to_float(speed))) if request.status_code < 400: return request.content diff --git a/translatepy/translators/yandex.py b/translatepy/translators/yandex.py index 67debd8..4f9a5f0 100644 --- a/translatepy/translators/yandex.py +++ b/translatepy/translators/yandex.py @@ -27,6 +27,7 @@ } TRANSLIT_LANGS = ['am', 'bn', 'el', 'gu', 'he', 'hi', 'hy', 'ja', 'ka', 'kn', 'ko', 'ml', 'mr', 'ne', 'pa', 'si', 'ta', 'te', 'th', 'yi', 'zh'] +YT_KEYS = "trnsl.1.1.20181102T213252Z.15973c8fd1497069.dfef0ce2d1d66c4b3a560986cfd349cc27adceef,trnsl.1.1.20181102T213332Z.79148d90f1c6e2d5.c05d93cb4000e5eb194a8cb0302a2577e1786456,trnsl.1.1.20181102T213412Z.b7d99cd224b50875.78b25ec3b559d218c468a15718d62aa9160a6775,trnsl.1.1.20181102T213431Z.541628d09094c1a3.ff27af10a741cd223c176acde97e02d088e5f924,trnsl.1.1.20181102T213450Z.93ccf977a373c675.e773350d58a6b56434efb4e1192683e45462d7e9,trnsl.1.1.20181102T213509Z.f880b66413c0aaf3.9571c4386c6aeb148626ba31ec284691dec1ccaf,trnsl.1.1.20181102T213527Z.eb68115e91aab47f.4da25db7117bff3b15b1dc06fababa6a3c3a8535,trnsl.1.1.20181102T213549Z.ae5a65262a8dcd37.6d7ca0bc28077563a22044c73982101d802110ce,trnsl.1.1.20181102T213613Z.0bad11f72f75fcfa.5a93be6a7aa651b1ef3a36f1fe4ca9af3ac7e32b,trnsl.1.1.20181102T213633Z.a390634b03f595d9.451e188304339141a5c73bcd8b5c25bc0afa4dd9,trnsl.1.1.20181102T213652Z.d9d75034bf77120a.1737bc1c6984c39aeccf2e3581077be809b09b45,trnsl.1.1.20181102T213710Z.8b323dc6d80bba83.f83adcebeaca98ce4445a3d0d328acb59fb577a4,trnsl.1.1.20181102T213729Z.d50920bce790c915.e87f433ef69c108970909acd514734b31556ca4b,trnsl.1.1.20181102T213749Z.3e2b20c226adc8f7.fa0e8f8179d9824864262c0df8d98a222bf06e95,trnsl.1.1.20181102T213808Z.9c3b0910f60f8844.d7a5174868016700c629708e491842a4ff7dfff4,trnsl.1.1.20181102T213936Z.7c005f281fd3959e.a88a3f0411a0b373f941de434e960ec512f1892b,trnsl.1.1.20181102T214014Z.c351b40bd641f99c.114eb8303466d0add7fbca0f7a661d75def7f4c9,trnsl.1.1.20181102T214042Z.77ed3fa8560a999d.9001ccdb59651617814c0720a35dfc1c4ca32bc1,trnsl.1.1.20181102T214151Z.8c6ed1edcf6b527c.7a505097fe32ea5711ff27d44fadd1f84d64e87f,trnsl.1.1.20181102T204954Z.06a524538afb5370.d7c3461460c2e788cb6f67da941b076d65ee49f4,trnsl.1.1.20181102T205629Z.8c5a5671b2c94734.1cebf2b46d03aa6f21a3aada2c6f0dea72b2bb7c,trnsl.1.1.20181102T205740Z.53924bf8bf038b66.1497238b25def89dc7ef38dc919556eb18419aee,trnsl.1.1.20181102T205833Z.6fa2c1193d34ae03.095847fc36981d0abbc9f2d08ff4f2209ce4cbc9,trnsl.1.1.20181102T205859Z.f48f25f673c18de8.2662ca40ff4d9276e19d1a751353976374eb5027,trnsl.1.1.20181102T205922Z.8fcd584cb97e7b7b.96635d8adeb31ac33d8af5f1b84b94bca7785a1b,trnsl.1.1.20181102T205943Z.c107053b80b3da23.33f28db3a836c230ab1fb2ec519c94e6b07f9375,trnsl.1.1.20181102T210007Z.3aba0562159ceb75.5ff0ac290dbd2d01a62023b130581f594c65bd62,trnsl.1.1.20181102T210030Z.48694ecb9d7aef4e.39aa18ca356b09014ce79c7b8cda4f56e7646f58,trnsl.1.1.20181102T210101Z.8ca38ca32d1eeae2.9cf56256c908fd101a9e0bceccaf2ffd729099c4,trnsl.1.1.20181102T210122Z.14226828ff16677d.e64bf54ba3da5fa26a43d522a979e11760cb878a,trnsl.1.1.20181102T210145Z.3ff15c7295b2dec4.252e06955b1265504be710c4871b1b829166f7e9,trnsl.1.1.20181102T210207Z.9c8d671f4e895030.90514dfd6b7cc782e3ff2bcbd046835a661106d1,trnsl.1.1.20181102T210233Z.acd76b1b0033dd87.f0d1034c8b9a9ebd5abd89a0beee582b34a3ee7a,trnsl.1.1.20181102T210309Z.084714f2e6d4c8d6.2113ff52f70e8edb8d15e5dc6b5edc04882d4847,trnsl.1.1.20181102T214252Z.c7d523a692f21cf9.f061c197cf868b9bb22fa1000ed73a131a87a241,trnsl.1.1.20181102T214324Z.68589c5b7b1beaca.4887133744773fe4890cd25061e8619d5817e545,trnsl.1.1.20181102T214351Z.a5f4ec70259dfcdd.e657e0c9a59f33274144633d7cf42475077afb67,trnsl.1.1.20181102T214416Z.cc1f850655586c0f.b6d2866d529d2e001a0318180bc7e9e715f0a5b5,trnsl.1.1.20181102T214444Z.24553e66aa23b466.be6804cb85f09c6f64c5c5f3a17720cf47dc9e86,trnsl.1.1.20181102T214947Z.dfd66bd7b21dd3af.275de56c2ea7a5109ddd1fbc43406a9f485cca65,trnsl.1.1.20181102T215052Z.ec57c48f3cb24691.54a8091b9c8f364af1a9277a7aeb642356476d87,trnsl.1.1.20181102T215122Z.af8710eead58551a.97f86308053dccb2d148577e2c023c0a13489b54,trnsl.1.1.20181102T215154Z.ae797e662ffc0055.af3030fcbd863a5cc71b24afd8a3122b5a6bc2b2,trnsl.1.1.20181102T215230Z.23c8ec80d3d564ee.0285f07c82e3d91e03c274b72d889701a7de7485,trnsl.1.1.20181102T215258Z.3d6142281267b1ee.ef871a7bae00682a9225f34f40c2084aa0cd1f51,trnsl.1.1.20181102T215327Z.b0324d3620775026.f61c65f8c6c31b2f45973397e86eeb4af8ef7bc9,trnsl.1.1.20181102T215356Z.e72ddaaa2e5d3029.fc6d21d8bf367760164caf2073be332b87e558c5,trnsl.1.1.20181102T215421Z.e7bb329825ab40c2.6b4368f077ab1f36aca314f1f5d3855de9b7ffb4,trnsl.1.1.20181102T215447Z.3d4324e2958136ff.8d6085bf3873f653c02d07433ad2f336de64c23f,trnsl.1.1.20181102T215517Z.c17384773f356575.be5756d5d3ede5b0823a865aa8b2a401d5b1cf8d,trnsl.1.1.20181102T215548Z.89db0ce5c7b3bef8.c8407a45bc8655af5ab12da309f504475756371b,trnsl.1.1.20181102T215618Z.c22f22948a0fdbd1.876a5521c7737b41b6a43ca9af0b66e3f8166ab6,trnsl.1.1.20181102T215641Z.fe52de1dc3618d73.740023052cbd6a0bc98bd5bf5ff05a55770350be,trnsl.1.1.20181102T215711Z.9c77c09515106e89.72841b56546a7f19ed8f77b27b91a6dee93a59b1,trnsl.1.1.20181102T215736Z.162caa5087e102a8.6f8bbfcf5ef76652dc6c1b3249c35fc7cd944d19,trnsl.1.1.20181102T215811Z.6e967911b314d9f2.040c8dc577ac16ddea33e33225a2b334b8fd0be3,trnsl.1.1.20181102T215859Z.467b4f132813ab8a.5c04bd040c0ddbccdb9fd1be799384e6826a5635,trnsl.1.1.20181102T220047Z.ad0e4a72ac465775.9f9a21610f30534627db70c35c2c1e453cbc7c36,trnsl.1.1.20181102T220117Z.09ce6c0292c9761c.8cc1dfc8f30b6c3bf30c8e356088ef2685f37d86".split(',') class YandexTranslate(): """A Python implementation of Yandex Translation's APIs""" @@ -124,7 +125,7 @@ def translate(self, text, destination_language, source_language="auto") -> Union def _request(): """ """ - url = self._base_url + "translate?id=" + self._sid + "-0-0&srv=tr-text&lang=" + str(source_language) +"-" + str(destination_language) + "&reason=auto&format=text" + url = self._base_url + "translate?id=" + self._sid + "-0-0&srv=tr-text&lang=" + str(source_language) + "-" + str(destination_language) + "&reason=auto&format=text" request = get(url, headers=self._headers, data={'text': str(text), 'options': '4'}) data = loads(request.text) if request.status_code < 400 and data["code"] == 200: @@ -133,7 +134,7 @@ def _request(): return None, None _lang, _text = _request() - if _lang is None or _text is None: + if _lang is None and _text is None: if self.refreshSID(): _lang, _text = _request() return _lang, _text