-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvoxw.py
75 lines (58 loc) · 2.04 KB
/
voxw.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
import time
import requests
from bs4 import BeautifulSoup
import re
def get_filename_from_cd(cd):
"""
Get filename from content-disposition
"""
if not cd:
return None
fname = re.findall("filename=(.+)", cd)
if len(fname) == 0:
return None
return fname[0]
def main():
session = requests.Session()
res = session.get("https://voxworker.com/ru")
res.raise_for_status()
soup = BeautifulSoup(res.text, "html.parser")
# with open(r'e:\temp\vox.htm', 'r', encoding='utf-8') as f:
# soup = BeautifulSoup(f.read(), 'html.parser')
textId = soup.select("input[name=textId]")
sessionId = soup.select("input[name=sessionId]")
data = dict(
textId=textId[0]["value"],
sessionId=sessionId[0]["value"],
voice="rh-anna",
speed="1.0",
pitch="1.0",
text="Вы можете менять ударение знаком плюс. Например: хлоп+ок в ладоши или белый хл+опок.",
)
res = session.post("https://voxworker.com/ru/ajax/convert", data=data)
res.raise_for_status()
resj = res.json()
if resj["status"] == "notify":
print(f"FAIL: {resj['error']}, {resj['errorText']}")
return
statusAttemptCount = 0
while statusAttemptCount < 60 and resj["status"] == "queue":
res = session.get(f'https://voxworker.com/ru/ajax/status?id={resj["taskId"]}')
res.raise_for_status()
resj = res.json()
statusAttemptCount += 1
time.sleep(1)
if statusAttemptCount == 60:
print("[FAIL] Conversion error")
return
if resj["status"] != "ok":
print(f"FAIL: {resj['error']}, {resj['errorText']}")
return
if resj["status"] == "ok":
print(resj["downloadUrl"])
res = session.get(resj["downloadUrl"])
res.raise_for_status()
filename = get_filename_from_cd(res.headers.get("content-disposition"))
open(filename.strip('"'), "wb").write(res.content)
if __name__ == "__main__":
main()