Skip to content

Commit 0821734

Browse files
committed
Added language filtering to get_available_voices()
1 parent 4a58d4d commit 0821734

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

ivona_api/ivona_api.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,27 @@ def set_voice(self, voice_name, language):
7676
self._voice_name = voice_name
7777
self._language = language
7878

79-
def get_available_voices(self):
79+
def get_available_voices(self, filter_language=None):
8080
"""
8181
Returns a list of available voices, via 'ListVoices' endpoint
8282
http://developer.ivona.com/en/speechcloud/actions.html#ListVoices
8383
"""
8484
endpoint = urljoin(
8585
IVONA_REGION_ENDPOINTS[self.region], 'ListVoices',
8686
)
87-
r = requests.get(endpoint, auth=self._aws4auth)
87+
if filter_language:
88+
if not any([v['Language'] == filter_language
89+
for v in self.available_voices]):
90+
raise ValueError("Incorrect language.")
91+
92+
data = {
93+
'Voice': {
94+
'Language': filter_language,
95+
},
96+
}
97+
r = requests.post(endpoint, auth=self._aws4auth, json=data)
98+
else:
99+
r = requests.get(endpoint, auth=self._aws4auth)
88100

89101
if 'x-amzn-ErrorType' in r.headers:
90102
raise IvonaAPIException(r.headers['x-amzn-ErrorType'])

ivona_api/test/test_ivona_api.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,29 @@ def test_init(auth_keys):
4848
def test_available_voices(auth_keys):
4949
"""Test getting available voices"""
5050
ivona_api = IvonaAPI(auth_keys[0], auth_keys[1])
51-
voices = ivona_api.available_voices
5251

52+
voices = ivona_api.available_voices
5353
assert len(voices) > 1
5454

5555
# Make sure that default voice is available
5656
assert any([v['Name'] == 'Salli' and v['Language'] == 'en-US'
5757
for v in voices])
5858

5959

60-
def test_text_to_speech_custom_voice(auth_keys):
61-
"""Test setting custom voice"""
60+
@flaky
61+
def test_available_voices_with_filter(auth_keys):
62+
"""Test getting available voices with filtering"""
6263
ivona_api = IvonaAPI(auth_keys[0], auth_keys[1])
6364

6465
with pytest.raises(ValueError):
65-
with tempfile.NamedTemporaryFile() as temp_file:
66-
ivona_api.text_to_speech(
67-
str(uuid4()), temp_file,
68-
voice_name=str(uuid4()),
69-
)
66+
ivona_api.get_available_voices(str(uuid4()))
67+
68+
voices = ivona_api.get_available_voices('en-US')
69+
assert len(voices) > 1
70+
71+
# Make sure that default voice is available
72+
assert any([v['Name'] == 'Salli' and v['Language'] == 'en-US'
73+
for v in voices])
7074

7175

7276
@flaky
@@ -86,3 +90,15 @@ def test_text_to_speech(auth_keys, voice_name, voice_language, content,
8690
ivona_api.text_to_speech(content, temp_file)
8791

8892
assert filecmp.cmp(org_file, temp_file.name)
93+
94+
95+
def test_text_to_speech_custom_voice(auth_keys):
96+
"""Test setting custom voice"""
97+
ivona_api = IvonaAPI(auth_keys[0], auth_keys[1])
98+
99+
with pytest.raises(ValueError):
100+
with tempfile.NamedTemporaryFile() as temp_file:
101+
ivona_api.text_to_speech(
102+
str(uuid4()), temp_file,
103+
voice_name=str(uuid4()),
104+
)

0 commit comments

Comments
 (0)