ssut / py-googletrans

(unofficial) Googletrans: Free and Unlimited Google translate API for Python. Translates totally free of charge.
http://py-googletrans.rtfd.io
MIT License
3.93k stars 724 forks source link

error in result (AttributeError: 'NoneType' object has no attribute 'group') #234

Open Mohammadwh opened 4 years ago

Mohammadwh commented 4 years ago

hi i use Python 3.6.9 and when i call translate have this error: image

ssut commented 4 years ago

It seems the problem comes from such as network environment, rate limits. Could you provide your environment info, e.g. OS, ISP(your network provider), etc. Also please let me know whether it's okay from translate.google.com.

1e3m commented 4 years ago

I am getting this error too. It was starting 28 october.

Win8.1 last updated, Python version 3.6.6, googletrans 3.0.0,

laubil commented 4 years ago

same here. googletrans 3.0.0, Python 3.8, Linux Ubuntu 20.04.

In a list of 10 words to translate, 2 or 3 would generate the error. Threading and pausing (1s) improves the problem but does not suppress it.

Mohammadwh commented 4 years ago

i test it on ubuntu server and windows 10 but not work

ssut commented 4 years ago

Could you let me know your location and internet service provider? and did you check if it's working on translate.google.com? Because this library is not an official implementation I need you to check your network condition or elaborate on what's giving your problem so that I can confirm this issue and help you.

1e3m commented 4 years ago

Could you let me know your location and internet service provider? and did you check if it's working on translate.google.com? Because this library is not an official implementation I need you to check your network condition or elaborate on what's giving your problem so that I can confirm this issue and help you.

Russia, Rostov on Don, ISP: Megalink. translate.goole.com is working without problems.

sahil-gorivale commented 4 years ago

I'm also getting same issue.

pbienst commented 4 years ago

Note sure if it's related, but over on gTTS they have similar issues with accessing google translate, but then for text-to-speech. One user reported that switching from urllib.requests to requests fixed the issue:

https://github.com/pndurette/gTTS/issues/226

It might be worthwhile experimenting with vanilla requests instead of httpx...

ssut commented 4 years ago

I've tried reproducing this several times but so far I don't think I can easily reproduce this in my environment (from the major ISPs in South Korea: Korea Telecom, SK Broadband, and U+.)

