Skip to content

Commit

Permalink
Added language filtering to get_available_voices()
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelad committed Jun 6, 2016
1 parent 4a58d4d commit 0821734
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
16 changes: 14 additions & 2 deletions ivona_api/ivona_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,27 @@ def set_voice(self, voice_name, language):
self._voice_name = voice_name
self._language = language

def get_available_voices(self):
def get_available_voices(self, filter_language=None):
"""
Returns a list of available voices, via 'ListVoices' endpoint
http://developer.ivona.com/en/speechcloud/actions.html#ListVoices
"""
endpoint = urljoin(
IVONA_REGION_ENDPOINTS[self.region], 'ListVoices',
)
r = requests.get(endpoint, auth=self._aws4auth)
if filter_language:
if not any([v['Language'] == filter_language
for v in self.available_voices]):
raise ValueError("Incorrect language.")

data = {
'Voice': {
'Language': filter_language,
},
}
r = requests.post(endpoint, auth=self._aws4auth, json=data)
else:
r = requests.get(endpoint, auth=self._aws4auth)

if 'x-amzn-ErrorType' in r.headers:
raise IvonaAPIException(r.headers['x-amzn-ErrorType'])
Expand Down
32 changes: 24 additions & 8 deletions ivona_api/test/test_ivona_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,29 @@ def test_init(auth_keys):
def test_available_voices(auth_keys):
"""Test getting available voices"""
ivona_api = IvonaAPI(auth_keys[0], auth_keys[1])
voices = ivona_api.available_voices

voices = ivona_api.available_voices
assert len(voices) > 1

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


def test_text_to_speech_custom_voice(auth_keys):
"""Test setting custom voice"""
@flaky
def test_available_voices_with_filter(auth_keys):
"""Test getting available voices with filtering"""
ivona_api = IvonaAPI(auth_keys[0], auth_keys[1])

with pytest.raises(ValueError):
with tempfile.NamedTemporaryFile() as temp_file:
ivona_api.text_to_speech(
str(uuid4()), temp_file,
voice_name=str(uuid4()),
)
ivona_api.get_available_voices(str(uuid4()))

voices = ivona_api.get_available_voices('en-US')
assert len(voices) > 1

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


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

assert filecmp.cmp(org_file, temp_file.name)


def test_text_to_speech_custom_voice(auth_keys):
"""Test setting custom voice"""
ivona_api = IvonaAPI(auth_keys[0], auth_keys[1])

with pytest.raises(ValueError):
with tempfile.NamedTemporaryFile() as temp_file:
ivona_api.text_to_speech(
str(uuid4()), temp_file,
voice_name=str(uuid4()),
)

0 comments on commit 0821734

Please sign in to comment.