pndurette/gTTS

(temporary?) fix for AttributeError: 'NoneType' object has no attribute 'group'

Closed this issue · 20 comments

Found a fix (atleast temporary)

in gtts_token/gtts_token.py

remove lines

        tkk_expr = re.search(".*?(TKK=.*?;)W.*?", line).group(1)
        a = re.search("a\\\\x3d(-?\d+);", tkk_expr).group(1)
        b = re.search("b\\\\x3d(-?\d+);", tkk_expr).group(1)

        result = str(hours) + "." + str(int(a) + int(b))

replace them with line
result = re.search("TKK='(.+?)';", line).group(1)

same issue, can you make a pull request to repo?

not right now, don't have my .git enviroment set yet after OS reinstall.

In the meantime just edit the file as described (just comment those 4 lines and add one line) and basic TTS will be working again (it does for me). I am not sure if it does affect something inadvertedly, but i don't suppose so.

in my case this file is present in /usr/local/lib/pythonX.X/dist-packages/gtts_token/gtts_token.py

Mic76 commented
luapp commented

hello, I try today to use the gtts on python but is not working
can somebody help me please ?

the error is:

Exception has occurred: OSError
[Errno 22] Invalid argument: 'C:\test.mp3'
File "C:\Users\Administrator\Desktop\Speech-Recognition.py", line 39, in
speech.save("C:\test.mp3")

My code:

import speech_recognition as sr
from gtts import gTTS
import os
import playsound

while True:

command = 0
speech = 0

mic = sr.Microphone()
r = sr.Recognizer()
with mic as source:
print("Calibrate Microphone")
print("Please wait...")
r.adjust_for_ambient_noise(source, duration=1)
print("listening...")
audio = r.listen(source)

try:
command = (r.recognize_google(audio))
print("you said:"+command)

except sr.UnknownValueError:
print("")

except sr.RequestError:
print("")

if "hello" in command:
print("hello")
speech = gTTS("hello", lang="en")
if os.path.exists("C:\test.mp3"):
print("file exist")
playsound.playsound("C:\test.mp3", True)
else:
print("saving new mp3 sound")
speech.save("C:\test.mp3")
playsound.playsound("C:\test.mp3", True)

completely unrelated to this issue

luapp commented

oups sorry

aro28 commented

Same for me.

Assistant is listening....

ON_CONVERSATION_TURN_STARTED
ON_END_OF_UTTERANCE
ON_END_OF_UTTERANCE
ON_RECOGNIZING_SPEECH_FINISHED:
{"text": "stream I got you"}
ON_RECOGNIZING_SPEECH_FINISHED:
{"text": "stream I got you"}
stopping vlc
Getting youtube link
It is no longer available in youtube
ERROR:main:'NoneType' object has no attribute 'group'
Traceback (most recent call last):
File "/home/pi/GassistPi/src/actions.py", line 1004, in YouTube_No_Autoplay
say("Getting youtube link")
File "/home/pi/GassistPi/src/actions.py", line 213, in say
tts.save(ttsfilename)
File "/home/pi/env/lib/python3.5/site-packages/gtts/tts.py", line 247, in save
self.write_to_fp(f)
File "/home/pi/env/lib/python3.5/site-packages/gtts/tts.py", line 187, in write_to_fp
part_tk = self.token.calculate_token(part)
File "/home/pi/env/lib/python3.5/site-packages/gtts_token/gtts_token.py", line 28, in calculate_token
seed = self._get_token_key()
File "/home/pi/env/lib/python3.5/site-packages/gtts_token/gtts_token.py", line 62, in _get_token_key
a = re.search("a\\x3d(-?\d+);", tkk_expr).group(1)
AttributeError: 'NoneType' object has no attribute 'group'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/pi/GassistPi/src/main.py", line 661, in
Myassistant().main()
File "/home/pi/GassistPi/src/main.py", line 518, in main
YouTube_No_Autoplay(str(usrcmd).lower())
File "/home/pi/GassistPi/src/actions.py", line 1017, in YouTube_No_Autoplay
say('It is no longer available in youtube')
File "/home/pi/GassistPi/src/actions.py", line 213, in say
tts.save(ttsfilename)
File "/home/pi/env/lib/python3.5/site-packages/gtts/tts.py", line 247, in save
self.write_to_fp(f)
File "/home/pi/env/lib/python3.5/site-packages/gtts/tts.py", line 187, in write_to_fp
part_tk = self.token.calculate_token(part)
File "/home/pi/env/lib/python3.5/site-packages/gtts_token/gtts_token.py", line 28, in calculate_token
seed = self._get_token_key()
File "/home/pi/env/lib/python3.5/site-packages/gtts_token/gtts_token.py", line 62, in _get_token_key
a = re.search("a\\x3d(-?\d+);", tkk_expr).group(1)
AttributeError: 'NoneType' object has no attribute 'group'
^CException ignored in: <module 'threading' from '/usr/lib/python3.5/threading.py'>
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 1288, in _shutdown
t.join()
File "/usr/lib/python3.5/threading.py", line 1054, in join
self._wait_for_tstate_lock()
File "/usr/lib/python3.5/threading.py", line 1070, in _wait_for_tstate_lock
elif lock.acquire(block, timeout):
KeyboardInterrupt

