Created
April 29, 2014 15:07
-
-
Save un33k/11403113 to your computer and use it in GitHub Desktop.
Keeps the startup office whisper quiet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import cgi | |
import getopt | |
import urlparse | |
import socket | |
from subprocess import call, Popen, PIPE | |
from wsgiref.util import setup_testing_defaults, request_uri | |
from wsgiref.simple_server import make_server | |
OSX_VOICES = [ | |
"alex", "kathy", "bruce", "fred", "junior", "ralph", "agnes", | |
"princess","vicki", "victoria", "albert" | |
] | |
class Whisper(object): | |
""" | |
Voice messaging for OSX | |
""" | |
WHISPER_VOLUME = 5 | |
_volume = WHISPER_VOLUME | |
_speaker = 'alex' | |
def __init__(self, *args, **kwargs): | |
self._volume = self.volume | |
@property | |
def speaker(self): | |
return self._speaker | |
@speaker.setter | |
def speaker(self, name): | |
if name.lower() in OSX_VOICES: | |
self._speaker = name.lower() | |
return self._speaker | |
@property | |
def volume(self): | |
self._volume = int(Popen(['osascript', | |
'-e', 'get (output volume of (get volume settings))'], | |
stdout=PIPE).stdout.read().strip()) | |
return self._volume | |
@volume.setter | |
def volume(self, value=5): | |
call(['osascript', '-e', 'set Volume %s' % value]) | |
self._volume = value | |
return self._volume | |
def whisper(self, message=''): | |
volume = self.volume | |
if volume <= 10: | |
self.volume(self.WHISPER_VOLUME) | |
call(['say', '-v', '%s' % self._speaker, '%s' % message]) | |
if volume <= 10: | |
self.volume(volume) | |
def voices_server(environ, start_response): | |
setup_testing_defaults(environ) | |
start_response('200 OK', [('Content-type', 'text/html')]) | |
qs = cgi.parse_qs(urlparse.urlparse(request_uri(environ, True)).query or "") | |
speaker, message = (qs.get('v') and qs['v'][0] or "alex",qs.get('m') and qs['m'][0] or "") | |
whisper = Whisper() | |
whisper.speaker = speaker | |
response_message = "" | |
if message: | |
whisper.whisper(message) | |
response_message = "<i>Say command sent.</i><br /><br />" | |
return """%s<form action="" method="get">Voice: <select name="v">%s</select><br/><br /> | |
message:<br/><textarea style="height: 100px; width:400px;" name="m">%s</textarea><br /> | |
<input type="submit" name="s" value="speak" /></form> | |
""" % (response_message, ''.join(['<option valu="%s" %s>%s</option>\n' % (speaker, | |
(speaker==v and "selected" or ""), v) for v in OSX_VOICES]), message) | |
def main(): | |
try: | |
opts, args = getopt.getopt(sys.argv[1:], "h", ["help"]) | |
except getopt.error, msg: | |
print msg, "for help use --help" | |
sys.exit(2) | |
for o, a in opts: | |
if o in ("-h", "--help"): | |
print "Usage: python voices.py x.x.x.x:port\n" | |
sys.exit(0) | |
if len(args) > 1: | |
print "incorrect usage. for help use --help" | |
sys.exit(2) | |
if len(args) == 1: | |
ip, port = args[0].split(':') | |
else: | |
print "Explicit IP and port not entered. Attempted to autodiscover IP address." | |
ip, port = (socket.gethostbyname(socket.gethostname()), 2046) | |
httpd = make_server(ip, int(port), voices_server) | |
print "Serving on %s:%s" % (ip, port) | |
httpd.serve_forever() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment