From 7a0295675c8987617a318e3ecd9c191ed56b6311 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 22 Dec 2017 03:14:19 +0100 Subject: [PATCH] #20 Added TTS plugin --- platypush/plugins/tts/__init__.py | 33 +++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 platypush/plugins/tts/__init__.py diff --git a/platypush/plugins/tts/__init__.py b/platypush/plugins/tts/__init__.py new file mode 100644 index 00000000..c85f3d96 --- /dev/null +++ b/platypush/plugins/tts/__init__.py @@ -0,0 +1,33 @@ +import subprocess +import urllib.parse + +from platypush.message.response import Response + +from .. import Plugin + +class TtsPlugin(Plugin): + """ Default Text-to-Speech plugin. It leverages Google Translate and + requires mplayer """ + + def say(self, phrase, lang='en-gb'): + 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) + +# vim:sw=4:ts=4:et: +