imsys/AwesomeTTS — pull request #19 by corpulentcoffee (2013-11-21 04:42:48+00:00)
Title: Caching Support for Google TTS Service

Ordinarily, the download URL for every unique on-the-fly `` tag is passed to `mplayer` for it to stream, and the file must be downloaded again each time the tag is encountered (e.g. for reviews of the same card).

These changes add caching support for these downloads such that when the "cache downloads from Google TTS" checkbox is enabled on the Configuration screen, the MP3s are instead downloaded to disk and the path of the downloaded MP3 is passed to `mplayer`.

Caching the files locally has the benefit of speeding up successive reviews of cards and also allows the TTS functionality to continue working when network connectivity isn't available (assuming, of course, that the given `` tag has been encountered at least once before).

The files in cache directory are handled by hashing the phrases within each `` tag after some minimal normalization (e.g. removal of excess whitespace and HTML). The cache directory can be emptied from the user interface with the "Clear Cache" button on the Configuration screen.

---

Here's a quick rundown of the changes:

* `designer/configurator.ui` increases to the height of the form, adds a checkbox to enable/disable the feature, and adds a button to clear cached MP3s
* `forms/configurator.py` is a newly-built version of the configurator form, based on the changes to `designer/configurator.ui` (n.b. I did this using a newer version of PyQt, so the diff looks more dramatic than it really should)
* `config.py` adds a new `caching` column to the `general` table (which, as of right now in this pull request, defaults to `true`), detects and updates old versions of the table, exposes the value outside of the module, and provides serialization
* `main.py` is updated to handle the initialization of the new configuration checkbox and button, including the action handler for clearing the cache
* `services/Google.py` has the new code for actually doing the caching; this includes normalizing the input, hashing the phrase, retrieving the audio and storing to disk, and adjusting the path to be passed to the `mplayer` process

I've tested these changes out on Arch Linux and Windows 7 with success.