Skip to content

Commit

Permalink
Merge 06c6628 into 2f83668
Browse files Browse the repository at this point in the history
  • Loading branch information
ssut committed Jun 14, 2020
2 parents 2f83668 + 06c6628 commit e93bf14
Show file tree
Hide file tree
Showing 16 changed files with 181 additions and 107 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -60,3 +60,7 @@ target/

.DS_Store
.python-version

# IDE
.idea
.vscode
1 change: 0 additions & 1 deletion .travis.yml
@@ -1,6 +1,5 @@
language: python
python:
- 3.5
- 3.6
- 3.7
- 3.8
Expand Down
13 changes: 1 addition & 12 deletions Pipfile
Expand Up @@ -4,20 +4,9 @@ verify_ssl = true
name = "pypi"

[packages]
requests = "==2.13.0"






httpx = "==0.13.3"

[dev-packages]

coveralls = "*"
"pytest-watch" = "*"
"pytest-testmon" = "*"


[requires]
python_version = "3.7"
98 changes: 86 additions & 12 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 3 additions & 9 deletions README.rst
Expand Up @@ -9,7 +9,7 @@ implemented Google Translate API. This uses the `Google Translate Ajax
API <https://translate.google.com>`__ to make calls to such methods as
detect and translate.

Compatible with Python 3.5+.
Compatible with Python 3.6+.

For details refer to the `API
Documentation <https://py-googletrans.readthedocs.io/en/latest>`__.
Expand All @@ -22,7 +22,6 @@ Features
- Auto language detection
- Bulk translations
- Customizable service URL
- Connection pooling (the advantage of using requests.Session)
- HTTP/2 support

TODO
Expand All @@ -36,11 +35,7 @@ more features are coming soon.
HTTP/2 support
~~~~~~~~~~~~~~

This is a great deal for everyone! (up to 2x times faster in my test) If
you want to get googletrans faster you should install
`hyper <https://github.com/Lukasa/hyper>`__ package. Googletrans will
automatically detect if hyper is installed and if so, it will be used
for http networking.
This library uses httpx for HTTP requests so HTTP/2 is supported by default.

How does this library work
~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -64,8 +59,7 @@ Installation

To install, either use things like pip with the package "googletrans"
or download the package and put the "googletrans" directory into your
python path. Anyway, it is noteworthy that, this just requires two
modules: requests and future.
python path.

.. code:: bash
Expand Down
2 changes: 1 addition & 1 deletion googletrans/__init__.py
@@ -1,6 +1,6 @@
"""Free Google Translate API for Python. Translates totally free of charge."""
__all__ = 'Translator',
__version__ = '2.4.1'
__version__ = '3.0.0'


from googletrans.client import Translator
Expand Down
16 changes: 0 additions & 16 deletions googletrans/adapters.py

This file was deleted.

63 changes: 38 additions & 25 deletions googletrans/client.py
Expand Up @@ -4,16 +4,21 @@
You can translate text using this module.
"""
import requests
import random
import typing

import httpcore
import httpx
from httpx import Timeout

from googletrans import urls, utils
from googletrans.adapters import TimeoutAdapter
from googletrans.gtoken import TokenAcquirer
from googletrans.constants import DEFAULT_USER_AGENT, LANGCODES, LANGUAGES, SPECIAL_CASES
from googletrans.constants import (
DEFAULT_USER_AGENT, LANGCODES, LANGUAGES, SPECIAL_CASES,
DEFAULT_RAISE_EXCEPTION, DUMMY_DATA
)
from googletrans.models import Translated, Detected


EXCLUDES = ('en', 'ca', 'fr')


Expand All @@ -34,33 +39,35 @@ class Translator:
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.
: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'}``
:param raise_exception: if `True` then raise exception if smth will go wrong
:type raise_exception: boolean
"""

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

self.session = requests.Session()
if proxies is not None:
self.session.proxies = proxies
self.session.headers.update({
self.client = httpx.Client()
if proxies is not None: # pragma: nocover
self.client.proxies = proxies

self.client.headers.update({
'User-Agent': user_agent,
})

if timeout is not None:
self.session.mount('https://', TimeoutAdapter(timeout))
self.session.mount('http://', TimeoutAdapter(timeout))
self.client.timeout = timeout

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

# Use HTTP2 Adapter if hyper is installed
try: # pragma: nocover
from hyper.contrib import HTTP20Adapter
self.session.mount(urls.BASE, HTTP20Adapter())
except ImportError: # pragma: nocover
pass
self.token_acquirer = TokenAcquirer(client=self.client, host=self.service_urls[0])
self.raise_exception = raise_exception

def _pick_service_url(self):
if len(self.service_urls) == 1:
Expand All @@ -73,10 +80,16 @@ def _translate(self, text, dest, src, override):
token=token, override=override)

url = urls.TRANSLATE.format(host=self._pick_service_url())
r = self.session.get(url, params=params)
r = self.client.get(url, params=params)

data = utils.format_json(r.text)
return data
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

def _parse_extra_data(self, data):
response_parts_name_mapping = {
Expand Down Expand Up @@ -189,7 +202,7 @@ def translate(self, text, dest='en', src='auto', **kwargs):
if pron is None:
try:
pron = data[0][1][2]
except: # pragma: nocover
except: # pragma: nocover
pass

if dest in EXCLUDES and pron == origin:
Expand Down
3 changes: 3 additions & 0 deletions googletrans/constants.py
Expand Up @@ -182,3 +182,6 @@
}

LANGCODES = dict(map(reversed, LANGUAGES.items()))
DEFAULT_RAISE_EXCEPTION = False
DUMMY_DATA = [[["", None, None, 0]], None, "en", None,
None, None, 1, None, [["en"], None, [1], ["en"]]]
8 changes: 4 additions & 4 deletions googletrans/gtoken.py
Expand Up @@ -4,7 +4,7 @@
import re
import time

import requests
import httpx

from googletrans.utils import rshift

Expand Down 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', session=None, host='translate.google.com'):
self.session = session or requests.Session()
def __init__(self, tkk='0', client: httpx.Client = None, host='translate.google.com'):
self.client = client or httpx.Client()
self.tkk = tkk
self.host = host if 'http' in host else 'https://' + host

Expand All @@ -51,7 +51,7 @@ def _update(self):
if self.tkk and int(self.tkk.split('.')[0]) == now:
return

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

raw_tkk = self.RE_TKK.search(r.text)
if raw_tkk:
Expand Down
2 changes: 1 addition & 1 deletion googletrans/utils.py
@@ -1,6 +1,6 @@
"""A conversion module for googletrans"""
import re
import json
import re


def build_params(query, src, dest, token, override):
Expand Down
6 changes: 1 addition & 5 deletions setup.py
Expand Up @@ -52,18 +52,14 @@ def install():
'Operating System :: MacOS :: MacOS X',
'Topic :: Education',
'Programming Language :: Python',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8'],
packages=find_packages(exclude=['docs', 'tests']),
keywords='google translate translator',
install_requires=[
'requests',
'httpx==0.13.3',
],
extras_require={
'h2': ['hyper'],
},
tests_require=[
'pytest',
'coveralls',
Expand Down

0 comments on commit e93bf14

Please sign in to comment.