Skip to content

Commit 765b068

Browse files
committed
using languages enum now + kitten improvement
1 parent 1a9e4d8 commit 765b068

File tree

3 files changed

+153
-253
lines changed

3 files changed

+153
-253
lines changed

scratchattach/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from .other.other_apis import *
1111
from .other.project_json_capabilities import ProjectBody, get_empty_project_pb, get_pb_from_dict, read_sb3_file, download_asset
1212
from .utils.encoder import Encoding
13-
from .utils.supportedlangs import TranslateSupportedLangs as TSLangs
13+
from .utils.supportedlangs import Languages
1414

1515
from .site.activity import Activity
1616
from .site.backpack_asset import BackpackAsset

scratchattach/other/other_apis.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from ..utils import commons
77
from ..utils.exceptions import BadRequest
88
from ..utils.requests import Requests as requests
9-
from ..utils.supportedlangs import tts_lang, TSL_CODES, TSL_NAMES
9+
from ..utils.supportedlangs import Languages
1010

1111

1212
# --- Front page ---
@@ -148,16 +148,17 @@ def scratch_team_members() -> dict:
148148
return json.loads(text)
149149

150150

151-
def translate(language: str, text: str = "hello"):
152-
if language not in TSL_CODES:
153-
if language.lower() in TSL_CODES:
154-
language = language.lower()
155-
elif language.title() in TSL_NAMES:
156-
language = TSL_CODES[TSL_NAMES.index(language.title())]
157-
else:
158-
warnings.warn(f"'{language}' is probably not a supported language")
151+
def translate(language: str | Languages, text: str = "hello"):
152+
if language.lower() not in Languages.all_of("code", str.lower):
153+
if language.lower() in Languages.all_of("name", str.lower):
154+
language = Languages.find(language.lower(), apply_func=str.lower).code
155+
156+
lang = Languages.find(language, "code", str.lower)
157+
if lang is None:
158+
raise ValueError(f"{language} is not a supported translate language")
159+
159160
response_json = requests.get(
160-
f"https://translate-service.scratch.mit.edu/translate?language={language}&text={text}").json()
161+
f"https://translate-service.scratch.mit.edu/translate?language={lang.code}&text={text}").json()
161162

162163
if "result" in response_json:
163164
return response_json["result"]
@@ -182,22 +183,29 @@ def text2speech(text: str = "hello", gender: str = "female", language: str = "en
182183
gender = ("male", .84)
183184
elif gender == "kitten":
184185
gender = ("female", 1.41)
186+
split = text.split(' ')
187+
text = ''
188+
for token in split:
189+
if token.strip() != '':
190+
text += "meow "
185191
else:
186192
gender = ("female", 1)
187193

188-
if language not in TSL_NAMES:
189-
if language.lower() in TSL_NAMES:
190-
language = language.lower()
194+
og_lang = language
195+
if isinstance(language, Languages):
196+
language = language.value.tts_locale
191197

192-
elif language.title() in TSL_CODES:
193-
language = TSL_NAMES[TSL_CODES.index(language.title())]
198+
if language is None:
199+
raise ValueError(f"Language '{og_lang}' is not a supported tts language")
194200

195-
lang = tts_lang(language.title())
196-
if lang is None:
197-
warnings.warn(f"Language '{language}' is probably not a supported language")
198-
else:
199-
language = lang["speechSynthLocale"]
201+
if language.lower() not in Languages.all_of("tts_locale", str.lower):
202+
if language.lower() in Languages.all_of("name", str.lower):
203+
language = Languages.find(language.lower(), apply_func=str.lower).tts_locale
204+
205+
lang = Languages.find(language, "tts_locale")
206+
if lang is None or language is None:
207+
raise ValueError(f"Language '{og_lang}' is not a supported tts language")
200208

201209
response = requests.get(f"https://synthesis-service.scratch.mit.edu/synth"
202-
f"?locale={language}&gender={gender[0]}&text={text}")
210+
f"?locale={lang.tts_locale}&gender={gender[0]}&text={text}")
203211
return response.content, gender[1]

0 commit comments

Comments
 (0)