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

Translation not working - always returns empty string as response #115

Closed
AdrianLC opened this issue Feb 15, 2016 · 8 comments
Closed

Translation not working - always returns empty string as response #115

AdrianLC opened this issue Feb 15, 2016 · 8 comments

Comments

@AdrianLC
Copy link
Contributor

Hello,

Google must have changed something.

In [1]: import textblob

In [2]: blob = textblob.TextBlob("bonjour")

In [3]: blob.translate()
> /home/alopez/.virtualenvs/proj/lib/python2.7/site-packages/textblob/translate.py(42)translate()
     41         import ipdb; ipdb.set_trace()
---> 42         json5 = self._get_json5(self.url, host=host, type_=type_, data=data)
     43         if self._translation_successful(json5):

ipdb> s
--Call--
> /home/alopez/.virtualenvs/proj/lib/python2.7/site-packages/textblob/translate.py(84)_get_json5()
     83 
---> 84     def _get_json5(self, url, host=None, type_=None, data=None):
     85         encoded_data = urlencode(data).encode('utf-8')

ipdb> n
> /home/alopez/.virtualenvs/proj/lib/python2.7/site-packages/textblob/translate.py(85)_get_json5()
     84     def _get_json5(self, url, host=None, type_=None, data=None):
---> 85         encoded_data = urlencode(data).encode('utf-8')
     86         req = request.Request(url=url, headers=self.headers, data=encoded_data)

ipdb> 
> /home/alopez/.virtualenvs/proj/lib/python2.7/site-packages/textblob/translate.py(86)_get_json5()
     85         encoded_data = urlencode(data).encode('utf-8')
---> 86         req = request.Request(url=url, headers=self.headers, data=encoded_data)
     87         if host or type_:

ipdb> 
> /home/alopez/.virtualenvs/proj/lib/python2.7/site-packages/textblob/translate.py(87)_get_json5()
     86         req = request.Request(url=url, headers=self.headers, data=encoded_data)
---> 87         if host or type_:
     88             req.set_proxy(host=host, type=type_)

ipdb> 
> /home/alopez/.virtualenvs/proj/lib/python2.7/site-packages/textblob/translate.py(89)_get_json5()
     88             req.set_proxy(host=host, type=type_)
---> 89         resp = request.urlopen(req)
     90         content = resp.read()

ipdb> 
> /home/alopez/.virtualenvs/proj/lib/python2.7/site-packages/textblob/translate.py(90)_get_json5()
     89         resp = request.urlopen(req)
---> 90         content = resp.read()
     91         return content.decode('utf-8')

ipdb> 
> /home/alopez/.virtualenvs/proj/lib/python2.7/site-packages/textblob/translate.py(91)_get_json5()
     90         content = resp.read()
---> 91         return content.decode('utf-8')
     92 

ipdb> content
'""'
ipdb> resp.code
200
ipdb> resp.msg
'OK'

I will post an update if I find out something...

@AdrianLC AdrianLC changed the title Translation not working - always returs empty string as response Translation not working - always returns empty string as response Feb 15, 2016
@AdrianLC
Copy link
Contributor Author

@jschnurr Hi. I see you did the change to the json format. May I ask, where/how did you find out about the client=p parameter and its options?

@jschnurr
Copy link
Collaborator

I don't recall. I do know that the documentation was limited / non-existent.

@Gijs-Koot
Copy link

Google did indeed change the format. The request still works, but the response body is no longer a json encoded object, but a simple string. I was investigating the same problem, see http://stackoverflow.com/questions/35420602/python-textblob-not-translating/35421116#35421116.

@AdrianLC
Copy link
Contributor Author

They also added a new code parameter to validate requests from their apps exclusively.
There's more info and a solution in soimort/translate-shell#94.
I will have a PR ready soon.

@jschnurr
Copy link
Collaborator

I provided PR #118 last night, but it doesn't deal with this extra parameter.

@AdrianLC
Copy link
Contributor Author

Yeah. We've been working in the same :(
Sorry, I would have merged your PR into mine if I didn't have the work already done.

@jschnurr
Copy link
Collaborator

The solutions are similar. It seems there are two factors that can cause "empty string" and/or 403 results from the Google Translate API:

  1. Headers. Once I added Connection & Accept headers, I started getting results back.
    headers = {'Connection': 'keep-alive',
               'Accept': '*/*',
               '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')
  1. tk parameter. Your solution doesn't have the headers, but the addition of the tk parameter appears to have the same effect.

Perhaps we should go with a 'belt and suspenders' approach here, and merge them together.

What do you think?

@AdrianLC
Copy link
Contributor Author

It can't hurt I suppose. And I see in the inspector that Google is using them so why not.
Already added. Thanks @jschnurr

@sloria sloria closed this as completed in 29515d1 Feb 18, 2016
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

No branches or pull requests

3 participants