Skip to content

Commit

Permalink
added timeout option
Browse files Browse the repository at this point in the history
  • Loading branch information
wizyoung committed Dec 16, 2018
1 parent 6ff49c5 commit d87736a
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/googletrans/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Free Google Translate API for Python. Translates totally free of charge."""
__all__ = 'Translator',
__version__ = '2.2.0'
__version__ = '2.3.0'


from googletrans.client import Translator
Expand Down
Binary file removed src/googletrans/__init__.pyc
Binary file not shown.
16 changes: 16 additions & 0 deletions src/googletrans/adapters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from requests.adapters import HTTPAdapter


class TimeoutAdapter(HTTPAdapter):
"""HTTP adapter that adds timeout to each query."""
def __init__(self, timeout=None, *args, **kwargs):
"""HTTP adapter that adds timeout to each query.
:param timeout: Timeout that will be added to each query
"""
self.timeout = timeout
super(TimeoutAdapter, self).__init__(*args, **kwargs)

def send(self, *args, **kwargs):
kwargs['timeout'] = self.timeout
return super(TimeoutAdapter, self).send(*args, **kwargs)
46 changes: 44 additions & 2 deletions src/googletrans/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import random

from googletrans import urls, utils
from googletrans.adapters import TimeoutAdapter
from googletrans.compat import PY3
from googletrans.gtoken import TokenAcquirer
from googletrans.constants import DEFAULT_USER_AGENT, LANGCODES, LANGUAGES, SPECIAL_CASES
Expand All @@ -28,13 +29,30 @@ class Translator(object):
:param user_agent: the User-Agent header to send when making requests.
:type user_agent: :class:`str`
:param proxies: proxies configuration.
Dictionary mapping protocol or protocol and host to the URL of the proxy
For example ``{'http': 'foo.bar:3128', 'http://host.name': 'foo.bar:4012'}``
:type proxies: dictionary
:param timeout: Definition of timeout for Requests library.
Will be used by every request.
:type timeout: number or a double of numbers
"""

def __init__(self, service_urls=None, user_agent=DEFAULT_USER_AGENT):
def __init__(self, service_urls=None, user_agent=DEFAULT_USER_AGENT,
proxies=None, timeout=None):

self.session = requests.Session()
if proxies is not None:
self.session.proxies = proxies
self.session.headers.update({
'User-Agent': user_agent,
})
if timeout is not None:
self.session.mount('https://', TimeoutAdapter(timeout))
self.session.mount('http://', TimeoutAdapter(timeout))

self.service_urls = service_urls or ['translate.google.com']
self.token_acquirer = TokenAcquirer(session=self.session, host=self.service_urls[0])

Expand Down Expand Up @@ -63,6 +81,28 @@ def _translate(self, text, dest, src):
data = utils.format_json(r.text)
return data

def _parse_extra_data(self, data):
response_parts_name_mapping = {
0: 'translation',
1: 'all-translations',
2: 'original-language',
5: 'possible-translations',
6: 'confidence',
7: 'possible-mistakes',
8: 'language',
11: 'synonyms',
12: 'definitions',
13: 'examples',
14: 'see-also',
}

extra = {}

for index, category in response_parts_name_mapping.items():
extra[category] = data[index] if (index < len(data) and data[index]) else None

return extra

def translate(self, text, dest='en', src='auto'):
"""Translate text from source language to destination language
Expand Down Expand Up @@ -134,6 +174,8 @@ def translate(self, text, dest='en', src='auto'):
# this code will be updated when the format is changed.
translated = ''.join([d[0] if d[0] else '' for d in data[0]])

extra_data = self._parse_extra_data(data)

# actual source language that will be recognized by Google Translator when the
# src passed is equal to auto.
try:
Expand Down Expand Up @@ -162,7 +204,7 @@ def translate(self, text, dest='en', src='auto'):

# put final values into a new Translated object
result = Translated(src=src, dest=dest, origin=origin,
text=translated, pronunciation=pron)
text=translated, pronunciation=pron, extra_data=extra_data)

return result

Expand Down
Binary file removed src/googletrans/client.pyc
Binary file not shown.
Binary file removed src/googletrans/compat.pyc
Binary file not shown.
4 changes: 3 additions & 1 deletion src/googletrans/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@
'xh': 'xhosa',
'yi': 'yiddish',
'yo': 'yoruba',
'zu': 'zulu'
'zu': 'zulu',
'fil': 'Filipino',
'he': 'Hebrew'
}

LANGCODES = dict(map(reversed, LANGUAGES.items()))
Binary file removed src/googletrans/constants.pyc
Binary file not shown.
10 changes: 5 additions & 5 deletions src/googletrans/gtoken.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ def _update(self):

r = self.session.get(self.host)

rawtkk = self.RE_RAWTKK.search(r.text)
if rawtkk:
self.tkk = rawtkk.group(1)
raw_tkk = self.RE_TKK.search(r.text)
if raw_tkk:
self.tkk = raw_tkk.group(1)
return

# this will be the same as python code after stripping out a reserved word 'var'
code = unicode(self.RE_TKK.search(r.text).group(1)).replace('var ', '')
# unescape special ascii characters such like a \x3d(=)
Expand Down Expand Up @@ -185,4 +185,4 @@ def acquire(self, text):
def do(self, text):
self._update()
tk = self.acquire(text)
return tk
return tk
Binary file removed src/googletrans/gtoken.pyc
Binary file not shown.
9 changes: 6 additions & 3 deletions src/googletrans/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ class Translated(object):
:param text: translated text
:param pronunciation: pronunciation
"""
def __init__(self, src, dest, origin, text, pronunciation):
def __init__(self, src, dest, origin, text, pronunciation, extra_data=None):
self.src = src
self.dest = dest
self.origin = origin
self.text = text
self.pronunciation = pronunciation
self.extra_data = extra_data

def __str__(self): # pragma: nocover
return self.__unicode__()

def __unicode__(self): # pragma: nocover
return u'Translated(src={src}, dest={dest}, text={text}, pronunciation={pronunciation})'.format(
src=self.src, dest=self.dest, text=self.text, pronunciation=self.pronunciation)
return u'Translated(src={src}, dest={dest}, text={text}, pronunciation={pronunciation}, ' \
u'extra_data={extra_data})'.format(
src=self.src, dest=self.dest, text=self.text, pronunciation=self.pronunciation,
extra_data='"' + repr(self.extra_data)[:10] + '..."')


class Detected(object):
Expand Down
Binary file removed src/googletrans/models.pyc
Binary file not shown.
Binary file removed src/googletrans/urls.pyc
Binary file not shown.
1 change: 1 addition & 0 deletions src/googletrans/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def format_json(original):
converted = json.loads(original)
except ValueError:
converted = legacy_format_json(original)

return converted


Expand Down
Binary file removed src/googletrans/utils.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion src/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1
2.1.1

0 comments on commit d87736a

Please sign in to comment.