Skip to content

Commit cbae51c

Browse files
authored
Fix TK
1 parent 3a9dd7b commit cbae51c

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

textblob/translate.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import ctypes
1212
import json
1313
import re
14-
import time
1514

1615
from textblob.compat import PY2, request, urlencode
1716
from 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?client=webapp"
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,9 @@ 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 = self.url + "&sl=" + from_lang + "&tl=" + to_lang + "&hl=" + to_lang + "&tk=" + _calculate_tk(source)
49+
response = self._request(url, host=host, type_=type_, data=data)
5350
result = json.loads(response)
5451
if isinstance(result, list):
5552
try:
@@ -65,11 +62,9 @@ def detect(self, source, host=None, type_=None):
6562
source = source.encode('utf-8')
6663
if len(source) < 3:
6764
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)
65+
data = {"q": source}
66+
url = self.url + "&sl=auto&tk=" + _calculate_tk(source)
67+
response = self._request(url, host=host, type_=type_, data=data)
7368
result, language = json.loads(response)
7469
return language
7570

@@ -105,7 +100,10 @@ def _unescape(text):
105100
def _calculate_tk(source):
106101
"""Reverse engineered cross-site request protection."""
107102
# Source: https://github.com/soimort/translate-shell/issues/94#issuecomment-165433715
108-
b = int(time.time() / 3600)
103+
# Source: http://www.liuxiatool.com/t.php
104+
105+
tkk = [406398, 561666268 + 1526272306]
106+
b = tkk[0]
109107

110108
if PY2:
111109
d = map(ord, source)
@@ -114,17 +112,20 @@ def _calculate_tk(source):
114112

115113
def RL(a, b):
116114
for c in range(0, len(b) - 2, 3):
117-
d = b[c+2]
115+
d = b[c + 2]
118116
d = ord(d) - 87 if d >= 'a' else int(d)
119117
xa = ctypes.c_uint32(a).value
120-
d = xa >> d if b[c+1] == '+' else xa << d
118+
d = xa >> d if b[c + 1] == '+' else xa << d
121119
a = a + d & 4294967295 if b[c] == '+' else a ^ d
122120
return ctypes.c_int32(a).value
123121

124122
a = b
123+
125124
for di in d:
126125
a = RL(a + di, "+-a^+6")
126+
127127
a = RL(a, "+-3^+b+-f")
128+
a ^= tkk[1]
128129
a = a if a >= 0 else ((a & 2147483647) + 2147483648)
129130
a %= pow(10, 6)
130131

0 commit comments

Comments
 (0)