commit 19f69ea2dea4ed91ab586218a20df5424dea992d Author: Ian Adam Naval Date: Thu May 22 04:59:41 2014 -0700 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bf814f8 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +ssl* diff --git a/keypress.py b/keypress.py new file mode 100644 index 0000000..0354cf0 --- /dev/null +++ b/keypress.py @@ -0,0 +1,63 @@ +from Xlib.display import Display +import Xlib +from Xlib import X +import Xlib.XK +import sys +import signal +import time +display = None +root = None + +def handle_event(event): + print "handle!" + if (event.type == X.KeyRelease): + send_key("x") + +# from http://shallowsky.com/software/crikey/pykey-0.1 +def send_key(emulated_key): + shift_mask = 0 # or Xlib.X.ShiftMask + window = display.get_input_focus()._data["focus"] + keysym = Xlib.XK.string_to_keysym(emulated_key) + keycode = display.keysym_to_keycode(keysym) + event = Xlib.protocol.event.KeyPress( + time = int(time.time()), + root = root, + window = window, + same_screen = 0, child = Xlib.X.NONE, + root_x = 0, root_y = 0, event_x = 0, event_y = 0, + state = shift_mask, + detail = keycode + ) + window.send_event(event, propagate = True) + event = Xlib.protocol.event.KeyRelease( + time = int(time.time()), + root = display.screen().root, + window = window, + same_screen = 0, child = Xlib.X.NONE, + root_x = 0, root_y = 0, event_x = 0, event_y = 0, + state = shift_mask, + detail = keycode + ) + window.send_event(event, propagate = True) + +def main(): + # current display + global display,root + display = Display() + root = display.screen().root + + # we tell the X server we want to catch keyPress event + root.change_attributes(event_mask = X.KeyPressMask|X.KeyReleaseMask) + # just grab the "1"-key for now + root.grab_key(10, 0, True,X.GrabModeSync, X.GrabModeSync) + + signal.signal(signal.SIGALRM, lambda a,b:sys.exit(1)) + signal.alarm(10) + while 1: + event = display.next_event() + print "event" + handle_event(event) + display.allow_events(X.AsyncKeyboard, X.CurrentTime) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/server.py b/server.py new file mode 100644 index 0000000..be05287 --- /dev/null +++ b/server.py @@ -0,0 +1,100 @@ +from flask import Flask, render_template, request +from OpenSSL import SSL +import subprocess +import re + + +cmds = { + 'open': """{program}""", + 'google': """google-chrome-stable http://google.com/#q={query}""" +} + + +programs = { + 'chrome': 'google-chrome', + 'sublime': 'subl3', + 'minecraft': 'minecraft &', + 'terminal': 'terminator' +} + +def execute(cmd): + print(cmd) + subprocess.call(cmd.split()) + + +def speak(words): + process = subprocess.Popen(['espeak'], stdin=subprocess.PIPE) + process.stdin.write(words + "\n") + process.stdin.flush() + process.stdin.close() + + +app = Flask(__name__) + + +def execute_command(cmd): + cmd = cmd.replace('okay', '').replace('please', '').strip() + + if cmd == 'computer': + speak("I have a name, ass hole.") + if cmd == 'Jarvis': + speak("yes?") + + if cmd.startswith('computer'): + cmd = cmd[9:] + if cmd.startswith('Jarvis'): + cmd = cmd[7:] + print cmd + if re.match(r'^identify', cmd.lower()): + speak("I am Jarvis, alpha version 0.1.") + elif cmd.lower().startswith('open'): + speak("Okay.") + tokens = cmd.split(' ') + program = ' '.join(tokens[1:]) + for key, value in programs.items(): + if key in program.lower(): + cmd_with_args = cmds['open'].format(program=value) + if key == 'chrome': + program = program.lower() + _, site = program.split("chrome") + cmd_with_args += site + execute(cmd_with_args) + break + elif cmd.lower().startswith('say'): + tokens = cmd.split(' ') + words = ' '.join(tokens[1:]) + speak(words) + elif cmd.lower().startswith('google'): + _, raw_query = cmd.split('Google', 1) + query = raw_query.strip().replace(' ', '+') + execute(cmds['google'].format(query=query)) + elif re.match(r'^(do|who|what|where|when|why|how)[\s\']', cmd.lower()): + speak("Let me Google that for you.") + query = cmd.strip().replace(' ', '+') + execute(cmds['google'].format(query=query)) + + +@app.route('/') +def root(): + return render_template('fancyshmancyapi.html') + + +@app.route('/command/', methods=["POST"]) +def command(): + words = request.form.get('words', '') + execute_command(words) + return "OK" + + +@app.route('/test/', methods=["POST"]) +def test(): + print(request.form) + return "OK" # request.form.get("final_transcript") + + +if __name__ == '__main__': + context = SSL.Context(SSL.SSLv23_METHOD) + context.use_privatekey_file('ssl.key') + context.use_certificate_file('ssl.crt') + app.run(debug=True, ssl_context=context) + diff --git a/static/mic-animate.gif b/static/mic-animate.gif new file mode 100644 index 0000000..afe0c9f Binary files /dev/null and b/static/mic-animate.gif differ diff --git a/static/mic-slash.gif b/static/mic-slash.gif new file mode 100644 index 0000000..227e427 Binary files /dev/null and b/static/mic-slash.gif differ diff --git a/static/mic.gif b/static/mic.gif new file mode 100644 index 0000000..ea72fc0 Binary files /dev/null and b/static/mic.gif differ diff --git a/templates/fancyshmancyapi.html b/templates/fancyshmancyapi.html new file mode 100644 index 0000000..d4751db --- /dev/null +++ b/templates/fancyshmancyapi.html @@ -0,0 +1,369 @@ + + + + + + + J.A.R.V.I.S. + + + + + + + +
+ +
+
+ + +
+
+
+
+

+ Hello, I am J.A.R.V.I.S. I am here to assist you. +

+ + + + + + + +
+
+ +
+
+ +
+ +
+    +
+
+ Powered by Google's Web + Speech API. +
+
+
+ + + + + \ No newline at end of file