Skip to content

Commit

Permalink
Merge 7650a90 into 9ff4aae
Browse files Browse the repository at this point in the history
  • Loading branch information
ssut committed Sep 4, 2020
2 parents 9ff4aae + 7650a90 commit 4e179c1
Show file tree
Hide file tree
Showing 10 changed files with 308 additions and 149 deletions.
1 change: 1 addition & 0 deletions .travis.yml
@@ -1,4 +1,5 @@
language: python
dist: bionic
python:
- 3.6
- 3.7
Expand Down
4 changes: 2 additions & 2 deletions Pipfile
Expand Up @@ -4,13 +4,13 @@ verify_ssl = true
name = "pypi"

[packages]
httpx = "==0.13.3"
httpx = "==0.14.1"

[dev-packages]
coveralls = "*"
"pytest-watch" = "*"
"pytest-testmon" = "*"
sphinx = "*"

[requires]

python_version = ">=3.6"
359 changes: 243 additions & 116 deletions Pipfile.lock

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion README.rst
Expand Up @@ -37,6 +37,14 @@ HTTP/2 support

This library uses httpx for HTTP requests so HTTP/2 is supported by default.

You can check if http2 is enabled and working by the `._response.http_version` of `Translated` or `Detected` object:

.. code:: python
>>> translator.translate('테스트')._response.http_version
# 'HTTP/2'
How does this library work
~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -194,7 +202,7 @@ changes at any time.
Contributing
-------------------------

Contributions are more than welcomed. See
Contributions are more than welcomed. See
`CONTRIBUTING.md <CONTRIBUTING.md>`__

-----------------------------------------
Expand Down
37 changes: 22 additions & 15 deletions googletrans/client.py
Expand Up @@ -42,7 +42,6 @@ class Translator:
:param timeout: Definition of timeout for httpx library.
Will be used for every request.
:type timeout: number or a double of numbers
||||||| constructed merge base
: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'}``
Expand All @@ -52,9 +51,11 @@ class Translator:

def __init__(self, service_urls=None, user_agent=DEFAULT_USER_AGENT,
raise_exception=DEFAULT_RAISE_EXCEPTION,
proxies: typing.Dict[str, httpcore.SyncHTTPTransport] = None, timeout: Timeout = None):
proxies: typing.Dict[str, httpcore.SyncHTTPTransport] = None,
timeout: Timeout = None,
http2=True):

self.client = httpx.Client()
self.client = httpx.Client(http2=http2)
if proxies is not None: # pragma: nocover
self.client.proxies = proxies

