Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError: not enough values to unpack (expected 2, got 1) #372

Open
yasminaaq opened this issue Mar 18, 2021 · 12 comments
Open

ValueError: not enough values to unpack (expected 2, got 1) #372

yasminaaq opened this issue Mar 18, 2021 · 12 comments

Comments

@yasminaaq
Copy link

I keep getting this error when calling detect_language.
This has started happening today only.

@igalma
Copy link

igalma commented Mar 18, 2021

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.

@quillfires
Copy link

I keep getting the same error when calling detect_language. Perhaps they changed changed their translate API again. Need a fix 😩

@bappctl
Copy link

bappctl commented Mar 18, 2021

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)

@mishra011
Copy link

same issue

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)

@quillfires
Copy link

quillfires commented Mar 19, 2021

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

@igalma
Copy link

igalma commented Mar 19, 2021

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

@quillfires
Copy link

this worked...why dont you pr and do this??

@igalma
Copy link

igalma commented Mar 19, 2021

Because I'm not a contributor... Just solved it for myself and decided to share.

t2kpbraune added a commit to t2kpbraune/TextBlob that referenced this issue Mar 21, 2021
@quillfires
Copy link

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? 🤔

@igalma
Copy link

igalma commented Mar 23, 2021

Seems they changed the api back to what it used to be so you can move back to the original way you used it

@bappctl
Copy link

bappctl commented Mar 23, 2021

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.

@markfilan
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants