Skip to content

Commit b9fc62a

Browse files
committed
Merge branch 'EpicJhon-dev' into dev
2 parents 86598a3 + 4cd410f commit b9fc62a

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ Contributors (chronological)
2222
- Jonathon Coe `@jonmcoe <https://github.com/jonmcoe>`_
2323
- Adrián López Calvo `@AdrianLC <https://github.com/AdrianLC>`_
2424
- Nitish Kulshrestha `@nitkul <https://github.com/nitkul>`_
25+
- Jhon Eslava `@EpicJhon <https://github.com/EpicJhon>`_

tests/test_translate.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ def test_failed_translation_raises_not_translated(self, mock_request):
3838
assert_equal(mock_request.call_count, len(failed_responses))
3939

4040
@mock.patch("textblob.translate.Translator._request")
41-
def test_tk_parameter_included_in_requests(self, mock_request):
41+
def test_tk_parameter_included_in_request_url(self, mock_request):
4242
mock_request.return_value = '["Esta es una frase.","en"]'
4343
self.translator.translate(self.sentence, to_lang="es")
4444
assert_true(mock_request.called_once)
4545
args, kwargs = mock_request.call_args
46-
tk = kwargs['data']['tk']
47-
assert_true(re.match(r'^\d+\.\d+$', tk))
46+
url = args[0]
47+
assert_true(re.match('.+&tk=\d+\.\d+$', url))
4848

4949
@mock.patch('textblob.translate.Translator._request')
5050
def test_detect(self, mock_request):
@@ -84,7 +84,7 @@ def test_translate_spaces(self):
8484
assert_equal(to_en, "Hello, my name is Adrian! How are you? I am good")
8585

8686
def test_translate_missing_from_language_auto_detects(self):
87-
text = "Ich besorge das Bier"
87+
text = "Ich hole das Bier"
8888
translated = self.translator.translate(text, to_lang="en")
8989
assert_equal(translated, "I'll get the beer")
9090

@@ -94,16 +94,16 @@ def test_translate_text(self):
9494
assert_equal(translated, "Esta es una frase.")
9595
es_text = "Esta es una frase."
9696
to_en = self.translator.translate(es_text, from_lang="es", to_lang="en")
97-
assert_equal(to_en, "This is a sentence.")
97+
assert_equal(to_en, "This is a phrase.")
9898

9999
def test_translate_non_ascii(self):
100100
text = "ذات سيادة كاملة"
101101
translated = self.translator.translate(text, from_lang='ar', to_lang='en')
102102
assert_equal(translated, "With full sovereignty")
103103

104-
text2 = "美丽优于丑陋"
104+
text2 = "美丽比丑陋更好"
105105
translated = self.translator.translate(text2, from_lang="zh-CN", to_lang='en')
106-
assert_equal(translated, "Beautiful is better than ugly")
106+
assert_equal(translated, "Beauty is better than ugly")
107107

108108
@mock.patch('textblob.translate.Translator._validate_translation', mock.MagicMock())
109109
def test_translate_unicode_escape(self):

textblob/translate.py

Lines changed: 21 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"
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):
105105
def _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

Comments
 (0)