gTTS: Token: regexp never finds anything

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

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 2
  • Comments: 32 (6 by maintainers)

Commits related to this issue

Most upvoted comments

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.

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.

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

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

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

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

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

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…

This was working fine yesterday.

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