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.compat import PY3
|
||||
from googletrans.compat import unicode
|
||||
from googletrans.gtoken import TokenAcquirer
|
||||
from googletrans.constants import DEFAULT_USER_AGENT, LANGUAGES, SPECIAL_CASES
|
||||
from googletrans.models import Translated, Detected
|
||||
@ -27,7 +26,7 @@ class Translator(object):
|
||||
self.token_acquirer = TokenAcquirer(session=self.session)
|
||||
|
||||
# Use HTTP2 Adapter if hyper is installed
|
||||
try:
|
||||
try: # pragma: nocover
|
||||
from hyper.contrib import HTTP20Adapter
|
||||
self.session.mount(urls.BASE, HTTP20Adapter())
|
||||
except ImportError: # pragma: nocover
|
||||
|
@ -1,8 +1,11 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import sys
|
||||
try: # pragma: nocover
|
||||
from urllib.parse import quote
|
||||
except: # pragma: nocover
|
||||
from urllib import quote
|
||||
|
||||
|
||||
PY3 = sys.version_info > (3, )
|
||||
|
||||
if PY3:
|
||||
unicode = str
|
||||
unicode = str if PY3 else unicode
|
||||
|
@ -2,7 +2,6 @@
|
||||
import ast
|
||||
import math
|
||||
import re
|
||||
import sys
|
||||
import time
|
||||
|
||||
import requests
|
||||
@ -42,7 +41,6 @@ class TokenAcquirer(object):
|
||||
|
||||
RE_TKK = re.compile(r'TKK=eval\(\'\(\(function\(\)\{(.+?)\}\)\(\)\)\'\);', re.DOTALL)
|
||||
|
||||
|
||||
def __init__(self, tkk='0', session=None):
|
||||
self.session = session or requests.Session()
|
||||
self.tkk = tkk
|
||||
@ -57,7 +55,7 @@ class TokenAcquirer(object):
|
||||
|
||||
r = self.session.get('https://translate.google.com')
|
||||
# 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(=)
|
||||
if PY3: # pragma: no cover
|
||||
code = code.encode().decode('unicode-escape')
|
||||
@ -77,7 +75,7 @@ class TokenAcquirer(object):
|
||||
keys[name] = node.value.n
|
||||
# the value can sometimes be negative
|
||||
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
|
||||
elif isinstance(node, ast.Return):
|
||||
# parameters should be set after this point
|
||||
@ -174,7 +172,6 @@ class TokenAcquirer(object):
|
||||
|
||||
return '{}.{}'.format(a, a ^ b)
|
||||
|
||||
|
||||
def do(self, text):
|
||||
self._update()
|
||||
tk = self.acquire(text)
|
||||
|
Loading…
Reference in New Issue
Block a user