pndurette / gTTS

Python library and CLI tool to interface with Google Translate's text-to-speech API
http://gtts.readthedocs.org/
MIT License
2.31k stars 362 forks source link

Token: regexp never finds anything #137

Closed tdi-odoo closed 6 years ago

tdi-odoo commented 6 years ago

Hello,

I know the code yielding this error is not in this repository but the gtts-token repository seems inactive...

Since a few hours ago it seems that one of the regular expressions when searching "TKK='integer.integer';" in Google Translate HTTP response doesn't find anything.

Here is my Traceback:

gtts-cli 'hello' --output hello.mp3
Traceback (most recent call last):
  File "/usr/local/bin/gtts-cli", line 9, in <module>
    load_entry_point('gTTS==2.0.1', 'console_scripts', 'gtts-cli')()
  File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/gtts/cli.py", line 194, in tts_cli
    tts.write_to_fp(output)
  File "/usr/local/lib/python2.7/dist-packages/gtts/tts.py", line 187, in write_to_fp
    part_tk = self.token.calculate_token(part)
  File "/usr/local/lib/python2.7/dist-packages/gtts_token/gtts_token.py", line 28, in calculate_token
    seed = self._get_token_key()
  File "/usr/local/lib/python2.7/dist-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'

Is it possible this is caused by a modification in Google Translate HTTP response? I tried with other google translate python apps and they also fail to find the token...

Best regards, Thomas

ghost commented 6 years ago

Hello,

I know the code yielding this error is not in this repository but the gtts-token repository seems inactive...

Since a few hours ago it seems that one of the regular expressions when searching "TKK='integer.integer';" in Google Translate HTTP response doesn't find anything.

Here is my Traceback:

gtts-cli 'hello' --output hello.mp3
Traceback (most recent call last):
  File "/usr/local/bin/gtts-cli", line 9, in <module>
    load_entry_point('gTTS==2.0.1', 'console_scripts', 'gtts-cli')()
  File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/gtts/cli.py", line 194, in tts_cli
    tts.write_to_fp(output)
  File "/usr/local/lib/python2.7/dist-packages/gtts/tts.py", line 187, in write_to_fp
    part_tk = self.token.calculate_token(part)
  File "/usr/local/lib/python2.7/dist-packages/gtts_token/gtts_token.py", line 28, in calculate_token
    seed = self._get_token_key()
  File "/usr/local/lib/python2.7/dist-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'

Is it possible this is caused by a modification in Google Translate HTTP response? I tried with other google translate python apps and they also fail to find the token...

Best regards, Thomas

Yes It is not working for me too...

pndurette commented 6 years ago

It’s very possible they’ve changed the way they do things.. :|

Will have to check. The token is calculated in JavaScript. So it’s all about seeing what changed (if that’s the issue).

Been very busy lately, which is why I haven’t spent a significant amount of time giving lots of TLC do gTTS.

-- Pierre Nick Durette Sent from mobile

On Sep 21, 2018, at 1:22 AM, bornToCode notifications@github.com wrote:

Hello,

I know the code yielding this error is not in this repository but the gtts-token repository seems inactive...

Since a few hours ago it seems that one of the regular expressions when searching "TKK='integer.integer';" in Google Translate HTTP response doesn't find anything.

Here is my Traceback:

