Update scraper/voicevox.py
Browse files- scraper/voicevox.py +16 -9
scraper/voicevox.py
CHANGED
@@ -1,15 +1,21 @@
|
|
1 |
import requests
|
2 |
import time
|
3 |
-
from functools import lru_cache
|
4 |
-
|
5 |
|
6 |
class StatusError(Exception):
|
7 |
pass
|
8 |
|
9 |
-
|
10 |
-
|
|
|
|
|
11 |
if speaker is None:
|
12 |
speaker = 1
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
url = f"https://api.tts.quest/v3/voicevox/synthesis?text={text}&speaker={speaker}"
|
14 |
resp = requests.get(url)
|
15 |
resp.raise_for_status()
|
@@ -19,9 +25,12 @@ def synthesis(text, speaker = None):
|
|
19 |
raise StatusError("Unexpected status")
|
20 |
|
21 |
resp = requests.get(data["mp3DownloadUrl"])
|
22 |
-
|
23 |
|
24 |
-
|
|
|
|
|
|
|
25 |
|
26 |
def check_audio_status(url):
|
27 |
is_ready = False
|
@@ -30,8 +39,6 @@ def check_audio_status(url):
|
|
30 |
resp.raise_for_status()
|
31 |
data = resp.json()
|
32 |
is_ready = data["isAudioReady"]
|
33 |
-
time.sleep(
|
34 |
|
35 |
return is_ready
|
36 |
-
|
37 |
-
|
|
|
1 |
import requests
|
2 |
import time
|
|
|
|
|
3 |
|
4 |
class StatusError(Exception):
|
5 |
pass
|
6 |
|
7 |
+
# Custom cache dictionary
|
8 |
+
cache = {}
|
9 |
+
|
10 |
+
def synthesis(text, speaker=None):
|
11 |
if speaker is None:
|
12 |
speaker = 1
|
13 |
+
|
14 |
+
# Check if the result is already in the cache
|
15 |
+
cache_key = (text, speaker)
|
16 |
+
if cache_key in cache:
|
17 |
+
return cache[cache_key]
|
18 |
+
|
19 |
url = f"https://api.tts.quest/v3/voicevox/synthesis?text={text}&speaker={speaker}"
|
20 |
resp = requests.get(url)
|
21 |
resp.raise_for_status()
|
|
|
25 |
raise StatusError("Unexpected status")
|
26 |
|
27 |
resp = requests.get(data["mp3DownloadUrl"])
|
28 |
+
audio_content = resp.content
|
29 |
|
30 |
+
# Cache the result
|
31 |
+
cache[cache_key] = audio_content
|
32 |
+
|
33 |
+
return audio_content
|
34 |
|
35 |
def check_audio_status(url):
|
36 |
is_ready = False
|
|
|
39 |
resp.raise_for_status()
|
40 |
data = resp.json()
|
41 |
is_ready = data["isAudioReady"]
|
42 |
+
time.sleep(0.5)
|
43 |
|
44 |
return is_ready
|
|
|
|