This commit is contained in:
SuHun Han 2017-03-10 23:16:47 +09:00 committed by ssut
parent 791e32a23b
commit dda50305b7
2 changed files with 18 additions and 12 deletions

View File

@ -33,21 +33,24 @@ class Translator(object):
pass
def _translate(self, text, dest='en', src='auto'):
if src != 'auto' and src not in LANGUAGES.keys() and src in SPECIAL_CASES.keys():
src = SPECIAL_CASES[src]
elif src != 'auto' and src not in LANGUAGES.keys():
raise ValueError('invalid source language')
if src != 'auto':
if src not in LANGUAGES.keys() and src in SPECIAL_CASES.keys():
src = SPECIAL_CASES[src]
elif src not in LANGUAGES.keys():
raise ValueError('invalid source language')
if dest not in LANGUAGES.keys() and dest in SPECIAL_CASES.keys():
dest = SPECIAL_CASES[dest]
elif dest not in LANGUAGES.keys():
raise ValueError('invalid destination language')
if dest not in LANGUAGES.keys():
if dest in SPECIAL_CASES.keys():
dest = SPECIAL_CASES[dest]
else:
raise ValueError('invalid destination language')
if not PY3 and isinstance(text, str): # pragma: nocover
text = text.decode('utf-8')
token = self.token_acquirer.do(text)
params = utils.build_params(query=text, src=src, dest=dest, token=token)
params = utils.build_params(query=text, src=src, dest=dest,
token=token)
r = self.session.get(urls.TRANSLATE, params=params)
data = utils.format_json(r.text)

View File

@ -39,7 +39,8 @@ class TokenAcquirer(object):
950629.577246
"""
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):
self.session = session or requests.Session()
@ -96,7 +97,8 @@ class TokenAcquirer(object):
elif isinstance(node, ast.BitXor): # pragma: nocover
operator = '^'
# a safety way to avoid Exceptions
clause = compile('{1}{0}{2}'.format(operator, keys['a'], keys['b']), '', 'eval')
clause = compile('{1}{0}{2}'.format(
operator, keys['a'], keys['b']), '', 'eval')
value = eval(clause, dict(__builtin__={}))
result = '{}.{}'.format(n, value)
@ -150,7 +152,8 @@ class TokenAcquirer(object):
elif l < 2048:
e.append(l >> 6 | 192)
# append calculated value if l matches special condition
elif (l & 64512) == 55296 and g + 1 < size and ord(text[g + 1]) & 64512 == 56320:
elif (l & 64512) == 55296 and g + 1 < size and \
ord(text[g + 1]) & 64512 == 56320:
g += 1
l = 65536 + ((l & 1023) << 10) + ord(text[g]) & 1023
e.append(l >> 18 | 240)