-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebnovel.py
139 lines (100 loc) · 3.69 KB
/
webnovel.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
import requests
from bs4 import BeautifulSoup
import edge_tts
import os
import asyncio
import regex as re
import patch
import ss
#keyboard immortal
chapters = ss.chapters
novel = "SS"
base_url = f"https://novelbin.com/b/shadow-slave/"
# base_url = f"https://novelbin.com/b/keyboard-immortal-novel/"
location = novel.lower()
chapters_lot = {
"KI":2,
"SS":3
}
voice_list = {
"KI":"Microsoft Andrew Online (Natural) - English (United States)",
"SS":"Microsoft Steffan Online (Natural) - English (United States)"
}
#avaliable Good Voices
# Microsoft Ryan Online (Natural) - English (United Kingdom)
# Microsoft AndrewMultilingual Online (Natural) - English (United States)
# Microsoft Christopher Online (Natural) - English (United States)
# Microsoft Ava Online (Natural) - English (United States)
# Microsoft Steffan Online (Natural) - English (United States)
# Microsoft Andrew Online (Natural) - English (United States)
def main(chapter):
new_text = ""
try:
chapter = chapter.lower()
url = f"{base_url}{chapter}"
response = requests.get(url)
if response.status_code == 404 :
return "done"
soup = BeautifulSoup(response.content, "html.parser")
text = soup.find_all("p")
for paragraph in text:
paragraph = paragraph.get_text()
if paragraph == " Translator: Pika\n":
continue
if "1." in paragraph:
break
new_text += paragraph
if "\\" in new_text:
new_text.replace("\\","")
return new_text
except Exception as e:
return "continue"
async def tts() -> None:
voices = await edge_tts.list_voices()
os_path = os.getcwd()
for chapter in reversed(chapters):
chapter = chapter.split("-")[1]
if ":" in chapter:
chapter = chapter.replace(":", "")
if int(chapter)%int(chapters_lot[novel]) == 0:
chapters_to_convert = chapter
break
folder_path = f"{os_path}\\output_file\\{location}"
if os.path.exists(folder_path) == False:
os.makedirs(folder_path)
for i in range(0,int(chapters_to_convert),chapters_lot[novel]):
text = ""
path = f"{os_path}\\output_file\\{location}\\{novel}-{int(i)+1}-{int(i)+chapters_lot[novel]}.mp3"
if os.path.exists(path):
continue
VOICE = get_voice(novel, voices)
for a in range(i,i+chapters_lot[novel]):
chapter = chapters[a]
OUTPUT_FILE = f"{os_path}\\output_file\\{location}\\{novel}-{int(i)+1}-{int(i)+chapters_lot[novel]}.mp3"
if "-" in chapter:
text_chapter = chapter.replace("-"," ")
elif "-" in chapter:
text_chapter = chapter.replace("-"," ")
# if text == "":
# text += f"{text_chapter}. "
# else:
# text += f". {text_chapter}. "
text1 = main(chapter)
text += text1
if text1 == "done":
print(text1, chapter)
break
print(text)
if text == "done":
pass
else:
communicate = edge_tts.Communicate(text, VOICE, pitch = "+5Hz",rate = "-5%")
await communicate.save(OUTPUT_FILE)
def get_voice(novel, voices):
voice_need = voice_list[novel]
for voice in voices:
# print(voice["FriendlyName"])
if voice["FriendlyName"] == voice_need:
speak_voice = voice["ShortName"]
return speak_voice
asyncio.run(tts())