gtoken: use custom server to generate token (#19)
* use custome server to generate token * fix test fail issue
This commit is contained in:
parent
286593607e
commit
ae4953dff9
@ -35,8 +35,8 @@ class Translator(object):
|
|||||||
self.session.headers.update({
|
self.session.headers.update({
|
||||||
'User-Agent': user_agent,
|
'User-Agent': user_agent,
|
||||||
})
|
})
|
||||||
self.token_acquirer = TokenAcquirer(session=self.session)
|
|
||||||
self.service_urls = service_urls or ['translate.google.com']
|
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
|
# Use HTTP2 Adapter if hyper is installed
|
||||||
try: # pragma: nocover
|
try: # pragma: nocover
|
||||||
|
@ -41,9 +41,10 @@ class TokenAcquirer(object):
|
|||||||
RE_TKK = re.compile(r'TKK=eval\(\'\(\(function\(\)\{(.+?)\}\)\(\)\)\'\);',
|
RE_TKK = re.compile(r'TKK=eval\(\'\(\(function\(\)\{(.+?)\}\)\(\)\)\'\);',
|
||||||
re.DOTALL)
|
re.DOTALL)
|
||||||
|
|
||||||
def __init__(self, tkk='0', session=None):
|
def __init__(self, tkk='0', session=None, host='translate.google.com'):
|
||||||
self.session = session or requests.Session()
|
self.session = session or requests.Session()
|
||||||
self.tkk = tkk
|
self.tkk = tkk
|
||||||
|
self.host = host if 'http' in host else 'https://' + host
|
||||||
|
|
||||||
def _update(self):
|
def _update(self):
|
||||||
"""update tkk
|
"""update tkk
|
||||||
@ -53,7 +54,7 @@ class TokenAcquirer(object):
|
|||||||
if self.tkk and int(self.tkk.split('.')[0]) == now:
|
if self.tkk and int(self.tkk.split('.')[0]) == now:
|
||||||
return
|
return
|
||||||
|
|
||||||
r = self.session.get('https://translate.google.com')
|
r = self.session.get(self.host)
|
||||||
# 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).group(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(=)
|
||||||
|
Loading…
Reference in New Issue
Block a user