forked from eisneim/cytron_tts_gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actionHandlers.py
executable file
·147 lines (129 loc) · 3.65 KB
/
actionHandlers.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
from baidutts import Baidutts
import time
import shutil
from os.path import join
import uuid
import re
ttsInstance = None
MAX_TEX_LEN = 2048
def end_app(ctx, payload, _):
ctx.running = 0
def get_token(ctx, payload, action):
global ttsInstance
ttsInstance = Baidutts(payload["appid"], payload["appsecret"])
err, data = ttsInstance.get_access_token()
if err:
ctx.queue.put({
"type": "GET_TOKEN_ERROR",
"payload": err
})
else:
# save appid and secret
ctx.config.set("appid", payload["appid"])
ctx.config.set("appsecret", payload["appsecret"])
ctx.config.set("token", data["access_token"])
expireTime = time.time() + data["expires_in"]
ctx.config.set("expireTime", expireTime)
# save to local file
ctx.config.save()
ctx.queue.put({
"type": "GET_TOKEN",
"payload": data,
})
def splitText(text, maxBytes):
# remove extra newline
text = re.sub(r"\n+", "\n", text)
# every chinese char == 3 bytes
maxChineseChar = maxBytes // 3 + 1
idx = 0
parts = []
while idx < len(text):
parts.append(text[idx : (idx + maxChineseChar)])
idx += maxChineseChar
return parts
def postRequest(ctx, payload, action):
global ttsInstance
if not ttsInstance:
ttsInstance = Baidutts(ctx.config.get("appid"), ctx.config.get("appsecret"))
token = ctx.config.get("token")
cuid = ctx.config.get("cuid")
tex = payload["text"]
fileName = str(uuid.uuid1()) + ".mp3"
filePath = join(payload["dest"], fileName)
# check if text is too long, if so, split it
if len(tex.encode("utf-8")) < MAX_TEX_LEN:
err, res = ttsInstance.t2a(payload["text"],
token,
cuid=cuid,
spd=payload["spd"],
pit=payload["pit"],
per=payload["per"],
vol=payload["vol"])
if err:
ctx.queue.put({
"type": "POST_REQUEST_ERROR",
"payload": err
})
return
# should save to mp3 file
# http://stackoverflow.com/questions/13137817/how-to-download-image-using-requests
with open(filePath, "wb") as fout:
# res.raw.decode_content = True
shutil.copyfileobj(res.raw, fout)
else:
textList = splitText(tex, MAX_TEX_LEN)
rawResList = []
for idx, tx in enumerate(textList):
err, res = ttsInstance.t2a(tx,
token,
cuid=cuid,
spd=payload["spd"],
pit=payload["pit"],
per=payload["per"],
vol=payload["vol"])
time.sleep(1)
# save raw resonponse if respons is 200
reqeustSuccess = res and res.status_code == 200
if reqeustSuccess:
rawResList.append(res)
else:
while not reqeustSuccess:
print(" 400 error, retry")
err, res = ttsInstance.t2a(tx,
token,
cuid=cuid,
spd=payload["spd"],
pit=payload["pit"],
per=payload["per"],
vol=payload["vol"])
reqeustSuccess = res and res.status_code == 200
if reqeustSuccess:
rawResList.append(res)
if err:
ctx.queue.put({
"type": "POST_REQUEST_ERROR",
"payload": err
})
return
ctx.queue.put({
"type": "POST_REQUEST_PROGRESS",
"payload": (idx + 1) / len(textList),
})
# concat all mp3 binary
with open(filePath, "wb") as fout:
# res.raw.decode_content = True
# shutil.copyfileobj(binary, fout)
for res in rawResList:
for chunk in res.iter_content(chunk_size=MAX_TEX_LEN):
fout.write(chunk)
ctx.queue.put({
"type": "POST_REQUEST_DONE",
"payload": {
"filePath": filePath,
},
})
handlers = {
"END_APP": end_app,
"GET_TOKEN": get_token,
"POST_REQUEST": postRequest,
}