From 19f69ea2dea4ed91ab586218a20df5424dea992d Mon Sep 17 00:00:00 2001 From: Ian Adam Naval Date: Thu, 22 May 2014 04:59:41 -0700 Subject: [PATCH] Initial commit --- .gitignore | 1 + keypress.py | 63 ++++++ server.py | 100 +++++++++ static/mic-animate.gif | Bin 0 -> 2995 bytes static/mic-slash.gif | Bin 0 -> 807 bytes static/mic.gif | Bin 0 -> 492 bytes templates/fancyshmancyapi.html | 369 +++++++++++++++++++++++++++++++++ 7 files changed, 533 insertions(+) create mode 100644 .gitignore create mode 100644 keypress.py create mode 100644 server.py create mode 100644 static/mic-animate.gif create mode 100644 static/mic-slash.gif create mode 100644 static/mic.gif create mode 100644 templates/fancyshmancyapi.html 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 0000000000000000000000000000000000000000..afe0c9f17160d34c7b81512d22265f459e3cf5ec GIT binary patch literal 2995 zcmeH}X;9O59>)J6Tp`2+2;mgMAtGWV2~Y&v5)KKMQV+~pb%Rg`TiV6W+ER7p2nmrx zZU_*L1R^&iP>w(nLlTi&PPtkL0TdKhmMPXQ5H^%L`>r$HSGzO&ef4|&eCGK)KR^Fd zZYRQU01oI50k**2TM)l3SiqKE@Rs?7ZM!Smxa-@5o7+LRx6j_&4vpWwn6UF@*3Q>? z1qB6#g@r{$MLYLOm`rAIaq&((tE8l)w6wIWtc=BCvDxhM^74v`3J!D!g^Z81SprfNhSt(Rj zi39?HP$(3MM2{Xl5{t!MU0vPX-F&v_Fqlpl*<=pmYy%Hvb10QG+_8fo2|LlIXo2qAprK; zVOT|6rF{c;G=?O3)39e;&JSo$hI@NJ^q&RW-&`_4AgrFJuhcDc0VrG+X_y(|~UCyg9(v52w%#GzOF zz(Lz6fTb&=qFjYBj2Ne|1P!PI(_+iOK$b~Z%RT?oBn#bPnt+-n%)LDE3=H7yrOE?fXUZ{cz<#zu42#?LK3*ZLw5M$^ z;TcuJqs0OtpbB|r2IZsZEJPXUf5+8Jxle_*Ifq{AK~jjn<}Y>$ zg&~p-f8{S252R`72@9DTFpA@A?gLyr)-_xZv7l* zgVTS2q~}x(+hR2yc)m9KrP0gpnufWCI3}J0&X=mKtZjzVGA`h8T*n&`h6haEMnS%` z9SemS;zKv5#Ag%KLk$AV`MJ%ReV=-ICXy!z)hFN**HA1knq?G}A+jU4L*zahPysq5clG=1Ip!;J3gVRgLLxFWa=|A4 z_tce0#KN@H0g$>D3`?Y`)J64x04jAALp<`?d6p#)D~l}4Yh#a8HC7&}%O$3A+ltk* znkLK0=_#}7m?Dsp3AvS$Dpkzb$=&oV={kXc7i{x0rxLO%T%J_EvgrRBH zLHzt@B!4?`oMOR|ZZDzRWDv8$(0|mf|F7`>c!huW_wnz|q{0C1UKP0jtRB<)+hTTN zSdFdk_1oEA(%rhJ-zk-Thl@Kp_ZD+%3gsi070J1ZN%+8vwW6_3#nSYKe1x&~dT|*| zvR53>J`lSAf!H4B{5@G8oLFOCPNUkUvD z5pf0o{H)SYnw-pXn(ZiKo$3hP nbc)h8?KEVa)kM={cztecRXyEGPYXLXUrjadZ|27;t$O?mf!}_) literal 0 HcmV?d00001 diff --git a/static/mic-slash.gif b/static/mic-slash.gif new file mode 100644 index 0000000000000000000000000000000000000000..227e427fdff814629c6b4f2c219548d49be58ee8 GIT binary patch literal 807 zcmZ?wbhEHbG-5DfIF`Who7wpni_33Tm!E8|zu8=WvAcceboJjJ-^PXW=FOWwfBu363l=V1xMgCvu5quwd>Zc`#W><#*G^{Z{ECh>(*`Cw(Z=xbJwn2d-m+vyLa!tef#$B-~Vsz zp#ujF96WgN*s)`$PMtb;?%clv7cO4BcCqXIFPmZ(sj} ziIXNznL2IyjG41$&zU=K{(^-w;=|$>^#t-UNG*@^_Y7{c;$>jqSrlYoY-noH63Dfc zLuFx{fwq>Gwux=i=B-?Ti~LNqHLh#v#|3U-;I&%rVWf5ax`FD!dzT&>UDG%v&9asE z^_wRe3WCgAIUg)0Jj5MaU_eS<)f#Qg%myhwXCRoc&kgaIpW8KPn}+U zvJP2g9^07Mg&kuPj&K}pa&C-LY}(Us@X;y-Rm-N9UmN%v*n?Z+qW+!y+YrPz0%rQPb8)*yG=YHm(})y`zbq@ zm`=h0fuilfQ@BiHB~EE2d#un9$dYAf(qQ9NU9jV7(gEp-JyMSLf?>-&SoxI?sycj_ czJha-mdfVr`{(xV{{H^K;cof;ngR^g0FXw#tN;K2 literal 0 HcmV?d00001 diff --git a/static/mic.gif b/static/mic.gif new file mode 100644 index 0000000000000000000000000000000000000000..ea72fc0af7f40df06b33208d188d3c6dbd836287 GIT binary patch literal 492 zcmZ?wbhEHbG-5DfSZd5LbLPxhvu4enJ$ufaIdkXEoi}gZ{Q2`2ELgB`;lf3W7A;=9 zc*&9_OP4NPwrtt*<;z#DT)Ar1s@1DkuUWHZ?b@~L)~(yPapR^KIWymjl=ZQHi( z+_`htu3dZf?Ag0_@4kKe_V3?+;J|@{2M-=QcI?!tQ|Hc|JAeNC#fuj&UAlDn^5rX6 zuH3qH>&~4!_wL=hfB*i22M-=Sdi3PUlc!IgK7aoFKXE|uCkvwpgFS-|0}z1x#K89N z!2ALa9jX2kONvhBSk1q%qQq-$)cPB1%1*zn+W%lfg^%4Sw|T*mnMNvn+{brm81t|+ zH98r|cZo1FHZidp)!NH)Ff}!?X}5PaGFJ9Y=;~pbTHD#(%%(GKZtsHG)8{QIp3}p~ zx*}VaeG$X_6m?lGCt1!#jBL72N-}M6^5U{is@#j1nVngL_2M=71)OrWo2Wz?Ih1Hnkd63=wh@^mz85jA{UpKhYmZhWO9^D!hr@BK?!*#ix~mWT3Oh5 z*>oxvyg10nTk8?P;n2{)$SnR&_Ej6hVMS$jks6CgmV-^nI_(@C8x)^S5ReVJap&ab T + + + + + + 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