Cache.py 1.1 KB

123456789101112131415161718192021222324252627
  1. import hashlib
  2. import os
  3. import logging
  4. from core.FileManager import FileManager
  5. class Cache:
  6. DEFAULT_MODULE_NAME = "default"
  7. DEFAULT_CACHE_PATH = "/tmp/jarvis/tts"
  8. DEFAULT_CACHE_EXTENSION = ".tts"
  9. def __init__(self, module_name=DEFAULT_MODULE_NAME, cache_path=DEFAULT_CACHE_PATH, cache_extension=DEFAULT_CACHE_EXTENSION):
  10. self._module_name = module_name
  11. self._cache_path = cache_path
  12. self._cache_extension = cache_extension
  13. def get_audio_file_cache_path(self, words, voice, language):
  14. # fix UnicodeEncodeError: 'ascii' codec can't encode character X in position Y
  15. words = words.encode('utf-8')
  16. md5 = hashlib.md5(words).hexdigest()
  17. filename = voice + "." + md5 + self._cache_extension
  18. cache_directory = os.path.join(self._cache_path, self._module_name, language)
  19. file_path = os.path.join(cache_directory, filename)
  20. FileManager.create_directory(cache_directory)
  21. logging.debug("Cache directory %s exists and File path for audio is: %s", cache_directory, file_path)
  22. return file_path