Skip to content

Commit

Permalink
fix return voices list if error saving to file
Browse files Browse the repository at this point in the history
  • Loading branch information
alekssamos committed May 20, 2022
1 parent 902c8f8 commit 52126c7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 8 additions & 2 deletions msspeech/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ async def get_voices_list(self) -> List[Dict]:
```
"""

import sys
global _voices_list
_res = {}
if len(_voices_list) > 0:
Expand All @@ -275,9 +276,11 @@ async def get_voices_list(self) -> List[Dict]:
_voices_list = _res
except json.decoder.JSONDecodeError:
_voices_list = {}
sys.stderr.write(f"MSSpeech.get_voices_list: error reading {voicesplusfilepath}")
if len(_voices_list) > 0:
return _voices_list
async with aiohttp.ClientSession(headers=self.headers) as session:
sys.stdout.write("MSSpeech.get_voices_list: downloading voice list JSON file...")
async with session.get(
# self.endpoint + "consumer/speech/synthesize/readaloud/voices/list",
# "https://eastus.tts.speech.microsoft.com/cognitiveservices/voices/list",
Expand All @@ -290,8 +293,11 @@ async def get_voices_list(self) -> List[Dict]:
) as resp:
# _voices_list = await resp.json()
_voices_list = await resp.json(content_type = "text/plain; charset=utf-8")
with open(voicesplusfilepath, "w", encoding="UTF8") as fp:
json.dump(_voices_list, fp, ensure_ascii=False, indent=2)
try:
with open(voicesplusfilepath, "w", encoding="UTF8") as fp:
json.dump(_voices_list, fp, ensure_ascii=False, indent=2)
except OSError:
sys.stderr.write(f"MSSpeech.get_voices_list: error ssaving {voicesplusfilepath}")
return _voices_list

async def synthesize(self, text: str, filename_or_buffer: Any, multivoices:bool = True) -> int:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


NAME = "msspeech"
VERSION = "3.3"
VERSION = "3.4"


with open(pathjoin(dirname(__file__), "README.md"), encoding="UTF-8") as f:
Expand Down

0 comments on commit 52126c7

Please sign in to comment.