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({
|
||||
'User-Agent': user_agent,
|
||||
})
|
||||
self.token_acquirer = TokenAcquirer(session=self.session)
|
||||
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
|
||||
|
@ -41,9 +41,10 @@ class TokenAcquirer(object):
|
||||
RE_TKK = re.compile(r'TKK=eval\(\'\(\(function\(\)\{(.+?)\}\)\(\)\)\'\);',
|
||||
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.tkk = tkk
|
||||
self.host = host if 'http' in host else 'https://' + host
|
||||
|
||||
def _update(self):
|
||||
"""update tkk
|
||||
@ -53,7 +54,7 @@ class TokenAcquirer(object):
|
||||
if self.tkk and int(self.tkk.split('.')[0]) == now:
|
||||
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'
|
||||
code = unicode(self.RE_TKK.search(r.text).group(1)).replace('var ', '')
|
||||
# unescape special ascii characters such like a \x3d(=)
|
||||
|
Loading…
Reference in New Issue
Block a user