Skip to content

Commit

Permalink
[add] Adding multiple endpoints for Google [add] Officially adding te…
Browse files Browse the repository at this point in the history
…xt to speech
  • Loading branch information
Animenosekai committed Apr 25, 2021
1 parent 14a7345 commit 67b5a80
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 143 deletions.
2 changes: 2 additions & 0 deletions playground/translate
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion playground/tts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
https://translate.googleapis.com/translate_tts?client=gtx&ie=UTF-8&tl=<language>&q=<text>
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);
Expand All @@ -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=<language>&q=<text>
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"
227 changes: 101 additions & 126 deletions translatepy/translate.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
translatepy v1.5.2 (Stable)
translatepy v1.6 (Beta)
© Anime no Sekai — 2021
"""
Expand Down Expand Up @@ -82,54 +82,19 @@ 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
result = TranslationResult(source=text, result=response, source_language=lang, destination_language=destination_language, service=self.reverso_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.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]:
"""
Expand All @@ -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]:
"""
Expand All @@ -173,95 +137,61 @@ 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
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]:
"""
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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
Expand Down
Loading

0 comments on commit 67b5a80

Please sign in to comment.