1111import  ctypes 
1212import  json 
1313import  re 
14- import  time 
1514
1615from  textblob .compat  import  PY2 , request , urlencode 
1716from  textblob .exceptions  import  TranslatorError , NotTranslated 
@@ -31,7 +30,7 @@ class Translator(object):
3130        u'es' 
3231    """ 
3332
34-     url  =  "http://translate.google.com/translate_a/t" 
33+     url  =  "http://translate.google.com/translate_a/t?client=webapp&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&ie=UTF-8&oe=UTF-8&otf=2&ssel=0&tsel=0&kc=1 " 
3534
3635    headers  =  {
3736        'Accept' : '*/*' ,
@@ -45,11 +44,14 @@ def translate(self, source, from_lang='auto', to_lang='en', host=None, type_=Non
4544        """Translate the source text from one language to another.""" 
4645        if  PY2 :
4746            source  =  source .encode ('utf-8' )
48-         data  =  {"client" : "p" ,
49-                 "ie" : "UTF-8" , "oe" : "UTF-8" ,
50-                 "dt" : "at" , "tk" : _calculate_tk (source ),
51-                 "sl" : from_lang , "tl" : to_lang , "text" : source }
52-         response  =  self ._request (self .url , host = host , type_ = type_ , data = data )
47+         data  =  {"q" : source }
48+         url  =  u'{url}&sl={from_lang}&tl={to_lang}&hl={to_lang}&tk={tk}' .format (
49+             url = self .url ,
50+             from_lang = from_lang ,
51+             to_lang = to_lang ,
52+             tk = _calculate_tk (source ),
53+         )
54+         response  =  self ._request (url , host = host , type_ = type_ , data = data )
5355        result  =  json .loads (response )
5456        if  isinstance (result , list ):
5557            try :
@@ -65,11 +67,9 @@ def detect(self, source, host=None, type_=None):
6567            source  =  source .encode ('utf-8' )
6668        if  len (source ) <  3 :
6769            raise  TranslatorError ('Must provide a string with at least 3 characters.' )
68-         data  =  {"client" : "p" ,
69-                 "ie" : "UTF-8" , "oe" : "UTF-8" ,
70-                 "dt" : "at" , "tk" : _calculate_tk (source ),
71-                 "sl" : "auto" , "text" : source }
72-         response  =  self ._request (self .url , host = host , type_ = type_ , data = data )
70+         data  =  {"q" : source }
71+         url  =  u'{url}&sl=auto&tk={tk}' .format (url = self .url , tk = _calculate_tk (source ))
72+         response  =  self ._request (url , host = host , type_ = type_ , data = data )
7373        result , language  =  json .loads (response )
7474        return  language 
7575
@@ -105,7 +105,10 @@ def _unescape(text):
105105def  _calculate_tk (source ):
106106    """Reverse engineered cross-site request protection.""" 
107107    # Source: https://github.com/soimort/translate-shell/issues/94#issuecomment-165433715 
108-     b  =  int (time .time () /  3600 )
108+     # Source: http://www.liuxiatool.com/t.php 
109+ 
110+     tkk  =  [406398 , 561666268  +  1526272306 ]
111+     b  =  tkk [0 ]
109112
110113    if  PY2 :
111114        d  =  map (ord , source )
@@ -114,17 +117,20 @@ def _calculate_tk(source):
114117
115118    def  RL (a , b ):
116119        for  c  in  range (0 , len (b ) -  2 , 3 ):
117-             d  =  b [c + 2 ]
120+             d  =  b [c   +   2 ]
118121            d  =  ord (d ) -  87  if  d  >=  'a'  else  int (d )
119122            xa  =  ctypes .c_uint32 (a ).value 
120-             d  =  xa  >>  d  if  b [c + 1 ] ==  '+'  else  xa  <<  d 
123+             d  =  xa  >>  d  if  b [c   +   1 ] ==  '+'  else  xa  <<  d 
121124            a  =  a  +  d  &  4294967295  if  b [c ] ==  '+'  else  a  ^  d 
122125        return  ctypes .c_int32 (a ).value 
123126
124127    a  =  b 
128+ 
125129    for  di  in  d :
126130        a  =  RL (a  +  di , "+-a^+6" )
131+ 
127132    a  =  RL (a , "+-3^+b+-f" )
133+     a  ^=  tkk [1 ]
128134    a  =  a  if  a  >=  0  else  ((a  &  2147483647 ) +  2147483648 )
129135    a  %=  pow (10 , 6 )
130136
0 commit comments