Better compatiblity with all python versions
This commit is contained in:
parent
deb36364f7
commit
5f908fab16
@ -8,7 +8,6 @@ import requests
|
|||||||
|
|
||||||
from googletrans import urls, utils
|
from googletrans import urls, utils
|
||||||
from googletrans.compat import PY3
|
from googletrans.compat import PY3
|
||||||
from googletrans.compat import unicode
|
|
||||||
from googletrans.gtoken import TokenAcquirer
|
from googletrans.gtoken import TokenAcquirer
|
||||||
from googletrans.constants import DEFAULT_USER_AGENT, LANGUAGES, SPECIAL_CASES
|
from googletrans.constants import DEFAULT_USER_AGENT, LANGUAGES, SPECIAL_CASES
|
||||||
from googletrans.models import Translated, Detected
|
from googletrans.models import Translated, Detected
|
||||||
@ -27,7 +26,7 @@ class Translator(object):
|
|||||||
self.token_acquirer = TokenAcquirer(session=self.session)
|
self.token_acquirer = TokenAcquirer(session=self.session)
|
||||||
|
|
||||||
# Use HTTP2 Adapter if hyper is installed
|
# Use HTTP2 Adapter if hyper is installed
|
||||||
try:
|
try: # pragma: nocover
|
||||||
from hyper.contrib import HTTP20Adapter
|
from hyper.contrib import HTTP20Adapter
|
||||||
self.session.mount(urls.BASE, HTTP20Adapter())
|
self.session.mount(urls.BASE, HTTP20Adapter())
|
||||||
except ImportError: # pragma: nocover
|
except ImportError: # pragma: nocover
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import sys
|
import sys
|
||||||
|
try: # pragma: nocover
|
||||||
|
from urllib.parse import quote
|
||||||
|
except: # pragma: nocover
|
||||||
|
from urllib import quote
|
||||||
|
|
||||||
|
|
||||||
PY3 = sys.version_info > (3, )
|
PY3 = sys.version_info > (3, )
|
||||||
|
|
||||||
if PY3:
|
unicode = str if PY3 else unicode
|
||||||
unicode = str
|
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
import ast
|
import ast
|
||||||
import math
|
import math
|
||||||
import re
|
import re
|
||||||
import sys
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
@ -42,7 +41,6 @@ class TokenAcquirer(object):
|
|||||||
|
|
||||||
RE_TKK = re.compile(r'TKK=eval\(\'\(\(function\(\)\{(.+?)\}\)\(\)\)\'\);', re.DOTALL)
|
RE_TKK = re.compile(r'TKK=eval\(\'\(\(function\(\)\{(.+?)\}\)\(\)\)\'\);', re.DOTALL)
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, tkk='0', session=None):
|
def __init__(self, tkk='0', session=None):
|
||||||
self.session = session or requests.Session()
|
self.session = session or requests.Session()
|
||||||
self.tkk = tkk
|
self.tkk = tkk
|
||||||
@ -57,7 +55,7 @@ class TokenAcquirer(object):
|
|||||||
|
|
||||||
r = self.session.get('https://translate.google.com')
|
r = self.session.get('https://translate.google.com')
|
||||||
# this will be the same as python code after stripping out a reserved word 'var'
|
# this will be the same as python code after stripping out a reserved word 'var'
|
||||||
code = unicode(self.RE_TKK.search(r.text)[1]).replace('var ', '')
|
code = unicode(self.RE_TKK.search(r.text).group(1)).replace('var ', '')
|
||||||
# unescape special ascii characters such like a \x3d(=)
|
# unescape special ascii characters such like a \x3d(=)
|
||||||
if PY3: # pragma: no cover
|
if PY3: # pragma: no cover
|
||||||
code = code.encode().decode('unicode-escape')
|
code = code.encode().decode('unicode-escape')
|
||||||
@ -77,7 +75,7 @@ class TokenAcquirer(object):
|
|||||||
keys[name] = node.value.n
|
keys[name] = node.value.n
|
||||||
# the value can sometimes be negative
|
# the value can sometimes be negative
|
||||||
elif isinstance(node.value, ast.UnaryOp) and \
|
elif isinstance(node.value, ast.UnaryOp) and \
|
||||||
isinstance(node.value.op, ast.USub):
|
isinstance(node.value.op, ast.USub): # pragma: nocover
|
||||||
keys[name] = -node.value.operand.n
|
keys[name] = -node.value.operand.n
|
||||||
elif isinstance(node, ast.Return):
|
elif isinstance(node, ast.Return):
|
||||||
# parameters should be set after this point
|
# parameters should be set after this point
|
||||||
@ -174,7 +172,6 @@ class TokenAcquirer(object):
|
|||||||
|
|
||||||
return '{}.{}'.format(a, a ^ b)
|
return '{}.{}'.format(a, a ^ b)
|
||||||
|
|
||||||
|
|
||||||
def do(self, text):
|
def do(self, text):
|
||||||
self._update()
|
self._update()
|
||||||
tk = self.acquire(text)
|
tk = self.acquire(text)
|
||||||
|
Loading…
Reference in New Issue
Block a user