Open yasminaaq opened 3 years ago
calling translate also results in in exception because in _validate_translation function result.strip() is trying to strip a list. For some reason the returned value from the api changed to a very nested list.
I keep getting the same error when calling detect_language. Perhaps they changed changed their translate API again. Need a fix 😩
Running into same issue starting today. Surprisingly this library was pip installed a month back and working. Does this library get auto updated (which is not supposed to happen)
File "/home/deepak/miniconda3/lib/python3.8/site-packages/textblob/blob.py", line 568, in detect_language return self.translator.detect(self.raw) File "/home/deepak/miniconda3/lib/python3.8/site-packages/textblob/translate.py", line 73, in detect result, language = json.loads(response) ValueError: not enough values to unpack (expected 2, got 1)
Running into same issue starting today. Surprisingly this library was pip installed a month back and working. Does this library get auto updated (which is not supposed to happen)
Its not related to an update afaik.. And no update was released to pypi as well. It uses Google's translation API to do the thing. Most probably they've changed something up causing this to fall. We really need a good fix fast cause there's nothing better than this for this task
In the mean time I took the relevant functions and did some modifications to make it work. You guys can just copy this code and it will work (just fix some minor issues that created while copy pasting my code):
from textblob.exceptions import NotTranslated import ctypes import json from textblob.compat import request, urlencode
headers = { 'Accept': '/', 'Connection': 'keep-alive', 'User-Agent': ( 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) ' 'AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19') }
translate_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"
language = detect(source="some text in any language")
def detect(source, host=None, type_=None): """Detect the source text's language.""" if len(source) < 3: raise TranslatorError('Must provide a string with at least 3 characters.') data = {"q": source} url = '{url}&sl=auto&tk={tk}'.format(url=translate_url, tk=calculate_tk(source)) response = request(url, host=host, type=type_, data=data) language = json.loads(response)[0][0][2] return language
def request(url, host=None, type=None, data=None):
encoded_data = urlencode(data).encode('utf-8')
req = request.Request(url=url, headers=headers, data=encoded_data)
if host or type_:
req.set_proxy(host=host, type=type_)
resp = request.urlopen(req)
content = resp.read()
return content.decode('utf-8')
def calculate_tk(source): """Reverse engineered cross-site request protection."""
# Source: http://www.liuxiatool.com/t.php
tkk = [406398, 561666268 + 1526272306]
b = tkk[0]
d = source.encode('utf-8')
def RL(a, b):
for c in range(0, len(b) - 2, 3):
d = b[c + 2]
d = ord(d) - 87 if d >= 'a' else int(d)
xa = ctypes.c_uint32(a).value
d = xa >> d if b[c + 1] == '+' else xa << d
a = a + d & 4294967295 if b[c] == '+' else a ^ d
return ctypes.c_int32(a).value
a = b
for di in d:
a = RL(a + di, "+-a^+6")
a = RL(a, "+-3^+b+-f")
a ^= tkk[1]
a = a if a >= 0 else ((a & 2147483647) + 2147483648)
a %= pow(10, 6)
tk = '{0:d}.{1:d}'.format(a, a ^ b)
return tk
this worked...why dont you pr and do this??
Because I'm not a contributor... Just solved it for myself and decided to share.
In the mean time I took the relevant functions and did some modifications to make it work. You guys can just copy this code and it will work (just fix some minor issues that created while copy pasting my code):
from textblob.exceptions import NotTranslated import ctypes import json from textblob.compat import request, urlencode
headers = { 'Accept': '/', 'Connection': 'keep-alive', 'User-Agent': ( 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) ' 'AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19') }
translate_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"
language = detect(source="some text in any language")
def detect(source, host=None, type_=None): """Detect the source text's language.""" if len(source) < 3: raise TranslatorError('Must provide a string with at least 3 characters.') data = {"q": source} url = '{url}&sl=auto&tk={tk}'.format(url=translate_url, tk=calculate_tk(source)) response = request(url, host=host, type=type_, data=data) language = json.loads(response)[0][0][2] return language
def request(url, host=None, type=None, data=None):
encoded_data = urlencode(data).encode('utf-8') req = request.Request(url=url, headers=headers, data=encoded_data) if host or type_: req.set_proxy(host=host, type=type_) resp = request.urlopen(req) content = resp.read() return content.decode('utf-8')
def calculate_tk(source): """Reverse engineered cross-site request protection."""
Source: soimort/translate-shell#94 (comment)
Source: http://www.liuxiatool.com/t.php
tkk = [406398, 561666268 + 1526272306] b = tkk[0] d = source.encode('utf-8') def RL(a, b): for c in range(0, len(b) - 2, 3): d = b[c + 2] d = ord(d) - 87 if d >= 'a' else int(d) xa = ctypes.c_uint32(a).value d = xa >> d if b[c + 1] == '+' else xa << d a = a + d & 4294967295 if b[c] == '+' else a ^ d return ctypes.c_int32(a).value a = b for di in d: a = RL(a + di, "+-a^+6") a = RL(a, "+-3^+b+-f") a ^= tkk[1] a = a if a >= 0 else ((a & 2147483647) + 2147483648) a %= pow(10, 6) tk = '{0:d}.{1:d}'.format(a, a ^ b) return tk
Is this still working for you? 🤔
Seems they changed the api back to what it used to be so you can move back to the original way you used it
Seems they changed the api back to what it used to be so you can move back to the original way you used it
verified - you are right.
During a multiple value assignment, the ValueError: not enough values to unpack occurs when either you have fewer objects to assign than variables, or you have more variables than objects. This error caused by the mismatch between the number of values returned and the number of variables in the assignment statement. This error happened mostly in the case of using python split function. Verify the assignment variables. If the number of assignment variables is greater than the total number of variables, delete the excess variable from the assignment operator. The number of objects returned, as well as the number of variables available are the same.
To see what line is causing the issue, you could add some debug statements like this:
if len(line.split()) != "xx":
print line
This will resolve the value error.
I keep getting this error when calling detect_language. This has started happening today only.