-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtts.py
150 lines (135 loc) · 4.95 KB
/
gtts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# -*- coding: utf-8 -*-
import re, requests
from token import gToken
class gTTS:
""" gTTS (Google Text to Speech): an interface to Google's Text to Speech API """
GOOGLE_TTS_URL = 'https://translate.google.com/translate_tts'
MAX_CHARS = 100 # Max characters the Google TTS API takes at a time
LANGUAGES = {
'af' : 'Afrikaans',
'sq' : 'Albanian',
'ar' : 'Arabic',
'hy' : 'Armenian',
'ca' : 'Catalan',
'zh' : 'Chinese',
'zh-cn' : 'Chinese (Mandarin/China)',
'zh-tw' : 'Chinese (Mandarin/Taiwan)',
'zh-yue' : 'Chinese (Cantonese)',
'hr' : 'Croatian',
'cs' : 'Czech',
'da' : 'Danish',
'nl' : 'Dutch',
'en' : 'English',
'en-au' : 'English (Australia)',
'en-uk' : 'English (United Kingdom)',
'en-us' : 'English (United States)',
'eo' : 'Esperanto',
'fi' : 'Finnish',
'fr' : 'French',
'de' : 'German',
'el' : 'Greek',
'ht' : 'Haitian Creole',
'hi' : 'Hindi',
'hu' : 'Hungarian',
'is' : 'Icelandic',
'id' : 'Indonesian',
'it' : 'Italian',
'ja' : 'Japanese',
'ko' : 'Korean',
'la' : 'Latin',
'lv' : 'Latvian',
'mk' : 'Macedonian',
'no' : 'Norwegian',
'pl' : 'Polish',
'pt' : 'Portuguese',
'pt-br' : 'Portuguese (Brazil)',
'ro' : 'Romanian',
'ru' : 'Russian',
'sr' : 'Serbian',
'sk' : 'Slovak',
'es' : 'Spanish',
'es-es' : 'Spanish (Spain)',
'es-us' : 'Spanish (United States)',
'sw' : 'Swahili',
'sv' : 'Swedish',
'ta' : 'Tamil',
'th' : 'Thai',
'tr' : 'Turkish',
'vi' : 'Vietnamese',
'cy' : 'Welsh'
}
def __init__(self, text, lang = 'en', debug = False):
self.debug = debug
if lang.lower() not in self.LANGUAGES:
raise Exception('Language not supported: %s' % lang)
else:
self.lang = lang.lower()
if not text:
raise Exception('No text to speak')
else:
self.text = text
# Split text in parts
if len(text) <= self.MAX_CHARS:
text_parts = [text]
else:
text_parts = self._tokenize(text, self.MAX_CHARS)
# Clean
def strip(x): return x.replace('\n', '').strip()
text_parts = [strip(x) for x in text_parts]
text_parts = [x for x in text_parts if len(x) > 0]
self.text_parts = text_parts
# Google Translate token
self.token = gToken()
def save(self, savefile):
""" Do the Web request and save to `savefile` """
with open(savefile, 'wb') as f:
self.write_to_fp(f)
f.close()
def write_to_fp(self, fp):
""" Do the Web request and save to a file-like object """
for idx, part in enumerate(self.text_parts):
payload = { 'ie' : 'UTF-8',
'q' : part,
'tl' : self.lang,
'total' : len(self.text_parts),
'idx' : idx,
'client' : 't',
'textlen' : len(part),
'tk' : self.token.calculate_token(part)}
headers = {
"Referer" : "http://translate.google.com/",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"
}
if self.debug: print(payload)
try:
r = requests.get(self.GOOGLE_TTS_URL, params=payload, headers=headers)
if self.debug:
print("Headers: {}".format(r.request.headers))
print("Reponse: {}, Redirects: {}".format(r.status_code, r.history))
r.raise_for_status()
for chunk in r.iter_content(chunk_size=1024):
fp.write(chunk)
except Exception as e:
raise
def _tokenize(self, text, max_size):
""" Tokenizer on basic roman punctuation """
punc = "¡!()[]¿?.,;:—«»\n"
punc_list = [re.escape(c) for c in punc]
pattern = '|'.join(punc_list)
parts = re.split(pattern, text)
min_parts = []
for p in parts:
min_parts += self._minimize(p, " ", max_size)
return min_parts
def _minimize(self, thestring, delim, max_size):
""" Recursive function that splits `thestring` in chunks
of maximum `max_size` chars delimited by `delim`. Returns list. """
if len(thestring) > max_size:
idx = thestring.rfind(delim, 0, max_size)
return [thestring[:idx]] + self._minimize(thestring[idx:], delim, max_size)
else:
return [thestring]
if __name__ == "__main__":
tts = gTTS(text='Hello', lang='en')
tts.save("hello.mp3")
pass