Why "Googletrans.Translator" suddenly stopped working? [duplicate]
Asked Answered
K

3

3

I wrote a few lines using Translator function in Googletrans a few days ago. But I was trying to re-run those lines today and it popped a series of errors... I'm really confused by this. If you have experienced similar issue, please comment below. Any help is welcome!!

from googletrans import Translator
translator = Translator()
trans1 = translator.translate('Hello', dest = 'es')

The error I got is following:

AttributeError                            Traceback (most recent call last)
<ipython-input-19-c0f9e5495a2f> in <module>()
----> 1 trans1 = translator.translate('Hello', dest = 'es')

~\AppData\Local\Continuum\anaconda3\lib\site-packages\googletrans\client.py in translate(self, text, dest, src)
    170 
    171         origin = text
--> 172         data = self._translate(text, dest, src)
    173 
    174         # this code will be updated when the format is changed.

~\AppData\Local\Continuum\anaconda3\lib\site-packages\googletrans\client.py in _translate(self, text, dest, src)
     73             text = text.decode('utf-8')
     74 
---> 75         token = self.token_acquirer.do(text)
     76         params = utils.build_params(query=text, src=src, dest=dest,
     77                                     token=token)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\googletrans\gtoken.py in do(self, text)
    178 
    179     def do(self, text):
--> 180         self._update()
    181         tk = self.acquire(text)
    182         return tk

~\AppData\Local\Continuum\anaconda3\lib\site-packages\googletrans\gtoken.py in _update(self)
     57         r = self.session.get(self.host)
     58         # this will be the same as python code after stripping out a reserved word 'var'
---> 59         code = unicode(self.RE_TKK.search(r.text).group(1)).replace('var ', '')
     60         # unescape special ascii characters such like a \x3d(=)
     61         if PY3:  # pragma: no cover

AttributeError: 'NoneType' object has no attribute 'group'

I tried to do research on this error msg, but got nothing. What confused me the most is that this simple code worked perfectly fine 3 days ago. But when i open up this morning, i popped errors. Please help. Thank you so much!

Knowall answered 21/9, 2018 at 15:21 Comment(0)
M
4

I got the solution below from this link.

Solution:

pip install git+https://github.com/BoseCorp/py-googletrans.git --upgrade

It worked perfectly for me!

Mise answered 11/12, 2018 at 11:25 Comment(3)
This also stopped working now in 2022Drabble
As of 2023, this works for me: pip install googletrans==4.0.0-rc1Suppurative
does not some one send an email to the author of this package and notify him/her about this issue??!Teak
U
6

Update py-googletrans/googletrans/gtoken.py like the following:

RE_TKK = re.compile(r'TKK=eval\(\'\(\(function\(\)\{(.+?)\}\)\(\)\)\'\);',
                    re.DOTALL)
RE_RAWTKK = re.compile(r'tkk:\'([^\']*)\'',re.DOTALL)

def __init__(self, tkk='0', session=None, host='translate.google.com'):
    self.session = session or requests.Session()
    self.tkk = tkk
    self.host = host if 'http' in host else 'https://' + host

def _update(self):
    """update tkk
    """
    # we don't need to update the base TKK value when it is still valid
    now = math.floor(int(time.time() * 1000) / 3600000.0)
    if self.tkk and int(self.tkk.split('.')[0]) == now:
        return

    r = self.session.get(self.host)

    rawtkk = self.RE_RAWTKK.search(r.text)
    if rawtkk:
        self.tkk = rawtkk.group(1)
        return
Unformed answered 2/12, 2018 at 5:39 Comment(0)
M
4

I got the solution below from this link.

Solution:

pip install git+https://github.com/BoseCorp/py-googletrans.git --upgrade

It worked perfectly for me!

Mise answered 11/12, 2018 at 11:25 Comment(3)
This also stopped working now in 2022Drabble
As of 2023, this works for me: pip install googletrans==4.0.0-rc1Suppurative
does not some one send an email to the author of this package and notify him/her about this issue??!Teak
P
3

Google has changed the way the token is created. There is no fix at the time of writing. You have to wait for googletrans to be updated.

Prowl answered 22/9, 2018 at 21:40 Comment(6)
Hi Chris, thx for your answer. Do you know how long normally will it take to get fixed? based on your past experience maybe... Or do you know any alternative packages/functions? Thanks again!Knowall
It's June 2021 and the bug is alive and kicking! How long should I still wait for an update that ? :))Seline
As of 2023, this works for me: pip install googletrans==4.0.0-rc1Suppurative
@Dave: your comment merits being a separate answer, since it resolves the issue as of February 2024Milson
@essbee: Thanks. This question is closed (due to being a duplicate), thus I could not provide it as an answer. There is a link to the other question at the top of this page. Details of the solution are discussed there.Suppurative
as @Suppurative mentioned: "As of 2023, this works for me: pip install googletrans==4.0.0-rc1" what if this solution stops working, what is the permanent solution for this?Teak

© 2022 - 2024 — McMap. All rights reserved.