Well, I wish I saw this earlier. Alternative solution is to reinstall anaconda, reset the env and it will work without any change of codes. It is still a weird to observe such an error.

All right guys, I think you just need to update gtts_token to the new version.
The code below is taken from the new gtts_token.py, which addresses the group attribute error

 def _get_token_key(self):
    if self.token_key is not None:
        return self.token_key
    response = requests.get("https://translate.google.com/")
    line = response.text.split('\n')[-1]
    tkk_expr = re.search(".*?(TKK=.*?;)W.*?", line).group(1)

    try:
        # Grab the token directly if already generated by function call
        result = re.search("\d{6}\.[0-9]+", tkk_expr).group(0)
    except AttributeError:
        # Generate the token using algorithm
        timestamp = calendar.timegm(time.gmtime())
        hours = int(math.floor(timestamp / 3600))
        a = re.search("a\\\\x3d(-?\d+);", tkk_expr).group(1)
        b = re.search("b\\\\x3d(-?\d+);", tkk_expr).group(1)

        result = str(hours) + "." + str(int(a) + int(b))

    self.token_key = result
    return result
aro28 commented

How can i update gtts_token? Are you facing same issue?

aro28 commented

I use Raspbian OS.

Apparently pip update install gtts_token didn't work.
Try this one:

pip uninstall gtts_token
pip install gtts_token

This should update the script to the latest version and it will just work.

aro28 commented

I've installed it. But anyway error
ON_CONVERSATION_TURN_STARTED
ON_END_OF_UTTERANCE
ON_END_OF_UTTERANCE
ON_RECOGNIZING_SPEECH_FINISHED:
{"text": "stream I got you"}
ON_RECOGNIZING_SPEECH_FINISHED:
{"text": "stream I got you"}
stopping vlc
ERROR:main:'NoneType' object has no attribute 'group'
Traceback (most recent call last):
File "/home/pi/GassistPi/src/actions.py", line 1004, in YouTube_No_Autoplay
say("Getting youtube link")
File "/home/pi/GassistPi/src/actions.py", line 207, in say
words= translator.translate(words, dest=language)
File "/home/pi/env/lib/python3.5/site-packages/googletrans/client.py", line 172, in translate
data = self._translate(text, dest, src)
File "/home/pi/env/lib/python3.5/site-packages/googletrans/client.py", line 75, in _translate
token = self.token_acquirer.do(text)
File "/home/pi/env/lib/python3.5/site-packages/googletrans/gtoken.py", line 180, in do
self._update()
File "/home/pi/env/lib/python3.5/site-packages/googletrans/gtoken.py", line 59, in _update
code = unicode(self.RE_TKK.search(r.text).group(1)).replace('var ', '')
AttributeError: 'NoneType' object has no attribute 'group'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/pi/GassistPi/src/main.py", line 661, in
Myassistant().main()
File "/home/pi/GassistPi/src/main.py", line 518, in main
YouTube_No_Autoplay(str(usrcmd).lower())
File "/home/pi/GassistPi/src/actions.py", line 1017, in YouTube_No_Autoplay
say('It is no longer available in youtube')
File "/home/pi/GassistPi/src/actions.py", line 207, in say
words= translator.translate(words, dest=language)
File "/home/pi/env/lib/python3.5/site-packages/googletrans/client.py", line 172, in translate
data = self._translate(text, dest, src)
File "/home/pi/env/lib/python3.5/site-packages/googletrans/client.py", line 75, in _translate
token = self.token_acquirer.do(text)
File "/home/pi/env/lib/python3.5/site-packages/googletrans/gtoken.py", line 180, in do
self._update()
File "/home/pi/env/lib/python3.5/site-packages/googletrans/gtoken.py", line 59, in _update
code = unicode(self.RE_TKK.search(r.text).group(1)).replace('var ', '')
AttributeError: 'NoneType' object has no attribute 'group'

Based on your error message, seems like it is calling some functions in "/home/pi/env/lib/python3.5/site-packages/googletrans/gtoken.py", which is not gtts_token.py

Perhaps you want to try to do:
pip uninstall googletrans
pip install googletrans

By the way if you have gtts_token installed before, you have to do " uninstall and install" in order to update the scripts.

Had the same issue, was fixed by

replace them with line
result = re.search("TKK='(.+?)';", line).group(1)

from @deltaflyer4747 suggestion, thx!

I wanted to make a PR ....
but don't found the gtts_token.py file ;-)
but the patch works for me too

The issue is not with this package, instead of with gtts-token.
the package is patched already

tkk_expr = re.search(".?(TKK=.?;)W.*?", line).group(1)

Updating dependencies solved the issue.

No PR is required to this repository.

I don't see gtts-token as a dependency on googletrans. How you add and/or use gtts-token?

gtts-token is install along with gTTS.

It is defined in setup.py line: 26