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