Python 3.8.5 (default, Jul 21 2020, 10:48:26)
[Clang 11.0.3 (clang-1103.0.32.62)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from googletrans import Translator
>>> t = Translator()
>>> t.translate('asdf')
<googletrans.models.Translated object at 0x104596790>

However, I'm investigating this issue further right now and will get back to you shortly - I believe it's related to something strange happening with Google's anti-spamming work. If someone else has experienced similar or the same issues please submit to this issue with such details as ISPs, location, and whether it happens consistently at all or not.

suciokhan commented 4 years ago

Using it over a VPN, it has failed for me for servers in multiple countries over UDP. I'd rather not say specifics as to which ones. I added in try/except loop that makes it try a random new proxy if a query fails, but even so I can't seem to escape these failures. It works for a seemingly-random number of queries before failing (nowhere near 300, probably ~20 at the most). Using Ubuntu 20.04, Python 3.8.3, googletrans 3.0.0.

ssut commented 4 years ago

Using it over a VPN, it has failed for me for servers in multiple countries over UDP. I'd rather not say specifics as to which ones. I added in try/except loop that makes it try a random new proxy if a query fails, but even so I can't seem to escape these failures. It works for a seemingly-random number of queries before failing (nowhere near 300, probably ~20 at the most). Using Ubuntu 20.04, Python 3.8.3, googletrans 3.0.0.

Thanks for your information. I will try testing on other networks using proxies.

pbienst commented 4 years ago

I upgraded httpx to the latest version (in spite of pip complaining it's not compatible with py-googletrans), and the stability seems to be back to how it was before... Could be a coincidence though.

ssut commented 4 years ago

I upgraded httpx to the latest version (in spite of pip complaining it's not compatible with py-googletrans), and the stability seems to be back to how it was before... Could be a coincidence though.

Could you guys here test this and let me know if it fixes the issue?

Mohammadwh commented 4 years ago

I upgraded httpx to the latest version (in spite of pip complaining it's not compatible with py-googletrans), and the stability seems to be back to how it was before... Could be a coincidence though.

Could you guys here test this and let me know if it fixes the issue?

i test it not work 😐

zz860808 commented 4 years ago

i also got this error right now. i am a student from Taiwan. My ISP is TANet. And i try translate.goole.com on Chrome is okay without any problems. thanks.

zz860808 commented 4 years ago

i also got this error right now. i am a student from Taiwan. My ISP is TANet. And i try translate.goole.com on Chrome is okay without any problems. thanks.

But after i try it on my other computer with Linux Ubuntu 18.04 (never install the "googletrans"), it performed okay. So i think it might be banned by Google, and it'll be banned for a long time.

Dauflo commented 4 years ago

I'm actually having the same issue running googletrans inside a docker container. The error would stop all the processes of the container, but as soon as i restart the container, googletrans is not returning any error for the current process. In my case, i don't think it can be a ban from Google, otherwise it couldn't not work when i restart my container

alainrouillon commented 4 years ago

Hi all, We've been using py googletrans for a few weeks by now. Everything was going on ok, but it turned to go into "AttributeError: 'NoneType' object has no attribute 'group'" error since a few days. I figured out that the token generation in gtoken.py was involved as it is not retrieved ok by requesting google. I tried to implement a retry to recall the _update() function until it provides a valid token. This solution does not work as it keeps stuck in the retry loop (i.e. it never gets a proper token ...). Sure I would be interested in contributing to the solution, and/or get the issue solving if any.

alainrouillon commented 4 years ago

I'm actually having the same issue running googletrans inside a docker container. The error would stop all the processes of the container, but as soon as i restart the container, googletrans is not returning any error for the current process. In my case, i don't think it can be a ban from Google, otherwise it couldn't not work when i restart my container

I faced this behaviour as after a certain period of time a new request submit works fine ...

VOINTENT commented 4 years ago

This problem occurs if you create a Translator instance for each use of the request. This problem can be partially avoided by using only one instance of the class.

alainrouillon commented 4 years ago

This problem occurs if you create a Translator instance for each use of the request. This problem can be partially avoided by using only one instance of the class.

@vointent: Implemented as of, got stability back ! Thx mate

khannoaman commented 4 years ago

This problem occurs if you create a Translator instance for each use of the request. This problem can be partially avoided by using only one instance of the class.

Thanks, it worked when using only one instance of the class.

vonblanc commented 4 years ago

Tried the one instance solution worked for a while but now it's back.

Sn1F3rt commented 4 years ago

I upgraded httpx to the latest version (in spite of pip complaining it's not compatible with py-googletrans), and the stability seems to be back to how it was before... Could be a coincidence though.

Worked for me. Quite interestingly, changing the service URL to translate.google.com worked on my PC but not on my Linux server. However, there upgrading httpx seems to have temporarily fixed it. :)

TrongTheAlpaca commented 4 years ago

Hello, seems like I have the same problem. I have tried changing the service URL to translate.google.com, having only one instance of the Translator, and updating the package httpx, but the problem still persists. Oddly enough, the problem occur only from time to time, and I have no idea why.

I'm a student living in Norway if that helps.

RahulARanger commented 4 years ago

I guess this problem occurs randomly but doesn't occur always

So I tried to create temp. solution by constantly giving requests until it is accepted,

I tried as many times as I can.

import googletrans
class GT:
    def __init__(self):
        while True:
            self.translator=googletrans.Translator(service_urls=['translate.google.com'])
            try:
                trial=self.translator.detect('Hello there')
                break
            except Exception as e:
                print(e)   # can be commented
    def doThings(self):
        pass
if __name__=='__main__':
    a=GT()
alainrouillon commented 4 years ago

I guess this problem occurs randomly but doesn't occur always

So I tried to create temp. solution by constantly giving requests until it is accepted,

I tried as many times as I can it worked

import googletrans
class GT:
    def __init__(self):
        while True:
            self.translator=googletrans.Translator(service_urls=['translate.google.com'])
            try:
                trial=self.translator.detect('Hello there')
                break
            except Exception as e:
                print(e)   # can be commented
    def doThings(self):
        pass
if __name__=='__main__':
    a=GT()

This is a nice approach. I implemented something similar straight in gtoken.py from googletrans package. In case of persistent lack of positive feedback it ran into endless loop and end user could wait desperately :-(

I guess we're all facing the same issue, not from googletrans py package, but sure from google trying to oppose free usage of a service that has to be paid now...

RahulARanger commented 4 years ago

@alainrouillon yes that might be the case. but Once the Translator object is initialized properly then we can carry any number of operations. So it's with Google

Konstantina-Lazaridou commented 4 years ago

I also tried the one instance solution and it worked at first, but not anymore. I get the same AttributeError.

Konstantina-Lazaridou commented 4 years ago

This

pip install gtts --upgrade pip install gtts-token --upgrade

from here

Didn't work for me

grokpot commented 4 years ago

Thanks @Konstantina-Lazaridou (I deleted my comment after I realized it wasn't working permanently) I adapted @RahulARanger 's suggestion to my purpose and this is working:

        translator = Translator()
        # https://github.com/ssut/py-googletrans/issues/234
        while True:
          try:
            text = translator.translate(text).text
            break
          except Exception as e:
            translator = Translator()
Konstantina-Lazaridou commented 4 years ago

Any idea how many times does it loop until it works and breaks?

RahulARanger commented 4 years ago

Any idea how many times does it loop until it works and breaks?

Until now it took a maximum of 6 iterations and an average of 2 iterations. Till now I didn't experience a vast delay

eliran-n commented 4 years ago

I'm facing the same issue too... can't figure out how to fix it.

image

vonblanc commented 4 years ago

@RahulARanger and @ryanprater. That works, thanks guys

Mohammadwh commented 4 years ago

guys , you can use this for fix error:

from googletrans import Translator
from time import sleep
def getLang(text):
    translator = Translator()
    lang = None
    while lang == None:
        try:
            lang = translator.detect(text)
        except:
            translator = Translator()
            sleep(0.5)
            pass
    return lang

result = getLang('hello')            
print(result.lang)

def getTranslate(text,**kwargs):
    translator = Translator()
    result = None
    while result == None:
        try:
            result = translator.translate(text,**kwargs)
        except Exception as e:
            print(e)
            translator = Translator()
            sleep(0.5)
            pass
    return result     

result = getTranslate('hello',dest='ja')            
print(result)
SoohanC commented 4 years ago

@Mohammadwh This works for me! Thank you!

Tiubez commented 4 years ago

The issue came back for me too, about five days ago. At first very randomly and with lower occurrences, then in the last two days it ramped up to about 80% of the time.

Can confirm that the fixes given by:

@ryanprater @Mohammadwh

Are both working to resolve the issue.

SoohanC commented 4 years ago

@Tiubez The fixes will solve the problem, but looks like they do too many requests. I think this is just quick temporary fix for now. Also, request takes too long time.....

1e3m commented 4 years ago

I'm watched '_update' method in 'TokenAcquirer' class. I have a few questions.

If code returned NonType value raw_tkk = self.RE_TKK.search(r.text)

Then there was a check where we bypassed raw_tkk.group(1) for NonType:

if raw_tkk:
    self.tkk = raw_tkk.group(1)
    return

And then .group(1) for NonType object is called: self.RE_TKK.search(r.text).group(1).replace('var ', '')

This is the same as raw_tkk.group(1).replace('var', '')for raw_tkk NonType we got above.

It will lead to "None".group(1).replace('var ', '') -> Exception.

I think if was checked (raw_tkk is None) == True, should not calling self.RE_TKK.search(r.text).group(1).replace('var ', '')

Sorry for my english.

Codegenieur commented 4 years ago

Had the same error an hour ago too, still investigating and found this helpful thread.

guys , you can use this for fix error:

from googletrans import Translator
from time import sleep
def getLang(text):
    translator = Translator()
    lang = None
    while lang == None:
        try:
            lang = translator.detect(text)
        except:
            translator = Translator()
            sleep(0.5)
            pass
    return lang

result = getLang('hello')            
print(result.lang)

def getTranslate(text,**kwargs):
    translator = Translator()
    result = None
    while result == None:
        try:
            result = translator.translate(text,**kwargs)
        except Exception as e:
            print(e)
            translator = Translator()
            sleep(0.5)
            pass
    return result     

result = getTranslate('hello',dest='ja')            
print(result)

This can cause an endless loop in case result is always None.

Mohammadwh commented 4 years ago

Had the same error an hour ago too, still investigating and found this helpful thread.

guys , you can use this for fix error:

from googletrans import Translator
from time import sleep
def getLang(text):
    translator = Translator()
    lang = None
    while lang == None:
        try:
            lang = translator.detect(text)
        except:
            translator = Translator()
            sleep(0.5)
            pass
    return lang

result = getLang('hello')            
print(result.lang)

def getTranslate(text,**kwargs):
    translator = Translator()
    result = None
    while result == None:
        try:
            result = translator.translate(text,**kwargs)
        except Exception as e:
            print(e)
            translator = Translator()
            sleep(0.5)
            pass
    return result     

result = getTranslate('hello',dest='ja')            
print(result)

This can cause an endless loop in case result is always None.

you can set defult value and replace while true by for i in range πŸ™„ i test this code and workπŸ€”πŸ€”

Roadcrosser commented 4 years ago

EDIT: I made a comment below going into detail about the library if it's hard to understand the following comment.

I feel like running a loop to wait for a valid page to grab the TKK will become unfeasible continuing on as the TKK starts to show up less and less, until it just doesn't show up at all and all these scripts get stuck in a while loop spamming an endpoint.

The TKK appears to only be used on the single translation endpoint (that we are using), which is only used when "Instant Translation" is set to off, the setting of which can be seen in the following screenshot:

image

I took this from archive.org, as being able to turn off instant translations appears to have been deprecated since the new UI was launched, a fact which is corroborated here.

As this feature has been deprecated, it would make sense that the endpoint is as well, and thus there would be no need for a TKK at all, leading them to start removing it from their pages. It might be best to rewrite the library for the currently-used instant translation api instead. (Though reverse engineering that sure will be a pain, and there's no guarantee using it will be as painless).

I also realized my explainations will only make sense to people are really intimate with the code. I'll write a followup comment explaining how this library works.

Roadcrosser commented 4 years ago

Here is some supplementary information for my last comment, explaining how the library works to the uninitiated.

The endpoint used in the library to get translations is translate_a/single, which is a legacy endpoint used in Google Translate when you still had to click the translate button to make it work.

To deter people who using the endpoint automatically (which we are), they require your translation query to be sent with a token. This token is calculated using the text of the query as well as a value called TKK. This value changes once in a while (I don't remember the interval right now), and is embedded into the Javascript on the webpage.

So, we download the Translate page and locate the new TKK to update the cached one whenever it expires. This ensures the token can be calculated whenever we send a request.

Incidentally, by creating new instances of the translator every time you want to translate something, you end up querying for the TKK every time because the cache gets lost.

JaxChan25 commented 4 years ago

..SAME PROBLEM

RahulARanger commented 4 years ago
import googletrans
COUNT=0
class GT:
    def __init__(self):
        global COUNT
        while True:
            self.translator=googletrans.Translator(service_urls=['translate.google.com'])
            try:
                trial=self.translator.detect('Hello there')                
                break
            except Exception as e:
                COUNT+=1
if __name__=='__main__':    
    for i in range(100):
        a=GT()
        print(COUNT) # no of iterations
        COUNT=0

try this you can see it can max take 6-7 and avg. of 2 but each successful iteration takes 1 second time gap maybe we can boast this by threading (it didn't end up endless loop.) (This works only if you have valid internet connection else endless loop)

nyck33 commented 4 years ago

same problem here at a Canadian university using eduroam on WiFi: image

I'm trying @Mohammadwh first function, getTranslate() just wondering why did you include two functions?

Mohammadwh commented 4 years ago

same problem here at a Canadian university using eduroam on WiFi: hi i use this code on my project and just copy paste in github

KursX commented 4 years ago

Same problem. In use through command line from java code While using from command line for translate "text" -s en -d ru error occurs in 30-50% cases @ssut I can give you my test server credentials for reproduce this error

wajdyz commented 4 years ago

The same here, Saudi ISP. Works like 50% of the time, I use bulk translation and the standard translate.google