question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Token: regexp never finds anything

See original GitHub issue

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

Issue Analytics

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

github_iconTop GitHub Comments

6reactions
Boudewijn26commented, Sep 21, 2018

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.

6reactions
ilevncommented, Sep 21, 2018

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Regular expression : Match anything but full token
If your regex flavor did not support non-greedy matching, the following would do it, ... Depending on your regex favour, negative zero-width look-ahead...
Read more >
Lookahead and Lookbehind Zero-Length Assertions
The regex q(?=u)i can never match anything. It tries to match u and i at the same position. If there is a u...
Read more >
Everything you need to know about Regular Expressions
After reading this article you will have a solid understanding of what regular expressions are, what they can do, and what they can't...
Read more >
Regex Quantifier Tutorial: Greedy, Lazy, Possessive - RexEgg
A regex quantifier such as + tells the regex engine to match a certain quantity of the character, token or subexpression immediately to...
Read more >
Regular Expressions: Regexes in Python (Part 1)
This contains some useful information. span=(3, 6) indicates the portion of <string> in which the match was found. This means the same thing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found