platypush/platypush/plugins/tts/__init__.py

51 lines
1.3 KiB
Python
Raw Normal View History

2017-12-22 03:14:19 +01:00
import subprocess
import urllib.parse
from platypush.plugins import Plugin, action
2017-12-22 03:14:19 +01:00
class TtsPlugin(Plugin):
2018-06-25 19:57:43 +02:00
"""
Default Text-to-Speech plugin. It leverages Google Translate.
Requires:
* **mplayer** - see your distribution docs on how to install the mplayer package
"""
2017-12-22 03:14:19 +01:00
2018-01-03 02:23:25 +01:00
def __init__(self, lang='en-gb'):
super().__init__()
self.lang=lang
@action
2019-06-11 11:53:15 +02:00
def say(self, text, language=None):
2018-06-25 19:57:43 +02:00
"""
Say a phrase
2019-06-11 11:53:15 +02:00
:param text: Phrase to say
:type text: str
2018-06-25 19:57:43 +02:00
2019-06-11 11:53:15 +02:00
:param language: Language code
:type language: str
2018-06-25 19:57:43 +02:00
"""
2019-06-11 11:53:15 +02:00
if language is None: language=self.lang
2017-12-22 03:14:19 +01:00
output = None
errors = []
cmd = ['mplayer -ao alsa -really-quiet -noconsolecontrols ' +
'"http://translate.google.com/translate_tts?{}"'
.format(urllib.parse.urlencode({
'ie' : 'UTF-8',
'client' : 'tw-ob',
2019-06-11 11:53:15 +02:00
'tl' : language,
'q' : text,
2017-12-22 03:14:19 +01:00
}))]
try:
return subprocess.check_output(
2017-12-22 03:14:19 +01:00
cmd, stderr=subprocess.STDOUT, shell=True).decode('utf-8')
except subprocess.CalledProcessError as e:
raise RuntimeError(e.output.decode('utf-8'))
2017-12-22 03:14:19 +01:00
2018-06-25 19:57:43 +02:00
2017-12-22 03:14:19 +01:00
# vim:sw=4:ts=4:et: