Skip to content

Instantly share code, notes, and snippets.

@moriyoshi
Created May 30, 2017 01:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moriyoshi/e34ab492ebf0d2b29ba3eda39656e556 to your computer and use it in GitHub Desktop.
Save moriyoshi/e34ab492ebf0d2b29ba3eda39656e556 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import re
import json
from urllib.parse import urljoin
import urllib.request
TKK = [411121, 3613763667]
def Tj(a, b):
for c in range(0, len(b), 3):
d = b[c + 2]
if d >= 'a':
d = ord(d) - 87
else:
d = int(d)
if b[c + 1] == '+':
d = (a >> d) & 2147483647
else:
d = a << d
if b[c] == '+':
a = a + d & 4294967295
else:
a = a ^ d
return a
def Vj(a):
e = [(ord(c) if isinstance(c, str) else c) for c in a.encode('utf-8')]
a = TKK[0]
for b in e:
a += b
a = Tj(a, "+-a^+6")
a = Tj(a, "+-3^+b+-f")
a ^= TKK[1]
if a < 0:
a = (a & 2147483647) + 2147483648
a %= 1000000
return '%d.%d' % (a, a ^ TKK[0])
TRANSLATE_TTS_ENDPOINT = 'https://translate.google.com/translate_tts'
def build_speech_url(q):
params = [
('ie', 'UTF-8'),
('client', 't'),
('tl', 'ja'),
('idx', '0'),
('total', '1'),
('textlen', '%d' % len(q)),
('ttsspeed', '0.14'),
('tk', Vj(q)),
('q', q),
]
url = urljoin(TRANSLATE_TTS_ENDPOINT, '?' + '&'.join('%s=%s' % (urllib.parse.quote(k.encode('utf-8')), urllib.parse.quote(v.encode('utf-8'))) for k, v in params))
return url
# req.add_header(
# 'User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36')
# resp = urllib.request.urlopen(req)
if __name__ == '__main__':
import sys
print(build_speech_url(sys.argv[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment