platypush/platypush/plugins/tts/__init__.py

54 lines
1.3 KiB
Python
Raw Normal View History

2017-12-22 03:14:19 +01:00
import subprocess
import urllib.parse
from platypush.message.response import Response
from .. import Plugin
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
def say(self, phrase, lang=None):
2018-06-25 19:57:43 +02:00
"""
Say a phrase
:param phrase: Phrase to say
:type phrase: str
:param lang: Language code
:type lang: str
"""
2018-01-03 02:23:25 +01:00
if lang is None: lang=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',
'tl' : lang,
'q' : phrase,
}))]
try:
output = subprocess.check_output(
cmd, stderr=subprocess.STDOUT, shell=True).decode('utf-8')
except subprocess.CalledProcessError as e:
errors = [e.output.decode('utf-8')]
return Response(output=output, errors=errors)
2018-06-25 19:57:43 +02:00
2017-12-22 03:14:19 +01:00
# vim:sw=4:ts=4:et: