Skip to content

Commit

Permalink
[add] Adding text_to_speech in Alpha [fix] Fixing #6 [add] Adding the…
Browse files Browse the repository at this point in the history
… "cache_clean" method to Translator()
  • Loading branch information
Animenosekai committed Mar 2, 2021
1 parent 0701c36 commit 3393e44
Show file tree
Hide file tree
Showing 8 changed files with 343 additions and 16 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Minimum required versions: 3.2
Incompatible versions: 2
```

According to Vermin, Python 3.2 is needed for the backport of typing but some may say that it is available for python versions higher than 3.0
According to Vermin (`--backport typing`), Python 3.2 is needed for the backport of typing but some may say that it is available for python versions higher than 3.0

Always check if your Python version works with `translatepy` before using it in production

Expand All @@ -47,7 +47,7 @@ You can check if you successfully installed it by printing out its version:
```bash
python -c "import translatepy; print(translatepy.__version__)"
# output:
translatepy v1.4
translatepy v1.5
```

## List of Services
Expand Down Expand Up @@ -127,6 +127,10 @@ translatepy.EXAMPLE_CACHES = {}
translatepy.DICTIONNARY_CACHES = {}
```

Or by calling the `Translator()` method "`clean_cache`"

***Warning: `translatepy`'s caches are global: they are used through all instances of `Translator()`***

### The Translator Class
It is the High API providing all of the methods and optimizations for `translatepy`
- translate: To translate things
Expand All @@ -141,6 +145,10 @@ When something goes wrong or nothing got found, `None` is returned.
The source language while being most of the time an instance of the Language class can sometimes be a string if the conversion to the Language class failed.


An additional `"text_to_speech"` function can be found in the GoogleTranslate class (accessible with the `Translator()` class at `Translator().google_translate`).
***It is not officialy supported and is not very stable.***


## Deployment

This module is currently in development and might contain bugs.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
setup(
name = "translatepy",
packages = ["translatepy"],
version = "1.4",
version = "1.5",
license = "GNU General Public License v3 (GPLv3)",
description = "Translate, transliterate, get the language of texts in no time with the help of multiple APIs!",
author = "Anime no Sekai",
Expand Down
2 changes: 1 addition & 1 deletion translatepy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
__copyright__ = 'Copyright 2021, translate'
__credits__ = ['animenosekai']
__license__ = 'GNU General Public License v3 (GPLv3)'
__version__ = 'translatepy v1.4'
__version__ = 'translatepy v1.5'
__maintainer__ = 'Anime no Sekai'
__email__ = '[email protected]'
__status__ = 'Stable'
21 changes: 21 additions & 0 deletions translatepy/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,25 @@ 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 clean_cache(self) -> None:
"""
Cleans translatepy's global caches
Returns:
None
"""
global TRANSLATION_CACHES
global TRANSLITERATION_CACHES
global SPELLCHECK_CACHES
global LANGUAGE_CACHES
global EXAMPLE_CACHES
global DICTIONARY_CACHES

TRANSLATION_CACHES = {}
TRANSLITERATION_CACHES = {}
SPELLCHECK_CACHES = {}
LANGUAGE_CACHES = {}
EXAMPLE_CACHES = {}
DICTIONARY_CACHES = {}

#translator = Translator()
12 changes: 9 additions & 3 deletions translatepy/translators/bing.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def translate(self, text, destination_language, source_language="auto-detect") -
return None, None


def example(self, text, destination_language, source_language="auto-detect") -> Union[Tuple[str, List[Example]], Tuple[None, None]]:
def example(self, text, destination_language, source_language=None, translation=None) -> Union[Tuple[str, List[Example]], Tuple[None, None]]:
"""
Gives examples for the given text
Expand All @@ -101,9 +101,15 @@ def example(self, text, destination_language, source_language="auto-detect") ->
"""
try:
source_language, translation = self.translate(text, destination_language, source_language)
if translation is None:
return None, None
source_language, translation = self.translate(text, destination_language, source_language)
if translation is None or source_language is None:
return None, None
else:
if source_language is None:
source_language = self.language(text)
if source_language is None:
return None, None
request = post("https://www.bing.com/texamplev3", headers=HEADERS, params=PARAMS, data={'text': str(text).lower(), 'from': str(source_language), 'to': str(destination_language), 'translation': str(translation).lower()})
if request.status_code < 400:
return source_language, [Example(example) for example in loads(request.text)[0]["examples"]]
Expand Down
59 changes: 52 additions & 7 deletions translatepy/translators/google.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from typing import Union
from requests import get
from json import loads
from urllib.parse import quote
from traceback import print_exc

from translatepy.utils.gtoken import TokenAcquirer
from translatepy.utils.annotations import Tuple
from translatepy.utils.utils import convert_to_float

HEADERS = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36'
Expand All @@ -11,7 +15,10 @@
class GoogleTranslate():
"""A Python implementation of Google Translate's APIs"""
def __init__(self) -> None:
pass
try:
self.token_acquirer = TokenAcquirer()
except:
self.token_acquirer = None

def translate(self, text, destination_language, source_language="auto") -> Union[Tuple[str, str], Tuple[None, None]]:
"""
Expand All @@ -28,14 +35,15 @@ def translate(self, text, destination_language, source_language="auto") -> Union
"""
try:
text = quote(str(text), safe='')
if source_language is None:
source_language = "auto"
request = get("https://translate.googleapis.com/translate_a/single?client=gtx&dt=t&sl=" + str(source_language) + "&tl=" + str(destination_language) + "&q=" + str(text))
request = get("https://translate.googleapis.com/translate_a/single?client=gtx&dt=t&sl=" + str(source_language) + "&tl=" + str(destination_language) + "&q=" + text)
if request.status_code < 400:
data = loads(request.text)
return data[2], "".join([sentence[0] for sentence in data[0]])
else:
request = get("https://clients5.google.com/translate_a/t?client=dict-chrome-ex&sl=" + str(source_language) + "&tl=" + str(destination_language) + "&q=" + str(text), headers=HEADERS)
request = get("https://clients5.google.com/translate_a/t?client=dict-chrome-ex&sl=" + str(source_language) + "&tl=" + str(destination_language) + "&q=" + text, headers=HEADERS)
if request.status_code < 400:
data = loads(request.text)
return data['ld_result']["srclangs"][0], "".join(sentence["trans"] for sentence in data["sentences"])
Expand All @@ -44,14 +52,50 @@ def translate(self, text, destination_language, source_language="auto") -> Union
except:
return None, None

def transliterate():
def transliterate(self):
"""Transliterates the given text"""
raise NotImplementedError

def define():
def define(self):
"""Returns the definition of the given word"""
raise NotImplementedError

def text_to_speech(self, text, source_language=None, speed=1):
"""
Gives back the text to speech result for the given text
Args:
text:
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:
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
else:
return None
except:
print_exc()
return None

def language(self, text) -> Union[str, None]:
"""
Gives back the language of the given text
Expand All @@ -65,11 +109,12 @@ def language(self, text) -> Union[str, None]:
"""
try:
request = get("https://translate.googleapis.com/translate_a/single?client=gtx&dt=t&sl=auto&tl=ja&q=" + str(text))
text = quote(str(text), safe='')
request = get("https://translate.googleapis.com/translate_a/single?client=gtx&dt=t&sl=auto&tl=ja&q=" + text)
if request.status_code < 400:
return loads(request.text)[2]
else:
request = get("https://clients5.google.com/translate_a/t?client=dict-chrome-ex&sl=auto&tl=ja&q=" + str(text), headers=HEADERS)
request = get("https://clients5.google.com/translate_a/t?client=dict-chrome-ex&sl=auto&tl=ja&q=" + text, headers=HEADERS)
if request.status_code < 400:
return loads(request.text)['ld_result']["srclangs"][0]
else:
Expand Down
7 changes: 5 additions & 2 deletions translatepy/translators/yandex.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from random import randint
from os.path import dirname, abspath
from typing import Union
from urllib.parse import quote

from safeIO import TextFile
from requests import get, post
Expand Down Expand Up @@ -73,7 +74,7 @@ def refreshSID(self) -> bool:
self._last_tried = time() # maybe keep that in a file
self._last_tried_cache.write(self._last_tried)
# else
# do nothing as we know that yandex will rate-limit us if we ping too much their website
# do nothing as we know that yandex will rate-limit us if we ping them too much
return False
except:
return False
Expand Down Expand Up @@ -238,7 +239,9 @@ def language(self, text, hint=None) -> Union[str, None]:
if self._sid.replace(" ", "") == "" and not self.refreshSID():
return None

url = self._base_url + "detect?sid=" + self._sid + "&srv=tr-text&text=" + str(text) + "&options=1&hint=" + str(hint)
text = quote(str(text), safe='')

url = self._base_url + "detect?sid=" + self._sid + "&srv=tr-text&text=" + text + "&options=1&hint=" + str(hint)

def _request():
""" """
Expand Down
Loading

0 comments on commit 3393e44

Please sign in to comment.