Expand All @@ -66,7 +67,8 @@ def __init__(self, service_urls=None, user_agent=DEFAULT_USER_AGENT,
self.client.timeout = timeout

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

def _pick_service_url(self):
Expand All @@ -84,12 +86,14 @@ def _translate(self, text, dest, src, override):

if r.status_code == 200:
data = utils.format_json(r.text)
return data
else:
if self.raise_exception:
raise Exception('Unexpected status code "{}" from {}'.format(r.status_code, self.service_urls))
DUMMY_DATA[0][0][0] = text
return DUMMY_DATA
return data, r

if self.raise_exception:
raise Exception('Unexpected status code "{}" from {}'.format(
r.status_code, self.service_urls))

DUMMY_DATA[0][0][0] = text
return DUMMY_DATA, r

def _parse_extra_data(self, data):
response_parts_name_mapping = {
Expand All @@ -109,7 +113,8 @@ def _parse_extra_data(self, data):
extra = {}

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

return extra

Expand Down Expand Up @@ -179,7 +184,7 @@ def translate(self, text, dest='en', src='auto', **kwargs):
return result

origin = text
data = self._translate(text, dest, src, kwargs)
data, response = self._translate(text, dest, src, kwargs)

# this code will be updated when the format is changed.
translated = ''.join([d[0] if d[0] else '' for d in data[0]])
Expand Down Expand Up @@ -210,7 +215,9 @@ def translate(self, text, dest='en', src='auto', **kwargs):

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

return result

Expand Down Expand Up @@ -252,7 +259,7 @@ def detect(self, text, **kwargs):
result.append(lang)
return result

data = self._translate(text, 'en', 'auto', kwargs)
data, response = self._translate(text, 'en', 'auto', kwargs)

# actual source language that will be recognized by Google Translator when the
# src passed is equal to auto.
Expand All @@ -267,6 +274,6 @@ def detect(self, text, **kwargs):
confidence = data[8][-2][0]
except Exception: # pragma: nocover
pass
result = Detected(lang=src, confidence=confidence)
result = Detected(lang=src, confidence=confidence, response=response)

return result
4 changes: 2 additions & 2 deletions googletrans/gtoken.py
Expand Up @@ -38,8 +38,8 @@ class TokenAcquirer:
RE_TKK = re.compile(r'tkk:\'(.+?)\'', re.DOTALL)
RE_RAWTKK = re.compile(r'tkk:\'(.+?)\'', re.DOTALL)

def __init__(self, tkk='0', client: httpx.Client = None, host='translate.google.com'):
self.client = client or httpx.Client()
def __init__(self, client: httpx.Client, tkk='0', host='translate.google.com'):
self.client = client
self.tkk = tkk
self.host = host if 'http' in host else 'https://' + host

Expand Down
21 changes: 17 additions & 4 deletions googletrans/models.py
@@ -1,4 +1,12 @@
class Translated:
from httpx import Response


class Base:
def __init__(self, response: Response = None):
self._response = response


class Translated(Base):
"""Translate result object
:param src: source langauge (default: auto)
Expand All @@ -7,7 +15,10 @@ class Translated:
:param text: translated text
:param pronunciation: pronunciation
"""
def __init__(self, src, dest, origin, text, pronunciation, extra_data=None):

def __init__(self, src, dest, origin, text, pronunciation, extra_data=None,
**kwargs):
super().__init__(**kwargs)
self.src = src
self.dest = dest
self.origin = origin
Expand All @@ -29,13 +40,15 @@ def __unicode__(self): # pragma: nocover
)


class Detected:
class Detected(Base):
"""Language detection result object
:param lang: detected language
:param confidence: the confidence of detection result (0.00 to 1.00)
"""
def __init__(self, lang, confidence):

def __init__(self, lang, confidence, **kwargs):
super().__init__(**kwargs)
self.lang = lang
self.confidence = confidence

Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
@@ -1,7 +1,7 @@
from pytest import fixture


@fixture
@fixture(scope='session')
def translator():
from googletrans import Translator
return Translator()
return Translator()
10 changes: 5 additions & 5 deletions tests/test_client.py
@@ -1,6 +1,6 @@
from httpcore import TimeoutException
from httpcore._exceptions import ConnectError
from httpx import Timeout, Client
from httpx import Timeout, Client, ConnectTimeout
from unittest.mock import patch
from pytest import raises

Expand Down Expand Up @@ -57,7 +57,8 @@ def test_language_name(translator):


def test_language_name_with_space(translator):
result = translator.translate(u'Hello', src='en', dest='chinese (simplified)')
result = translator.translate(
u'Hello', src='en', dest='chinese (simplified)')
assert result.dest == 'zh-cn'


Expand Down Expand Up @@ -85,7 +86,7 @@ def test_detect_language(translator):
ko = translator.detect(u'한국어')
en = translator.detect('English')
rubg = translator.detect('тест')

assert ko.lang == 'ko'
assert en.lang == 'en'
assert rubg.lang == ['ru', 'bg']
Expand Down Expand Up @@ -133,7 +134,7 @@ def test_dest_not_in_supported_languages(translator):

def test_timeout():
# httpx will raise ConnectError in some conditions
with raises((TimeoutException, ConnectError)):
with raises((TimeoutException, ConnectError, ConnectTimeout)):
translator = Translator(timeout=Timeout(0.0001))
translator.translate('안녕하세요.')

Expand All @@ -148,4 +149,3 @@ def __init__(self, status_code):
def test_403_error(session_mock):
translator = Translator()
assert translator.translate('test', dest='ko')

7 changes: 5 additions & 2 deletions tests/test_gtoken.py
@@ -1,11 +1,14 @@
# -*- coding: utf-8 -*-
import httpx

from googletrans import gtoken
from pytest import fixture


@fixture
@fixture(scope='session')
def acquirer():
return gtoken.TokenAcquirer()
client = httpx.Client(http2=True)
return gtoken.TokenAcquirer(client=client)


def test_acquire_token(acquirer):
Expand Down

0 comments on commit 4e179c1

Please sign in to comment.