-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadtome.py
executable file
·74 lines (66 loc) · 2.07 KB
/
readtome.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
#!/usr/bin/python3
import json
import sys
import os
from html.parser import HTMLParser
import ebooklib
from ebooklib import epub
from watson_developer_cloud import TextToSpeechV1
#IBM Watson Cognitive Service API Setup
auth = json.loads(open('auth.json').read())
tts = TextToSpeechV1(
username=auth['username'],
password=auth['password'],
x_watson_learning_opt_out=True)
#Convert Epub HTML to SSML
class Parser(HTMLParser):
def __init__(self):
self.reset()
self.strict = False
self.convert_charrefs = True
self.fed = []
def handle_data(self, data):
self.fed.append(data.strip())
def handle_starttag(self, tag, attrs):
self.fed.append(self.process_tag(tag, True))
def handle_endtag(self, tag):
self.fed.append(self.process_tag(tag, False))
def get_data(self):
return ''.join(self.fed)
def feed(self, data):
super().feed(data)
return self
def process_tag(self, tag, start):
#Convert an HTML tag to SSML data
tagstart = '<' if start else '</'
if tag == 'b':
return tagstart + 'emphasis level="strong">'
elif tag == 'i':
return tagstart + 'emphasis level="strong">'
elif tag == 'p':
return '\n'
else:
return ''
def main(filename):
#Get chapter selection
if len(sys.argv) > 2:
selected = sys.argv[2]
else:
selected = -1
#Load and process epub
book = epub.read_epub(filename)
for index, chapter in enumerate(book.get_items_of_type(ebooklib.ITEM_DOCUMENT)):
if chapter.is_chapter():
html = chapter.get_content().decode()
ssml = Parser().feed(html).get_data()
if selected == str(index) or selected == 'all':
generateAudio(os.path.splitext(filename)[0], index, ssml)
elif selected == -1:
print(str(index) + ': ' + ssml[:40].replace('\n', ' ').strip() + '...\n')
def generateAudio(filename, index, ssml):
with open(filename + '-part' + str(index) + '.mp3', 'wb') as mp3:
voice = '<voice-transformation type="Custom" glottal_tension="-80%" rate="20%">'
data = tts.synthesize(voice + ssml + '</voice-transformation>', accept='audio/mp3', voice='en-US_AllisonVoice')
mp3.write(data)
if __name__ == '__main__':
main(sys.argv[1])