gtts-cli 'hello' --output hello.mp3 Traceback (most recent call last): File "/usr/local/bin/gtts-cli", line 9, in load_entry_point('gTTS==2.0.1', 'console_scripts', 'gtts-cli')() File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 722, in call return self.main(args, kwargs) File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 697, in main rv = self.invoke(ctx) File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 895, in invoke return ctx.invoke(self.callback, ctx.params) File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 535, in invoke return callback(args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/gtts/cli.py", line 194, in tts_cli tts.write_to_fp(output) File "/usr/local/lib/python2.7/dist-packages/gtts/tts.py", line 187, in write_to_fp part_tk = self.token.calculate_token(part) File "/usr/local/lib/python2.7/dist-packages/gtts_token/gtts_token.py", line 28, in calculate_token seed = self._get_token_key() File "/usr/local/lib/python2.7/dist-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' Is it possible this is caused by a modification in Google Translate HTTP response? I tried with other google translate python apps and they also fail to find the token...

Best regards, Thomas

Yes It is not working for me too...

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

chathurangawijetunge commented 6 years ago

Yes it's not working

ElCresh commented 6 years ago

Yes. Same issue for me on RPi3.

letitbe1503 commented 6 years ago

I'm not expert in reg expression so i change like this and working now

/usr/local/lib/python2.7/dist-packages/gtts_token/gtts_token.py comment 3 line and add 6 line

#        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)
        lineStr = line
        lineStr = lineStr[lineStr.index("TKK='")+5:len(lineStr)]
        lineStr = lineStr[0:lineStr.index("'")]
        lineArr =  lineStr.split(".")
        a =  lineArr[0]
        b =  lineArr[1]

etc. I have another Google Text-to-Speech app develop by delphi I this not change anything now my app still work

ghost commented 6 years ago

I'm not expert in reg expression so i change like this and working now

/usr/local/lib/python2.7/dist-packages/gtts_token/gtts_token.py comment 3 line and add 6 line

#        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)
        lineStr = line
        lineStr = lineStr[lineStr.index("TKK='")+5:len(lineStr)]
        lineStr = lineStr[0:lineStr.index("'")]
        lineArr =  lineStr.split(".")
        a =  lineArr[0]
        b =  lineArr[1]

etc. I have another Google Text-to-Speech app develop by delphi I this not change anything now my app still work

Thank You It solved my problem Can you explain the problem?

rupdev commented 6 years ago

Thank you so much for trying to fix this issue. As a newbie I really don't know how to apply letitbe1503 solution. gTTS is the best.

Edit: Actually, I've tried and it's really easy to implement.

rupdev commented 6 years ago

I'm not expert in reg expression so i change like this and working now

/usr/local/lib/python2.7/dist-packages/gtts_token/gtts_token.py comment 3 line and add 6 line

#        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)
        lineStr = line
        lineStr = lineStr[lineStr.index("TKK='")+5:len(lineStr)]
        lineStr = lineStr[0:lineStr.index("'")]
        lineArr =  lineStr.split(".")
        a =  lineArr[0]
        b =  lineArr[1]

etc. I have another Google Text-to-Speech app develop by delphi I this not change anything now my app still work

Thank you for that!

ilevn commented 6 years ago

I just wanted to drop by and say that I've provided a regex based solution that is both faster and cleaner compared to regular list indexing.

parsed = re.search("(?:TKK='(?:(\d+)\.(\d+))';)", line)
a, b = parsed.groups()

Cheers

chathurangawijetunge commented 6 years ago

When will this be fixed in gTTS Eg:- in pip3 install gTTS

ilevn commented 6 years ago

That's hard to say as this exception is the result of a faulty regex handler in another repo (for which I've submitted a pull request for) that is rather inactive as of now.

However, in the meantime you can monkey-patch the affected function like so:

import calendar
import time
import math
import re
import requests
from gtts import gTTS
from gtts_token.gtts_token import Token

def _patch_faulty_function(self):
    if self.token_key is not None:
        return self.token_key

    timestamp = calendar.timegm(time.gmtime())
    hours = int(math.floor(timestamp / 3600))

    response = requests.get("https://translate.google.com/")
    line = response.text.split('\n')[-1]

    parsed = re.search("(?:TKK='(?:(\d+)\.(\d+))';)", line)
    a, b = parsed.groups()

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

# Monkey patch faulty function.
Token._get_token_key = _patch_faulty_function

# Then call it normally.
tts = gTTS('hello')
tts.save('hello.mp3')

I hope that helps.

Guy-Dipietro-Bose commented 6 years ago

I've likewise put up a PR to address the issue in gtts_token: https://github.com/Boudewijn26/gTTS-token/pull/8

ggqq12356 commented 6 years ago

i have same problem today

[My solution]
I checked the code of line 64, in gtts_token.py

and i try to print(a), but it show a None type

so i repair that bug in line 64:

# bug
# a = re.search(“a\\\\x3d(-?\d+);”, tkk_expr).group(1)
# b = re.search(“b\\\\x3d(-?\d+);”, tkk_expr).group(1)

# debug
tkk = tkk_expr.replace(“\’”, “”).split(“;”)[0].split(“=”)[1]
a = tkk.split(“.”)[0]
b = tkk.split(“.”)[1]
— — — — — — — — — — — — — — — — — — — — — — — — — — — — and it’s work~

Guy-Dipietro-Bose commented 6 years ago

Fix was merged into gtts-token. This should be fixed now.

Boudewijn26 commented 6 years ago

gTTS-token 1.1.2 has just been released. Thanks a lot for the work everyone. It's good to see something I've created years ago still being cared about.

ilevn commented 6 years ago

And thank you for putting in all the work to create something like this (Btw, the link you posted points to the management page of your project and 403s respectively)

pndurette commented 6 years ago

@Boudewijn26 You and @guydipietro both rock! Amazing!

Thanks for your help everyone, e.g. @tdi-odoo, @letitbe1503 , @ilevn!

🙌

pndurette commented 6 years ago

Re-ran Travis build on master, all good!

luapp commented 6 years ago

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)
Boudewijn26 commented 6 years ago

Hi Creator-360,

The error indicated that the argument 'C:\test.mp3' is invalid. Perhaps gTTS expects forward slashes: 'C:/test.mp3'. This appears to be unrelated to the issues other people were having, those triggered AttributeError: 'NoneType' object has no attribute 'group'

Hoeby commented 6 years ago

I found the gtts token somewhere else. But changed it to this Unfortunally it doesn't work. Does somebody have an idea what i am doing wrong?

        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)
            lineStr = line
            lineStr = lineStr[lineStr.index("TKK='")+5:len(lineStr)]
            lineStr = lineStr[0:lineStr.index("'")]
            lineArr =  lineStr.split(".")
            a = lineArr[0]
            b = lineArr[1]

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

        self.token_key = result
        return result
Akavall commented 5 years ago

It is not working for me. The main problem seems to be the response that I get after:

response = requests.get("https://translate.google.com/")

This certainly looks wrong,

In [6]: line = response.text.split('\n')[-1]

In [7]: line
Out[7]: '</script>      </body> </html>'

This was working fine yesterday.

n8wachT commented 5 years ago

This was working fine yesterday.

Stopped working for me too over night, glad I'm not the only one

rupdev commented 5 years ago

This was working fine yesterday.

Stopped working for me too over night, glad I'm not the only one

Yes, sadly gTTS has stopped working. the 'NoneType' object has no attribute 'groups' issue again. Anyone else?

intermediatech commented 5 years ago

Try this.. It works now.

import calendar
import time
import math
import re
import requests
from gtts import gTTS
from gtts_token.gtts_token import Token

def _patch_faulty_function(self):
    if self.token_key is not None:
        return self.token_key

    timestamp = calendar.timegm(time.gmtime())
    hours = int(math.floor(timestamp / 3600))

    results = requests.get("https://translate.google.com/")
    tkk_expr = re.search("(tkk:*?'\d{2,}.\d{3,}')", results.text).group(1)
    tkk = re.search("(\d{5,}.\d{6,})", tkk_expr).group(1)

    a , b = tkk.split('.')

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

# Monkey patch faulty function.
Token._get_token_key = _patch_faulty_function

# Then call it normally.
tts = gTTS("Hello World")
tts.save('hello.mp3')

Worked on code by @ilevn Thanks @ilevn

kaushaljjw206 commented 5 years ago

Try this.. It works now.

import calendar
import time
import math
import re
import requests
from gtts import gTTS
from gtts_token.gtts_token import Token

def _patch_faulty_function(self):
    if self.token_key is not None:
        return self.token_key

    timestamp = calendar.timegm(time.gmtime())
    hours = int(math.floor(timestamp / 3600))

    results = requests.get("https://translate.google.com/")
    tkk_expr = re.search("(tkk:*?'\d{2,}.\d{3,}')", results.text).group(1)
    tkk = re.search("(\d{5,}.\d{6,})", tkk_expr).group(1)

    a , b = tkk.split('.')

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

# Monkey patch faulty function.
Token._get_token_key = _patch_faulty_function

# Then call it normally.
tts = gTTS("Hello World")
tts.save('hello.mp3')

Worked on code by @ilevn Thanks @ilevn

Thank You for the solution Its working now

Akavall commented 5 years ago

This issue looks to be gone in gTTS-token==1.1.3

Upgrading from 1.1.2 to 1.1.3 fixed the issue.

pip install gTTS-token --upgrade

Boudewijn26 commented 5 years ago

gTTS-token 1.1.3 indeed contains the fixes for the latest Google Translate update. Please update and verify it's working as expected

bretan12 commented 5 years ago

When i run the below code

from gtts import gTTS import os

tts = gTTS('Hello World',lang='en') tts.save('good.mp3') os.system('good.mp3')

I get this error

Traceback (most recent call last): tts = gTTS('Hello World',lang='en') File "C:\Program Files (x86)\Python36-32\lib\site-packages\gtts\tts.py", line 105, in init for k, v in locals().items(): RuntimeError: dictionary changed size during iteration

I don't have any idea of how I should solve this problem. Please help.

mihirbhavsar commented 5 years ago

Traceback (most recent call last): File "/home/pi/GassistPi/src/main.py", line 692, in Myassistant().main() File "/home/pi/GassistPi/src/main.py", line 554, in main radio(str(usrcmd).lower()) File "/home/pi/GassistPi/src/actions.py", line 282, in radio say("Tuning into " + conv) File "/home/pi/GassistPi/src/actions.py", line 229, 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 59, in _get_token_key lineStr = lineStr[lineStr.index("TKK='")+5:len(lineStr)] ValueError: substring not found

any solution for this error?

Tobils commented 5 years ago

Try this.. It works now.

import calendar
import time
import math
import re
import requests
from gtts import gTTS
from gtts_token.gtts_token import Token

def _patch_faulty_function(self):
    if self.token_key is not None:
        return self.token_key

    timestamp = calendar.timegm(time.gmtime())
    hours = int(math.floor(timestamp / 3600))

    results = requests.get("https://translate.google.com/")
    tkk_expr = re.search("(tkk:*?'\d{2,}.\d{3,}')", results.text).group(1)
    tkk = re.search("(\d{5,}.\d{6,})", tkk_expr).group(1)

    a , b = tkk.split('.')

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

# Monkey patch faulty function.
Token._get_token_key = _patch_faulty_function

# Then call it normally.
tts = gTTS("Hello World")
tts.save('hello.mp3')

Worked on code by @ilevn Thanks @ilevn

I have same issue and fortunately its work for me, thanks for sharing. thanks @ilevn

DiegoBneiNoah commented 4 years ago

When will this be fixed in gTTS Eg:- in pip3 install gTTS

I trying gtts-cli -l pt -f file.txt -o output.mp3 and got message from import gtts from tts_cli not found. I had lost the module and making pip3 gTTS I could get it working again. ThankX!