class YoutubeControl(object): """Maps voice command detections to youtube controls.""" def __init__(self): """Creates an instance of `YoutubeControl`.""" self._keyboard = PyKeyboard() self._command_lookup = { "left": self._keyboard.left_key, "right": self._keyboard.right_key, "up": self._keyboard.up_key, "down": self._keyboard.down_key, "shift": self._keyboard.shift_key } def run_command(self, command): """Parses and excecuted a command.""" if len(command) == 1: self._keyboard.tap_key(command) elif command in self._command_lookup.keys(): self._keyboard.tap_key(self._command_lookup[command]) elif "+" in command: keys = command.split("+") press_list = [] for key in keys: if len(key) == 1: press_list.append(key) elif key in self._command_lookup.keys(): press_list.append(self._command_lookup[key]) else: print("Can't parse: ", command) return self._keyboard.press_keys(press_list)
def hot_key(): '''快捷键命令''' k = PyKeyboard() key_map = { "播放/暂停": [k.control_key, k.alt_key, "p"], "上一曲": [k.control_key, k.alt_key, k.left_key], "下一曲": [k.control_key, k.alt_key, k.right_key], "音量加": [k.control_key, k.alt_key, k.up_key], "音量减": [k.control_key, k.alt_key, k.down_key], "待机": ([k.control_key, k.alt_key, k.delete_key], k.left_key, k.enter_key), } data = json.loads(request.data) description = data["description"] if description not in key_map: return jsonify({"message": "找不到相应的快捷键"}) # 判断是否打开了网易云音乐 if description in ["播放/暂停", "上一曲", "下一曲", "音量加", "音量减"] and len(subprocess.run("pgrep -f netease-cloud-m", shell=True, stdout=subprocess.PIPE).stdout.split(b"\n")) <= 2: subprocess.call("netease-cloud-music &", shell=True) time.sleep(2) keys_comb = key_map[description] if isinstance(keys_comb, tuple): for item in keys_comb: time.sleep(0.5) if isinstance(item, list): k.press_keys(item) else: k.tap_key(item) elif isinstance(keys_comb, list): k.press_keys(keys_comb) else: k.tap_key(keys_comb) label.config(text=description) return jsonify({"message": 0})
def combo(*arg): theCombo = arg[:-1] theVelocity = arg[-1] # not used k = PyKeyboard() k.press_keys(theCombo)
def on_End_combo(): logging.info(30*'_' + " on_End_combo") k = PyKeyboard() #抬起功能按键Ctrl,否则End效果会变为Ctrl+End效果 k.release_key(k.control_key) k.press_keys([k.end_key, ';', k.enter_key]) return False
def upload_mac(file_path): k = PyKeyboard() m = PyMouse() file_path_heard = '/' # 利用 Command + Shift + G 组合调起文件路径输入框 k.press_keys(['Command', 'Shift', 'G']) x_dim, y_dim = m.screen_size() m.click(x_dim // 2, y_dim // 2, 1) # 复制文件路径开头的斜杠/ pyperclip.copy(file_path_heard) time.sleep(2) # 粘贴斜杠/ k.press_keys(['Command', 'V']) time.sleep(2) # 输入文件全路径进去 k.type_string(file_path) # 按Enter键 k.press_key('Return') time.sleep(2) k.press_key('Return') time.sleep(2) # 如果处于中文输入法下 输入路径后是有一个提示 需要按Enter才会输入 会消耗掉1个Enter 故增加1个Enter 容错 k.press_key('Return') time.sleep(2) logging.info("上传图片: {}成功!".format(file_path))
class SystemTools: def __init__(self, *args, **kwargs): self.k = PyKeyboard() self.screen = Wnck.Screen.get_default() self.screen.force_update() self.active_window = self.screen.get_active_window() def activate_previous_window(self): self.window_list = self.screen.get_windows_stacked() self.window_list[-2].activate(0) def copy_text(self): self.k.press_keys([self.k.control_key, 'c']) def run_for_terminal(self): self.activate_previous_window() self.copy_text() def run_for_other(self): self.copy_text() self.activate_previous_window() def run(self): if self.active_window.get_name() == "Terminal": self.run_for_terminal() else: self.run_for_other()
def input_keyevnet(self, timeout): # 循环获取当前时间,与once_time变量做时间戳做减法 while time.time() - self.once_time <= timeout: print("Pressed the %s key" % self.key) k = PyKeyboard() k.press_keys(['Command', 's']) k.press_keys(['Return']) # 按下某个键 time.sleep(5) # 每5秒间隔一次
def capture(url, save_fn="capture.png"): mouse = PyMouse() keyboard = PyKeyboard() browser = webdriver.Firefox(executable_path='/home/wws/Soft/geckodriver' ) # Get local session of firefox browser.set_window_size(1900, 900) browser.get(url) mouse.click(300, 250) # Load page # browser.execute_script(""" # (function () { # var y = 0; # var step = 100; # window.scroll(0, 0); # # function f() { # if (y < document.body.scrollHeight) { # y += step; # window.scroll(0, y); # setTimeout(f, 50); # } else { # window.scroll(0, 0); # document.title += "scroll-done"; # } # } # # setTimeout(f, 1000); # })(); # """) # browser.execute_script() # browser.save_screenshot('first.png') keyboard.press_keys([keyboard.shift_key, 'F2']) # keyboard.release_keys(["Shift","F2"]) # png = base64.b64decode(browser.execute('screenshot --fullpage')['value'].encode('ascii')) # with open('first1.png', 'wb') as f: # f.write(png) # # browser.execute("screenshot") # # keyboard.press_key(keyboard.page_down_key) # keyboard.release_key(keyboard.page_down_key) # time.sleep(1) # browser.save_screenshot('second.png') # browser.close() time.sleep(1) keyboard.type_string( "screenshot --fullpage /home/wws/workspace/git/SPIDER/Crawler-master/amazon/fullpage.png" ) keyboard.press_key(keyboard.enter_key) keyboard.release_key(keyboard.enter_key) time.sleep(1)
def callSequence(*args): m = PyMouse() k = PyKeyboard() print args #k.press_keys(args) #k.type_string(args[0]) keys = ["cmd", "a"] #callArray(keys) k.press_keys(keys)
def _press_key_command(self, keys): """Press the keys specified. Args: keys (list(str)): A list containing names of keys to press. """ kb = PyKeyboard() # tap key if there is only one key, else press keys if len(keys) == 1: kb.tap_key(keys[0]) else: kb.press_keys(keys)
def snapshot_screen(page_size): k = PyKeyboard() m = PyMouse() # k.tap_key('KEYTYPE_NEXT') p_next_page = [1414, 472] for i in range(1, page_size): time.sleep(4) # kk.press_keys(['Command','shift','3']) # time.sleep(1) m.click(p_next_page[0], p_next_page[1]) time.sleep(4) k.press_keys(['Command', 'shift', '3'])
def fuck_with_arduino(port): print("F*****g with the arduino") ser = serial.Serial(port=port, baudrate=9600, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=1, xonxoff=False, rtscts=False, dsrdtr=False, writeTimeout=2) mouse = PyMouse() keyboard = PyKeyboard() screen_size = mouse.screen_size() screen_width = (screen_size[0]) / 244 screen_height = (screen_size[1]) / 244 assert (ser.isOpen()) while True: ray = ser.readline() if (len(ray) > 2 and ray[0] == 109): mouse.move(int((ray[1] - 11) * screen_width), int((ray[2] - 11) * screen_height)) elif (len(ray) > 1 and ray[0] == ord('C')): print("mouse_down: " + str(ray[1])) pos = mouse.position() mouse.press(pos[0], pos[1], ray[1]) elif (len(ray) > 1 and ray[0] == ord('U')): print("mouse_up: " + str(ray[1])) pos = mouse.position() mouse.release(pos[0], pos[1], ray[1]) elif (len(ray) > 0 and ray[0] == ord('p')): print("PASTE M**********R") ray = ray.decode() if (len(ray) > 0): ray = ray[1:ray.find("\n")] clipboard_copy(ray) keyboard.type_string(ray) elif (len(ray) > 0 and ray[0] == ord('c')): keyboard.press_keys([ "Command" if platform == "darwin" else keyboard.control_key, "c" ]) sleep(1) replyCopy(ser) elif (len(ray) > 1 and ray[0] == ord('s')): print("Status: <" + str(ray[1]))
def upload_file(self, file): self.click(self.salon_banner_loc) k = PyKeyboard() m = PyMouse() filepath = '/' k.press_keys(['Command', 'Shift', 'G']) x_dim, y_dim = m.screen_size() m.click(x_dim // 2, y_dim // 2, 1) # 复制文件路径开头的斜杠/ pyperclip.copy(filepath) # 粘贴斜杠/ k.press_keys(['Command', 'V']) # 输入文件全路径进去 k.type_string(file) k.press_key('Return') time.sleep(2) k.press_key('Return')
def find_sql(self, database, title, sql): """查询sql""" # print(self.app.windows()) title = '无标题 @{} ({}) - 查询 - Navicat Premium'.format(database, title) find_dlg = self.app[title] # find_dlg['TabControl'].Pane.print_control_identifiers() # 获取编辑框 edit = find_dlg['TabControl'].Pane # 获取编辑窗口控件位置 rect = edit.rectangle().mid_point() mouse.click(coords=(rect.x, rect.y)) k = PyKeyboard() # 输入sql语句 k.type_string("{}".format(sql)) time.sleep(1) # 使用ctrl+r运行语句 mouse.click(coords=(rect.x, rect.y)) k.press_keys([k.control_key, 'r'])
def run(): k = PyKeyboard() english = "0x409" chinese = "0x804" user32 = ctypes.WinDLL("user32", use_last_error=True) current_windown = user32.GetForegroundWindow() thread_id = user32.GetWindowThreadProcessId(current_windown, 0) klid = user32.GetKeyboardLayout(thread_id) lid = klid & (2 ** 16 - 1) lid_hex = hex(lid) if lid_hex == chinese: k.press_keys([k.control_l_key, k.shift_l_key]) if lid_hex == english: k.press_key(k.escape_key)
def press_browser_history_nav(self, direction): """Presses the keys according to the navigation browser in history intent. Args: direction (str): 'back' or 'forward'. Returns: str: 'back' or 'forward'. """ with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: if direction == "back": k.press_keys([k.alt_l_key, k.left_key]) elif direction == "forward": k.press_keys([k.alt_l_key, k.right_key]) else: pass return direction
def set_abm_params(start_iter, sample_rates): """Set the parameters for the sandag_abm. Parameters ---------- start_iter : int The iteration to start the process from. sample_rates : str or None The sample rates for the abm in string form. """ mouse = PyMouse() board = PyKeyboard() xdim, ydim = mouse.screen_size() mouse.click(int(round(xdim * 0.57083)), int(round(ydim * 0.04952))) sleep(2) board.press_keys([board.alt_key, 'd']) board.press_keys([board.alt_key, 'n']) board.type_string('Setup Scenario') board.tap_key(board.enter_key) sleep(2) board.tap_key(board.space_key) sleep(5) # Set database write to 'No' board.tap_key(board.tab_key, n=22) board.tap_key(board.space_key) if start_iter == 1: board.tap_key(board.tab_key, n=7) else: board.tap_key(board.tab_key, n=2 + start_iter) board.tap_key(board.space_key) board.tap_key(board.tab_key, n=5 - start_iter) if sample_rates: board.tap_key(board.backspace_key) board.type_string(sample_rates) board.tap_key(board.tab_key) board.tap_key(board.space_key) sleep(1)
def forwardPress(): k = PyKeyboard() print "forward button pressed" k.press_keys([k.alt_key, k.tab_key]) time.sleep(0.5) #k.tap_key(k.keypad_keys['Enter']) k.press_keys([k.shift_key, '.']) time.sleep(0.5) k.press_keys([k.alt_key, k.tab_key])
def play(): global started k = PyKeyboard() if started == 0: #start tunes print "started" k.press_keys([k.alt_key, k.tab_key]) time.sleep(0.5) k.type_string('mplayer -ao alsa:device=hw=0,0 -playlist playlist.m3u') k.tap_key(k.keypad_keys['Enter']) k.press_keys([k.alt_key, k.tab_key]) started = 1 createPlaylist() elif started == 1: #song is playing, button pauses print "playing" k.press_keys([k.alt_key, k.tab_key]) time.sleep(0.1) k.tap_key('p') time.sleep(0.1) k.press_keys([k.alt_key, k.tab_key])
def upload_file(self, file): # 定位上传按钮 self.click(self.salon_banner_loc) k = PyKeyboard() m = PyMouse() # 模拟快捷键Command+Shift+G k.press_keys(['Command', 'Shift', 'G']) # 输入文件路径 x_dim, y_dim = m.screen_size() m.click(x_dim // 2, y_dim // 2, 1) k.type_string(file) # 前往文件 k.press_keys(['Return']) # 点击确定进行上传 k.press_keys(['Return'])
def command(com, args, tw_user=None): global e if (e.is_set()): # System Tray Icon exit must trigger this exit(0) if not com.strip() or not isinstance(com, str): return False global inactive global user_full_name global user_prefix global config_file userin.twitter_user = tw_user print("You: " + com.upper()) doc = nlp(com) h = Helper(doc) if args["verbose"]: if len(doc) > 0: print("") print("{:12} {:12} {:12} {:12} {:12} {:12} {:12} {:12}".format("TEXT", "LEMMA", "POS", "TAG", "DEP", "SHAPE", "ALPHA", "STOP")) for token in doc: print("{:12} {:12} {:12} {:12} {:12} {:12} {:12} {:12}".format(token.text, token.lemma_, token.pos_, token.tag_, token.dep_, token.shape_, str(token.is_alpha), str(token.is_stop))) print("") if len(list(doc.noun_chunks)) > 0: print("{:12} {:12} {:12} {:12}".format("TEXT", "ROOT.TEXT", "ROOT.DEP_", "ROOT.HEAD.TEXT")) for chunk in doc.noun_chunks: print("{:12} {:12} {:12} {:12}".format(chunk.text, chunk.root.text, chunk.root.dep_, chunk.root.head.text)) print("") if inactive and not (h.directly_equal(["dragonfire", "hey"]) or (h.check_verb_lemma("wake") and h.check_nth_lemma(-1, "up")) or (h.check_nth_lemma(0, "dragon") and h.check_nth_lemma(1, "fire") and h.max_word_count(2))): return True if USER_ANSWERING['status']: if com.startswith("FIRST") or com.startswith("THE FIRST") or com.startswith("SECOND") or com.startswith("THE SECOND") or com.startswith("THIRD") or com.startswith("THE THIRD"): USER_ANSWERING['status'] = False selection = None if com.startswith("FIRST") or com.startswith("THE FIRST"): selection = 0 elif com.startswith("SECOND") or com.startswith("THE SECOND"): selection = 1 elif com.startswith("THIRD") or com.startswith("THE THIRD"): selection = 2 if USER_ANSWERING['for'] == 'wikipedia': with nostderr(): search_query = USER_ANSWERING['options'][selection] try: wikiresult = wikipedia.search(search_query) if len(wikiresult) == 0: userin.say("Sorry, " + user_prefix + ". But I couldn't find anything about " + search_query + " in Wikipedia.") return True wikipage = wikipedia.page(wikiresult[0]) wikicontent = "".join([i if ord(i) < 128 else ' ' for i in wikipage.content]) wikicontent = re.sub(r'\([^)]*\)', '', wikicontent) userin.execute(["sensible-browser", wikipage.url], search_query) userin.say(wikicontent) return True except requests.exceptions.ConnectionError: userin.execute([" "], "Wikipedia connection error.") userin.say("Sorry, " + user_prefix + ". But I'm unable to connect to Wikipedia servers.") return True except Exception: return True if h.directly_equal(["dragonfire", "hey"]) or (h.check_verb_lemma("wake") and h.check_nth_lemma(-1, "up")) or (h.check_nth_lemma(0, "dragon") and h.check_nth_lemma(1, "fire") and h.max_word_count(2)): inactive = False userin.say(choice([ "Yes, " + user_prefix + ".", "Yes. I'm waiting.", "What is your order?", "Ready for the orders!", user_prefix + ", tell me your wish." ])) return True if (h.check_verb_lemma("go") and h.check_noun_lemma("sleep")) or (h.check_verb_lemma("stop") and h.check_verb_lemma("listen")): inactive = True userin.execute(["echo"], "Dragonfire deactivated. To reactivate say 'Dragonfire!' or 'Wake Up!'") userin.say("I'm going to sleep") return True if h.directly_equal(["enough"]) or (h.check_verb_lemma("shut") and h.check_nth_lemma(-1, "up")): tts_kill() print("Dragonfire quiets.") return True if h.check_wh_lemma("what") and h.check_deps_contains("your name"): userin.execute([" "], "My name is Dragonfire.", True) return True if h.check_wh_lemma("what") and h.check_deps_contains("your gender"): userin.say("I have a female voice but I don't have a gender identity. I'm a computer program, " + user_prefix + ".") return True if (h.check_wh_lemma("who") and h.check_text("I")) or (h.check_verb_lemma("say") and h.check_text("my") and check_lemma("name")): userin.execute([" "], user_full_name) userin.say("Your name is " + user_full_name + ", " + user_prefix + ".") return True if h.check_verb_lemma("open") or h.check_adj_lemma("open") or h.check_verb_lemma("run") or h.check_verb_lemma("start") or h.check_verb_lemma("show"): if h.check_text("blender"): userin.execute(["blender"], "Blender") userin.say("Blender 3D computer graphics software") return True if h.check_text("draw"): userin.execute(["libreoffice", "--draw"], "LibreOffice Draw") userin.say("Opening LibreOffice Draw") return True if h.check_text("impress"): userin.execute(["libreoffice", "--impress"], "LibreOffice Impress") userin.say("Opening LibreOffice Impress") return True if h.check_text("math"): userin.execute(["libreoffice", "--math"], "LibreOffice Math") userin.say("Opening LibreOffice Math") return True if h.check_text("writer"): userin.execute(["libreoffice", "--writer"], "LibreOffice Writer") userin.say("Opening LibreOffice Writer") return True if h.check_text("gimp") or (h.check_noun_lemma("photo") and (h.check_noun_lemma("editor") or h.check_noun_lemma("shop"))): userin.execute(["gimp"], "GIMP") userin.say("Opening the photo editor software.") return True if h.check_text("inkscape") or (h.check_noun_lemma("vector") and h.check_noun_lemma("graphic")) or (h.check_text("vectorial") and h.check_text("drawing")): userin.execute(["inkscape"], "Inkscape") userin.say("Opening the vectorial drawing software.") return True if h.check_noun_lemma("office") and h.check_noun_lemma("suite"): userin.execute(["libreoffice"], "LibreOffice") userin.say("Opening LibreOffice") return True if h.check_text("kdenlive") or (h.check_noun_lemma("video") and h.check_noun_lemma("editor")): userin.execute(["kdenlive"], "Kdenlive") userin.say("Opening the video editor software.") return True if h.check_noun_lemma("browser") or h.check_noun_lemma("chrome") or h.check_text("firefox"): userin.execute(["sensible-browser"], "Web Browser") userin.say("Web browser") return True if h.check_text("steam"): userin.execute(["steam"], "Steam") userin.say("Opening Steam Game Store") return True if h.check_text("files") or (h.check_noun_lemma("file") and h.check_noun_lemma("manager")): userin.execute(["dolphin"], "File Manager") # KDE neon userin.execute(["pantheon-files"], "File Manager") # elementary OS userin.execute(["nautilus", "--browser"], "File Manager") # Ubuntu userin.say("File Manager") return True if h.check_noun_lemma("camera"): userin.execute(["kamoso"], "Camera") # KDE neon userin.execute(["snap-photobooth"], "Camera") # elementary OS userin.execute(["cheese"], "Camera") # Ubuntu userin.say("Camera") return True if h.check_noun_lemma("calendar"): userin.execute(["korganizer"], "Calendar") # KDE neon userin.execute(["maya-calendar"], "Calendar") # elementary OS userin.execute(["orage"], "Calendar") # Ubuntu userin.say("Calendar") return True if h.check_noun_lemma("calculator"): userin.execute(["kcalc"], "Calculator") # KDE neon userin.execute(["pantheon-calculator"], "Calculator") # elementary OS userin.execute(["gnome-calculator"], "Calculator") # Ubuntu userin.say("Calculator") return True if h.check_noun_lemma("software") and h.check_text("center"): userin.execute(["plasma-discover"], "Software Center") # KDE neon userin.execute(["software-center"], "Software Center") # elementary OS & Ubuntu userin.say("Software Center") return True if h.check_lemma("be") and h.check_lemma("-PRON-") and (h.check_lemma("lady") or h.check_lemma("woman") or h.check_lemma("girl")): config_file.update({'gender': 'female'}, Query().datatype == 'gender') config_file.remove(Query().datatype == 'callme') user_prefix = "My Lady" userin.say("Pardon, " + user_prefix + ".") return True if h.check_lemma("be") and h.check_lemma("-PRON-") and (h.check_lemma("sir") or h.check_lemma("man") or h.check_lemma("boy")): config_file.update({'gender': 'male'}, Query().datatype == 'gender') config_file.remove(Query().datatype == 'callme') user_prefix = "Sir" userin.say("Pardon, " + user_prefix + ".") return True if h.check_lemma("call") and h.check_lemma("-PRON-"): title = "" for token in doc: if token.pos_ == "NOUN": title += ' ' + token.text title = title.strip() callme_config = config_file.search(Query().datatype == 'callme') if callme_config: config_file.update({'title': title}, Query().datatype == 'callme') else: config_file.insert({'datatype': 'callme', 'title': title}) user_prefix = title userin.say("OK, " + user_prefix + ".") return True # only for The United States today but prepared for all countries. Also # only for celsius degrees today. --> by Radan Liska :-) if h.is_wh_question() and h.check_lemma("temperature"): city = "" for ent in doc.ents: if ent.label_ == "GPE": city += ' ' + ent.text city = city.strip() if city: owm = pyowm.OWM("16d66c84e82424f0f8e62c3e3b27b574") reg = owm.city_id_registry() weather = owm.weather_at_id(reg.ids_for(city)[0][0]).get_weather() fmt = "The temperature in {} is {} degrees celsius" msg = fmt.format(city, weather.get_temperature('celsius')['temp']) userin.execute([" "], msg) userin.say(msg) return True if h.check_nth_lemma(0, "keyboard") or h.check_nth_lemma(0, "type"): n = len(doc[0].text) + 1 with nostdout(): with nostderr(): k = PyKeyboard() for character in com[n:]: k.tap_key(character) k.tap_key(" ") return True if h.directly_equal(["enter"]) or (h.check_adj_lemma("new") or h.check_noun_lemma("line")): with nostdout(): with nostderr(): k = PyKeyboard() k.tap_key(k.enter_key) return True if h.check_adj_lemma("new") and h.check_noun_lemma("tab"): with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.control_l_key, 't']) return True if h.check_verb_lemma("switch") and h.check_noun_lemma("tab"): with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.control_l_key, k.tab_key]) return True if h.directly_equal(["CLOSE", "ESCAPE"]): with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.control_l_key, 'w']) k.tap_key(k.escape_key) return True if h.check_lemma("back") and h.max_word_count(4): with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.alt_l_key, k.left_key]) return True if h.check_lemma("forward") and h.max_word_count(4): with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.alt_l_key, k.right_key]) return True if h.check_text("swipe") or h.check_text("scroll"): if h.check_text("left"): with nostdout(): with nostderr(): m = PyMouse() m.scroll(0, -5) return True if h.check_text("right"): with nostdout(): with nostderr(): m = PyMouse() m.scroll(0, 5) return True if h.check_text("up"): with nostdout(): with nostderr(): m = PyMouse() m.scroll(5, 0) return True if h.check_text("down"): with nostdout(): with nostderr(): m = PyMouse() m.scroll(-5, 0) return True if h.directly_equal(["PLAY", "PAUSE", "SPACEBAR"]): with nostdout(): with nostderr(): k = PyKeyboard() k.tap_key(" ") return True if ((h.check_text("shut") and h.check_text("down")) or (h.check_text("power") and h.check_text("off"))) and h.check_text("computer"): userin.execute(["sudo", "poweroff"], "Shutting down", True, 3) return True if h.check_nth_lemma(0, "goodbye") or h.check_nth_lemma(0, "bye") or (h.check_verb_lemma("see") and h.check_noun_lemma("you") and h.check_noun_lemma("later")): userin.say("Goodbye, " + user_prefix) # raise KeyboardInterrupt thread.interrupt_main() return True if (h.check_lemma("search") or h.check_lemma("find")) and h.check_lemma("wikipedia"): with nostderr(): search_query = "" for token in doc: if not (token.lemma_ == "search" or token.lemma_ == "find" or token.lemma_ == "wikipedia" or token.is_stop): search_query += ' ' + token.text search_query = search_query.strip() if search_query: try: wikiresult = wikipedia.search(search_query) if len(wikiresult) == 0: userin.say("Sorry, " + user_prefix + ". But I couldn't find anything about " + search_query + " in Wikipedia.") return True wikipage = wikipedia.page(wikiresult[0]) wikicontent = "".join([i if ord(i) < 128 else ' ' for i in wikipage.content]) wikicontent = re.sub(r'\([^)]*\)', '', wikicontent) userin.execute(["sensible-browser", wikipage.url], search_query) userin.say(wikicontent) return True except requests.exceptions.ConnectionError: userin.execute([" "], "Wikipedia connection error.") userin.say("Sorry, " + user_prefix + ". But I'm unable to connect to Wikipedia servers.") return True except wikipedia.exceptions.DisambiguationError as disambiguation: USER_ANSWERING['status'] = True USER_ANSWERING['for'] = 'wikipedia' USER_ANSWERING['reason'] = 'disambiguation' USER_ANSWERING['options'] = disambiguation.options[:3] notify = "Wikipedia disambiguation. Which one of these you meant?:\n - " + disambiguation.options[0] message = user_prefix + ", there is a disambiguation. Which one of these you meant? " + disambiguation.options[0] for option in disambiguation.options[1:3]: message += ", or " + option notify += "\n - " + option notify += '\nSay, for example: "THE FIRST ONE" to choose.' userin.execute([" "], notify) userin.say(message) return True except BaseException: pass if (h.check_lemma("search") or h.check_lemma("find")) and h.check_lemma("youtube"): with nostdout(): with nostderr(): search_query = "" for token in doc: if not (token.lemma_ == "search" or token.lemma_ == "find" or token.lemma_ == "youtube" or token.is_stop): search_query += ' ' + token.text search_query = search_query.strip() if search_query: info = youtube_dl.YoutubeDL({}).extract_info('ytsearch:' + search_query, download=False, ie_key='YoutubeSearch') if len(info['entries']) > 0: youtube_title = info['entries'][0]['title'] youtube_url = "https://www.youtube.com/watch?v=%s" % (info['entries'][0]['id']) userin.execute(["sensible-browser", youtube_url], youtube_title) youtube_title = "".join([i if ord(i) < 128 else ' ' for i in youtube_title]) else: youtube_title = "No video found, " + user_prefix + "." userin.say(youtube_title) time.sleep(5) k = PyKeyboard() k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key('f') return True if (h.check_lemma("search") or h.check_lemma("find")) and (h.check_lemma("google") or h.check_lemma("web") or h.check_lemma("internet")) and not h.check_lemma("image"): with nostdout(): with nostderr(): search_query = "" for token in doc: if not (token.lemma_ == "search" or token.lemma_ == "find" or token.lemma_ == "google" or token.lemma_ == "web" or token.lemma_ == "internet" or token.is_stop): search_query += ' ' + token.text search_query = search_query.strip() if search_query: tab_url = "http://google.com/?#q=" + search_query userin.execute(["sensible-browser", tab_url], search_query, True) return True if (h.check_lemma("search") or h.check_lemma("find")) and (h.check_lemma("google") or h.check_lemma("web") or h.check_lemma("internet")) and h.check_lemma("image"): with nostdout(): with nostderr(): search_query = "" for token in doc: if not (token.lemma_ == "search" or token.lemma_ == "find" or token.lemma_ == "google" or token.lemma_ == "web" or token.lemma_ == "internet" or token.lemma_ == "image" or token.is_stop): search_query += ' ' + token.text search_query = search_query.strip() if search_query: tab_url = "http://google.com/?#q=" + search_query + "&tbm=isch" userin.execute(["sensible-browser", tab_url], search_query, True) return True arithmetic_response = arithmetic_parse(com) if arithmetic_response: userin.say(arithmetic_response) else: learnerresponse = learner.respond(com) if learnerresponse: userin.say(learnerresponse) else: if not omniscient.respond(com, not args["silent"], userin, user_prefix, args["server"]): dc_response = dc.respond(com, user_prefix) if dc_response: userin.say(dc_response)
fileName = sys.argv[1] iterNum = int(sys.argv[2]) webNum = int(sys.argv[3]) force_utf8_hack() if DEBUG2: print "Load Web Sites" #websites = list_data.load_data(input_dir + filename) url_file = open(input_dir+filename,"r") websites = url_file.readlines() if DEBUG2: print " # web sites: %d" % (len(websites)) open_file = "%s/%s.web_time.txt" %(outputDir,fileName) with open(open_file, "w") as myfile: for run in xrange(0, iterNum): for wi in xrange(0, webNum): website = websites[wi] print website c_time = datetime.datetime.now().time() c_time = c_time.isoformat() logstamp = "%s,%s\n" % (c_time, website) if DEBUG3: print "%s-run%d-web%d: %s" % (c_time, run, wi, website) myfile.write(logstamp) p = Popen(['start', 'chrome', website],stdout=PIPE, shell=True) time.sleep(openItvl) k.press_keys([k.control_key,'w']) time.sleep(closeItvl)
class Action(object): def __init__(self, file): self.file = file self.m = PyMouse() self.k = PyKeyboard() def action_read(self): # 从filename中读取操作序列 action_list = [] with open(self.file) as f: f_csv = csv.reader(f) for row in f_csv: action_list.append(row) return action_list def key_translate(self, key_data): # 识别序列中的键 if len(key_data) == 1: return key_data elif len(key_data) == 2 and key_data.startswith('F'): return self.k.function_keys[int(key_data[1])] elif key_data == 'enter': return self.k.enter_key elif key_data == 'space': return self.k.space_key elif key_data == 'control': return self.k.control_key elif key_data == 'alt': return self.k.alt_key else: raise Exception("未定义此操作!") def action_translte(self, action_unit): # 将序列语言翻译成指令 if action_unit[0] == '0': try: xy = eval(action_unit[1]) except Exception as e: return False x, y = xy[0], xy[1] self.m.click(x, y, 1) return True elif action_unit[0] == '1': try: keyboard_action_list = action_unit[1].split('_') if keyboard_action_list[0] == 'tap': # 敲击某个键 self.k.tap_key(key_data) elif keyboard_action_list[0] == 'press': # 按住某个键 key_data = self.key_translate(keyboard_action_list[1]) self.k.press_key(key_data) elif keyboard_action_list[0] == 'presses': # 按住某两个键 key_data1 = self.key_translate(keyboard_action_list[1]) key_data2 = self.key_translate(keyboard_action_list[2]) self.k.press_keys([key_data1, key_data2]) elif keyboard_action_list[0] == 'release': # 松开某个键 key_data = self.key_translate(keyboard_action_list[1]) self.k.release_key(key_data) elif keyboard_action_list[0] == 'type': # 输入 self.k.type_string(keyboard_action_list[1]) elif keyboard_action_list[0] == 'callcpp': x = int(keyboard_action_list[1]) y = int(keyboard_action_list[2]) #i_path = keyboard_action_list[3] #t_path = keyboard_action_list[4] result, x_t, y_t = call_c(x, y) #x_tt = x_t.value #y_tt = y_t.value print(x_t, y_t) x_tt = x_t.value y_tt = y_t.value self.k.type_string(str(x_tt) + '_' + str(y_tt)) elif keyboard_action_list[0] == 'drawc': self.draw_circle() except Exception: traceback.print_exc() return False else: print("对象输入错误") return False def run(self): action_list = self.action_read() action_lens = len(action_list) for i in range(1, action_lens): self.action_translte(action_list[i]) delay = eval( action_list[i][2] ) ######################################################??????????????????????????????? time.sleep(delay) def draw_circle(self): self.m.click(421, 62, 1) self.m.click(422, 61, 1) self.m.click(419, 59, 1) time.sleep(0.5) self.m.press(421, 500) time.sleep(0.5) self.m.move(600, 500) time.sleep(0.5) self.m.move(600, 670) time.sleep(0.5) self.m.release(600, 600) # type some words self.m.click(293, 68) self.m.click(478, 549) self.k.type_string("Hello World!")
] def on_press_start(key): if key == keyboard.Key.enter: print('starting...') return False def on_press_end(key): if key == keyboard.Key.end: print('ended by user') return False with keyboard.Listener(on_press=on_press_start) as listener: print('press Enter key to start') listener.join() # wait for Enter... # open new tabs 50 times with keyboard.Listener(on_press=on_press_end) as listener: for _ in range(50): print('still running...press End key to quit') for url in urls: webbrowser.open(url, new=0) time.sleep(10) # close previously opened tab k.press_keys([k.control_key, 'w']) if not listener.running: break
def command(speech): #here = os.path.dirname(os.path.realpath(__file__)) #Popen(["./gradlew","web","-q"], stdout=FNULL, stderr=FNULL) #os.chdir(here) ''' kernel = aiml.Kernel() with nostdout(): with nostderr(): kernel.learn(DRAGONFIRE_PATH + "/aiml/learn.aiml") ''' previous_command = "" global inactive global user_full_name global user_prefix global config_file while(True): line = speech.readline() if line.startswith("sentence1: ") or line.startswith("<search failed>"): com = google_speech_api() if com == "\n" or com == " ": com = "Enter" original_com = com if (com == 0): #speech_error() continue com = com.upper() print "You: " + com if inactive == 1 and "DRAGONFIRE" != com and "DRAGON FIRE" != com and "WAKE UP" != com and com != "HEY": continue if "DRAGONFIRE" == com or "DRAGON FIRE" == com or "WAKE UP" == com or com == "HEY": tts_kill() inactive = 0 userin.define([" "]," ") words_dragonfire = { 0 : "Yes, " + user_prefix + ".", 1 : "Yes. I'm waiting.", 2 : "What is your orders?" } userin.say(words_dragonfire[randint(0,2)]) elif "GO TO SLEEP" == com: tts_kill() inactive = 1 userin.define(["echo"],"Dragonfire deactivated. To reactivate say 'Dragonfire!' or 'Wake Up!'") userin.execute(0) userin.say("I'm going to sleep") previous_command = com elif "ENOUGH" == com or "SHUT UP" == com: print "Dragonfire quiets." tts_kill() elif "WHO AM I" == com or "SAY MY NAME" == com: tts_kill() userin.define([" "], user_full_name) userin.execute(0) userin.say("Your name is " + user_full_name + ", " + user_prefix + ".") previous_command = com elif "MY TITLE IS LADY" == com or "I'M A LADY" == com or "I'M A WOMAN" == com or "I'M A GIRL" == com: tts_kill() config_file.update({'gender': 'female'}, Query().datatype == 'gender') user_prefix = "My Lady" userin.define([" "]," ") userin.say("Pardon, " + user_prefix + ".") elif "MY TITLE IS SIR" == com or "I'M A MAN" == com or "I'M A BOY" == com: tts_kill() config_file.update({'gender': 'male'}, Query().datatype == 'gender') user_prefix = "Sir" userin.define([" "]," ") userin.say("Pardon, " + user_prefix + ".") elif com.startswith("CALL ME "): tts_kill() callme_config = config_file.search(Query().datatype == 'callme') if callme_config: config_file.update({'title': original_com[8:].lower()}, Query().datatype == 'callme') else: config_file.insert({'datatype': 'callme', 'title': original_com[8:].lower()}) user_prefix = original_com[8:].lower().encode("utf8") userin.define([" "]," ") userin.say("Pardon, " + user_prefix + ".") elif "WHAT IS YOUR NAME" == com: tts_kill() userin.define([" "],"My name is Dragonfire.") userin.execute(0) userin.say("My name is Dragon Fire.") previous_command = com elif "WHAT IS THE TEMPERATURE" in com: # only for The United States today but prepared for all countries. Also only for celsius degrees today. --> by Radan Liska :-) tts_kill() with nostdout(): with nostderr(): capture = re.search("WHAT IS THE TEMPERATURE (?:IN|ON|AT)? (?P<query_city>.*) (?:IN|ON|AT|)? (?:THE)? (?P<query_state>.*)",com) if capture: search_query_city = capture.group('query_city') try: owm = pyowm.OWM("16d66c84e82424f0f8e62c3e3b27b574") city_and_state = str(search_query_city + ",us") weather = owm.weather_at_place(city_and_state).get_weather() userin.define([" "],str("The temperature in " + search_query_city + " is " + str(weather.get_temperature("celsius")['temp']))) userin.execute(0) userin.say("The temperature in " + search_query_city + " is " + str(weather.get_temperature("celsius")['temp'])) previous_command = com except: pass elif "WHAT IS YOUR GENDER" == com: tts_kill() userin.define([" "]," ") userin.say("I have a female voice but I don't have a gender identity. I'm a computer program, " + user_prefix + ".") previous_command = com elif "FILE MANAGER" in com or "OPEN FILES" == com: tts_kill() userin.define(["dolphin"],"File Manager") # KDE neon userin.execute(0) userin.define(["pantheon-files"],"File Manager") # elementary OS userin.execute(0) userin.define(["nautilus","--browser"],"File Manager") # Ubuntu userin.execute(0) userin.say("File Manager") previous_command = com elif "WEB BROWSER" in com: tts_kill() userin.define(["sensible-browser"],"Web Browser") userin.execute(0) userin.say("Web Browser") previous_command = com elif "OPEN BLENDER" == com: tts_kill() userin.define(["blender"],"Blender") userin.execute(0) userin.say("Blender 3D computer graphics software") previous_command = com elif "PHOTO SHOP" in com or "PHOTO EDITOR" in com or "GIMP" in com: tts_kill() userin.define(["gimp"],"GIMP") userin.execute(0) userin.say("Photo editor") previous_command = com elif "INKSCAPE" in com or "VECTOR GRAPHICS" in com or "VECTORIAL DRAWING" in com: tts_kill() userin.define(["inkscape"],"Inkscape") userin.execute(0) userin.say("Inkscape") previous_command = com elif "VIDEO EDITOR" in com: tts_kill() #userin.define(["openshot"],"Openshot") #userin.execute(0) #userin.define(["lightworks"],"Lightworks") #userin.execute(0) userin.define(["kdenlive"],"Kdenlive") userin.execute(0) userin.say("Video editor") previous_command = com elif "OPEN CAMERA" == com: tts_kill() userin.define(["kamoso"],"Camera") # KDE neon userin.execute(0) userin.define(["snap-photobooth"],"Camera") # elementary OS userin.execute(0) userin.define(["cheese"],"Camera") # Ubuntu userin.execute(0) userin.say("Camera") previous_command = com elif "OPEN CALENDAR" == com: tts_kill() userin.define(["korganizer"],"Calendar") # KDE neon userin.execute(0) userin.define(["maya-calendar"],"Calendar") # elementary OS userin.execute(0) userin.define(["orage"],"Calendar") # Ubuntu userin.execute(0) userin.say("Calendar") previous_command = com elif "OPEN CALCULATOR" == com: tts_kill() userin.define(["kcalc"],"Calculator") # KDE neon userin.execute(0) userin.define(["pantheon-calculator"],"Calculator") # elementary OS userin.execute(0) userin.define(["gnome-calculator"],"Calculator") # Ubuntu userin.execute(0) userin.say("Calculator") previous_command = com elif "OPEN STEAM" == com: tts_kill() userin.define(["steam"],"Steam") userin.execute(0) userin.say("Steam Game Store") previous_command = com elif "SOFTWARE CENTER" in com: tts_kill() userin.define(["plasma-discover"],"Software Center") # KDE neon userin.execute(0) userin.define(["software-center"],"Software Center") # elementary OS & Ubuntu userin.execute(0) userin.say("Software Center") previous_command = com elif "OFFICE SUITE" in com: tts_kill() userin.define(["libreoffice"],"LibreOffice") userin.execute(0) userin.say("Office Suite") previous_command = com elif "OPEN WRITER" == com: tts_kill() userin.define(["libreoffice","--writer"],"LibreOffice Writer") userin.execute(0) userin.say("Writer") previous_command = com elif "OPEN MATH" == com: tts_kill() userin.define(["libreoffice","--math"],"LibreOffice Math") userin.execute(0) userin.say("Math") previous_command = com elif "OPEN IMPRESS" == com: tts_kill() userin.define(["libreoffice","--impress"],"LibreOffice Impress") userin.execute(0) userin.say("Impress") previous_command = com elif "OPEN DRAW" == com: tts_kill() userin.define(["libreoffice","--draw"],"LibreOffice Draw") userin.execute(0) userin.say("Draw") previous_command = com elif com.startswith("KEYBOARD "): tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() for character in original_com[9:]: k.tap_key(character) k.tap_key(" ") elif com == "ENTER": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.tap_key(k.enter_key) elif com == "NEW TAB": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.control_l_key,'t']) elif com == "SWITCH TAB": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.control_l_key,k.tab_key]) elif com == "CLOSE" or com == "ESCAPE": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.control_l_key,'w']) k.tap_key(k.escape_key) elif com == "GO BACK": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.alt_l_key,k.left_key]) elif com == "GO FORWARD": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.alt_l_key,k.right_key]) elif com == "SCROLL LEFT": tts_kill() with nostdout(): with nostderr(): m = PyMouse() m.scroll(0,-5) elif com == "SCROLL RIGHT": tts_kill() with nostdout(): with nostderr(): m = PyMouse() m.scroll(0,5) elif com == "SCROLL UP": tts_kill() with nostdout(): with nostderr(): m = PyMouse() m.scroll(5,0) elif com == "SCROLL DOWN": tts_kill() with nostdout(): with nostderr(): m = PyMouse() m.scroll(-5,0) elif com == "PLAY" or com == "PAUSE" or com == "SPACEBAR": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.tap_key(" ") elif "SHUTDOWN THE COMPUTER" == com: tts_kill() userin.define(["sudo","poweroff"],"Shutting down") userin.say("Shutting down") userin.execute(3) previous_command = com elif com == "GOODBYE" or com == "BYE BYE" or com == "SEE YOU LATER": tts_kill() userin.define([" "]," ") userin.say("Goodbye, " + user_prefix) previous_command = com julius_proc.terminate() with nostdout(): with nostderr(): try: os.system('rm -f /tmp/' + str(datetime.date.today().year) + '*.[Ww][Aa][Vv]') except: pass sys.exit(1) elif "WIKIPEDIA" in com and ("SEARCH" in com or "FIND" in com): tts_kill() with nostdout(): with nostderr(): capture = re.search("(?:SEARCH|FIND) (?P<query>.*) (?:IN|ON|AT|USING)? WIKIPEDIA", com) if capture: search_query = capture.group('query') try: wikipage = wikipedia.page(wikipedia.search(search_query)[0]) wikicontent = "".join([i if ord(i) < 128 else ' ' for i in wikipage.content]) wikicontent = re.sub(r'\([^)]*\)', '', wikicontent) userin.define(["sensible-browser",wikipage.url],search_query) userin.execute(0) userin.say(wikicontent) previous_command = com except: pass elif "YOUTUBE" in com and ("SEARCH" in com or "FIND" in com): tts_kill() with nostdout(): with nostderr(): capture = re.search("(?:SEARCH|FIND) (?P<query>.*) (?:IN|ON|AT|USING)? YOUTUBE", com) if capture: search_query = capture.group('query') info = youtube_dl.YoutubeDL({}).extract_info('ytsearch:' + search_query, download=False, ie_key='YoutubeSearch') if len(info['entries']) > 0: youtube_title = info['entries'][0]['title'] youtube_url = "https://www.youtube.com/watch?v=%s" % (info['entries'][0]['id']) userin.define(["sensible-browser",youtube_url],youtube_title) youtube_title = "".join([i if ord(i) < 128 else ' ' for i in youtube_title]) else: youtube_title = "No video found, " + user_prefix + "." userin.define(" "," ") userin.execute(0) userin.say(youtube_title) time.sleep(5) k = PyKeyboard() k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key('f') else: tts_kill() #dragonfire_respond = kernel.respond(com) aiml_respond = learn_.respond(com) if aiml_respond: userin.define([" "]," ") userin.say(aiml_respond) else: omniscient_.respond(original_com,True,userin) previous_command = com
def command(self, com): """Function that serves as the entry point for each one of the user commands. This function goes through these steps for each one of user's commands, respectively: - Search across the built-in commands via a simple if-else control flow. - Try to get a response from :func:`dragonfire.arithmetic.arithmetic_parse` function. - Try to get a response from :func:`dragonfire.learn.Learner.respond` method. - Try to get a answer from :func:`dragonfire.omniscient.Omniscient.respond` method. - Try to get a response from :func:`dragonfire.deepconv.DeepConversation.respond` method. Args: com (str): User's command. Returns: str: Response. """ if not self.args["server"]: global config_file global e if (e.is_set()): # System Tray Icon exit must trigger this exit(0) args = self.args userin = self.userin user_full_name = self.user_full_name user_prefix = self.user_prefix if self.testing: config_file = self.config_file if isinstance(com, str) and com: com = com.strip() else: return False print("You: " + com.upper()) doc = nlp(com) h = Helper(doc) if args["verbose"]: userin.pretty_print_nlp_parsing_results(doc) if self.inactive and not ( h.directly_equal(["dragonfire", "hey"]) or (h.check_verb_lemma("wake") and h.check_nth_lemma(-1, "up")) or (h.check_nth_lemma(0, "dragon") and h.check_nth_lemma(1, "fire") and h.max_word_count(2))): return "" if USER_ANSWERING['status']: if com.startswith("FIRST") or com.startswith( "THE FIRST") or com.startswith("SECOND") or com.startswith( "THE SECOND") or com.startswith( "THIRD") or com.startswith("THE THIRD"): USER_ANSWERING['status'] = False selection = None if com.startswith("FIRST") or com.startswith("THE FIRST"): selection = 0 elif com.startswith("SECOND") or com.startswith("THE SECOND"): selection = 1 elif com.startswith("THIRD") or com.startswith("THE THIRD"): selection = 2 if USER_ANSWERING['for'] == 'wikipedia': with nostderr(): search_query = USER_ANSWERING['options'][selection] try: wikiresult = wikipedia.search(search_query) if len(wikiresult) == 0: userin.say( "Sorry, " + user_prefix + ". But I couldn't find anything about " + search_query + " in Wikipedia.") return True wikipage = wikipedia.page(wikiresult[0]) wikicontent = "".join([ i if ord(i) < 128 else ' ' for i in wikipage.content ]) wikicontent = re.sub(r'\([^)]*\)', '', wikicontent) userin.execute(["sensible-browser", wikipage.url], search_query) return userin.say( wikicontent, cmd=["sensible-browser", wikipage.url]) except requests.exceptions.ConnectionError: userin.execute([" "], "Wikipedia connection error.") return userin.say( "Sorry, " + user_prefix + ". But I'm unable to connect to Wikipedia servers." ) except Exception: return False if h.directly_equal([ "dragonfire", "hey" ]) or (h.check_verb_lemma("wake") and h.check_nth_lemma(-1, "up")) or ( h.check_nth_lemma(0, "dragon") and h.check_nth_lemma(1, "fire") and h.max_word_count(2)): self.inactive = False return userin.say( choice([ "Yes, " + user_prefix + ".", "Yes. I'm waiting.", "What is your order?", "Ready for the orders!", user_prefix.capitalize() + ", tell me your wish." ])) if (h.check_verb_lemma("go") and h.check_noun_lemma("sleep")) or ( h.check_verb_lemma("stop") and h.check_verb_lemma("listen")): self.inactive = True userin.execute([ "echo" ], "Dragonfire deactivated. To reactivate say 'Dragonfire!' or 'Wake Up!'" ) return userin.say("I'm going to sleep") if h.directly_equal(["enough"]) or (h.check_verb_lemma("shut") and h.check_nth_lemma(-1, "up")): tts_kill() msg = "Dragonfire quiets." print(msg) return msg if h.check_wh_lemma("what") and h.check_deps_contains("your name"): return userin.execute([" "], "My name is Dragonfire.", True) if h.check_wh_lemma("what") and h.check_deps_contains("your gender"): return userin.say( "I have a female voice but I don't have a gender identity. I'm a computer program, " + user_prefix + ".") if (h.check_wh_lemma("who") and h.check_text("I")) or (h.check_verb_lemma("say") and h.check_text("my") and h.check_lemma("name")): userin.execute([" "], user_full_name) return userin.say("Your name is " + user_full_name + ", " + user_prefix + ".") if h.check_verb_lemma("open") or h.check_adj_lemma( "open") or h.check_verb_lemma("run") or h.check_verb_lemma( "start") or h.check_verb_lemma("show"): if h.check_text("blender"): userin.execute(["blender"], "Blender") return userin.say("Blender 3D computer graphics software") if h.check_text("draw"): userin.execute(["libreoffice", "--draw"], "LibreOffice Draw") return userin.say("Opening LibreOffice Draw") if h.check_text("impress"): userin.execute(["libreoffice", "--impress"], "LibreOffice Impress") return userin.say("Opening LibreOffice Impress") if h.check_text("math"): userin.execute(["libreoffice", "--math"], "LibreOffice Math") return userin.say("Opening LibreOffice Math") if h.check_text("writer"): userin.execute(["libreoffice", "--writer"], "LibreOffice Writer") return userin.say("Opening LibreOffice Writer") if h.check_text("gimp") or (h.check_noun_lemma("photo") and (h.check_noun_lemma("editor") or h.check_noun_lemma("shop"))): userin.execute(["gimp"], "GIMP") return userin.say("Opening the photo editor software.") if h.check_text("inkscape") or (h.check_noun_lemma("vector") and h.check_noun_lemma("graphic") ) or (h.check_text("vectorial") and h.check_text("drawing")): userin.execute(["inkscape"], "Inkscape") return userin.say("Opening the vectorial drawing software.") if h.check_noun_lemma("office") and h.check_noun_lemma("suite"): userin.execute(["libreoffice"], "LibreOffice") return userin.say("Opening LibreOffice") if h.check_text("kdenlive") or (h.check_noun_lemma("video") and h.check_noun_lemma("editor")): userin.execute(["kdenlive"], "Kdenlive") return userin.say("Opening the video editor software.") if h.check_noun_lemma("browser") or h.check_text( "chrome") or h.check_text("firefox"): userin.execute(["sensible-browser"], "Web Browser") return userin.say("Web browser") if h.check_text("steam"): userin.execute(["steam"], "Steam") return userin.say("Opening Steam Game Store") if h.check_text("files") or (h.check_noun_lemma("file") and h.check_noun_lemma("manager")): userin.execute(["dolphin"], "File Manager") # KDE neon userin.execute(["pantheon-files"], "File Manager") # elementary OS userin.execute(["nautilus", "--browser"], "File Manager") # Ubuntu return userin.say("File Manager") if h.check_noun_lemma("camera"): userin.execute(["kamoso"], "Camera") # KDE neon userin.execute(["snap-photobooth"], "Camera") # elementary OS userin.execute(["cheese"], "Camera") # Ubuntu return userin.say("Camera") if h.check_noun_lemma("calendar"): userin.execute(["korganizer"], "Calendar") # KDE neon userin.execute(["maya-calendar"], "Calendar") # elementary OS userin.execute(["orage"], "Calendar") # Ubuntu return userin.say("Calendar") if h.check_noun_lemma("calculator"): userin.execute(["kcalc"], "Calculator") # KDE neon userin.execute(["pantheon-calculator"], "Calculator") # elementary OS userin.execute(["gnome-calculator"], "Calculator") # Ubuntu return userin.say("Calculator") if h.check_noun_lemma("software") and h.check_text("center"): userin.execute(["plasma-discover"], "Software Center") # KDE neon userin.execute(["software-center"], "Software Center") # elementary OS & Ubuntu return userin.say("Software Center") if h.check_lemma("be") and h.check_lemma("-PRON-") and ( h.check_lemma("lady") or h.check_lemma("woman") or h.check_lemma("girl")): config_file.update({'gender': 'female'}, Query().datatype == 'gender') config_file.remove(Query().datatype == 'callme') user_prefix = "my lady" return userin.say("Pardon, " + user_prefix + ".") if h.check_lemma("be") and h.check_lemma("-PRON-") and ( h.check_lemma("sir") or h.check_lemma("man") or h.check_lemma("boy")): config_file.update({'gender': 'male'}, Query().datatype == 'gender') config_file.remove(Query().datatype == 'callme') user_prefix = "sir" return userin.say("Pardon, " + user_prefix + ".") if h.check_lemma("call") and h.check_lemma("-PRON-"): title = "" for token in doc: if token.pos_ == "NOUN": title += ' ' + token.text title = title.strip() if not args["server"]: callme_config = config_file.search( Query().datatype == 'callme') if callme_config: config_file.update({'title': title}, Query().datatype == 'callme') else: config_file.insert({'datatype': 'callme', 'title': title}) user_prefix = title return userin.say("OK, " + user_prefix + ".") if h.is_wh_question() and h.check_lemma("temperature"): city = "" for ent in doc.ents: if ent.label_ == "GPE": city += ' ' + ent.text city = city.strip() if city: owm = pyowm.OWM("16d66c84e82424f0f8e62c3e3b27b574") reg = owm.city_id_registry() try: weather = owm.weather_at_id( reg.ids_for(city)[0][0]).get_weather() fmt = "The temperature in {} is {} degrees celsius" msg = fmt.format( city, weather.get_temperature('celsius')['temp']) userin.execute([" "], msg) return userin.say(msg) except IndexError: msg = "Sorry, " + user_prefix + " but I couldn't find a city named " + city + " on the internet." userin.execute([" "], msg) return userin.say(msg) if (h.check_nth_lemma(0, "keyboard") or h.check_nth_lemma(0, "type")) and not args["server"]: n = len(doc[0].text) + 1 with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: for character in com[n:]: k.tap_key(character) k.tap_key(" ") return "keyboard" if (h.directly_equal(["enter"]) or (h.check_adj_lemma("new") and h.check_noun_lemma("line"))) and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.tap_key(k.enter_key) return "enter" if h.check_adj_lemma("new") and h.check_noun_lemma( "tab") and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.press_keys([k.control_l_key, 't']) return "new tab" if h.check_verb_lemma("switch") and h.check_noun_lemma( "tab") and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.press_keys([k.control_l_key, k.tab_key]) return "switch tab" if h.directly_equal(["CLOSE", "ESCAPE"]) and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.press_keys([k.control_l_key, 'w']) k.tap_key(k.escape_key) return "close" if h.check_lemma("back") and h.max_word_count( 4) and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.press_keys([k.alt_l_key, k.left_key]) return "back" if h.check_lemma("forward") and h.max_word_count( 4) and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.press_keys([k.alt_l_key, k.right_key]) return "forward" if (h.check_text("swipe") or h.check_text("scroll")) and not args["server"]: if h.check_text("left"): with nostdout(): with nostderr(): m = PyMouse() if not self.testing: m.scroll(0, -5) return "swipe left" if h.check_text("right"): with nostdout(): with nostderr(): m = PyMouse() if not self.testing: m.scroll(0, 5) return "swipe right" if h.check_text("up"): with nostdout(): with nostderr(): m = PyMouse() if not self.testing: m.scroll(5, 0) return "swipe up" if h.check_text("down"): with nostdout(): with nostderr(): m = PyMouse() if not self.testing: m.scroll(-5, 0) return "swipe down" if h.directly_equal(["PLAY", "PAUSE", "SPACEBAR" ]) and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.tap_key(" ") return "play" if ((h.check_text("shut") and h.check_text("down")) or (h.check_text("power") and h.check_text("off")) ) and h.check_text("computer") and not args["server"]: return userin.execute(["sudo", "poweroff"], "Shutting down", True, 3) if h.check_nth_lemma(0, "goodbye") or h.check_nth_lemma( 0, "bye") or (h.check_verb_lemma("see") and h.check_text("you") and h.check_adv_lemma("later")): response = userin.say("Goodbye, " + user_prefix) if not args["server"] and not self.testing: # raise KeyboardInterrupt thread.interrupt_main() return response if (h.check_lemma("search") or h.check_lemma("find")) and h.check_lemma("wikipedia"): with nostderr(): search_query = "" for token in doc: if not (token.lemma_ == "search" or token.lemma_ == "find" or token.lemma_ == "wikipedia" or token.is_stop): search_query += ' ' + token.text search_query = search_query.strip() if search_query: try: wikiresult = wikipedia.search(search_query) if len(wikiresult) == 0: userin.say( "Sorry, " + user_prefix + ". But I couldn't find anything about " + search_query + " in Wikipedia.") return True wikipage = wikipedia.page(wikiresult[0]) wikicontent = "".join([ i if ord(i) < 128 else ' ' for i in wikipage.content ]) wikicontent = re.sub(r'\([^)]*\)', '', wikicontent) userin.execute(["sensible-browser", wikipage.url], search_query) return userin.say( wikicontent, cmd=["sensible-browser", wikipage.url]) except requests.exceptions.ConnectionError: userin.execute([" "], "Wikipedia connection error.") return userin.say( "Sorry, " + user_prefix + ". But I'm unable to connect to Wikipedia servers." ) except wikipedia.exceptions.DisambiguationError as disambiguation: USER_ANSWERING['status'] = True USER_ANSWERING['for'] = 'wikipedia' USER_ANSWERING['reason'] = 'disambiguation' USER_ANSWERING['options'] = disambiguation.options[:3] notify = "Wikipedia disambiguation. Which one of these you meant?:\n - " + disambiguation.options[ 0] msg = user_prefix + ", there is a disambiguation. Which one of these you meant? " + disambiguation.options[ 0] for option in disambiguation.options[1:3]: msg += ", or " + option notify += "\n - " + option notify += '\nSay, for example: "THE FIRST ONE" to choose.' userin.execute([" "], notify) return userin.say(msg) except BaseException: pass if (h.check_lemma("search") or h.check_lemma("find")) and h.check_lemma("youtube"): with nostdout(): with nostderr(): search_query = "" for token in doc: if not (token.lemma_ == "search" or token.lemma_ == "find" or token.lemma_ == "youtube" or token.is_stop): search_query += ' ' + token.text search_query = search_query.strip() if search_query: info = youtube_dl.YoutubeDL({}).extract_info( 'ytsearch:' + search_query, download=False, ie_key='YoutubeSearch') if len(info['entries']) > 0: youtube_title = info['entries'][0]['title'] youtube_url = "https://www.youtube.com/watch?v=%s" % ( info['entries'][0]['id']) userin.execute(["sensible-browser", youtube_url], youtube_title) youtube_title = "".join([ i if ord(i) < 128 else ' ' for i in youtube_title ]) response = userin.say( youtube_title, ["sensible-browser", youtube_url]) else: youtube_title = "No video found, " + user_prefix + "." response = userin.say(youtube_title) k = PyKeyboard() if not args["server"] and not self.testing: time.sleep(5) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key('f') return response if (h.check_lemma("search") or h.check_lemma("find")) and ( h.check_lemma("google") or h.check_lemma("web") or h.check_lemma("internet")) and not h.check_lemma("image"): with nostdout(): with nostderr(): search_query = "" for token in doc: if not (token.lemma_ == "search" or token.lemma_ == "find" or token.lemma_ == "google" or token.lemma_ == "web" or token.lemma_ == "internet" or token.is_stop): search_query += ' ' + token.text search_query = search_query.strip() if search_query: tab_url = "http://google.com/?#q=" + search_query return userin.execute(["sensible-browser", tab_url], search_query, True) if (h.check_lemma("search") or h.check_lemma("find")) and ( h.check_lemma("google") or h.check_lemma("web") or h.check_lemma("internet")) and h.check_lemma("image"): with nostdout(): with nostderr(): search_query = "" for token in doc: if not (token.lemma_ == "search" or token.lemma_ == "find" or token.lemma_ == "google" or token.lemma_ == "web" or token.lemma_ == "internet" or token.lemma_ == "image" or token.is_stop): search_query += ' ' + token.text search_query = search_query.strip() if search_query: tab_url = "http://google.com/?#q=" + search_query + "&tbm=isch" return userin.execute(["sensible-browser", tab_url], search_query, True) original_com = com com = coref.resolve(com) if args["verbose"]: print("After Coref Resolution: " + com) arithmetic_response = arithmetic_parse(com) if arithmetic_response: return userin.say(arithmetic_response) else: learner_response = learner.respond(com) if learner_response: return userin.say(learner_response) else: omniscient_response = omniscient.respond( com, not args["silent"], userin, user_prefix, args["server"]) if omniscient_response: return omniscient_response else: dc_response = dc.respond(original_com, user_prefix) if dc_response: return userin.say(dc_response)
def command(self, com): """Function that serves as the entry point for each one of the user commands. This function goes through these steps for each one of user's commands, respectively: - Search across the built-in commands via a simple if-else control flow. - Try to get a response from :func:`dragonfire.arithmetic.arithmetic_parse` function. - Try to get a response from :func:`dragonfire.learn.Learner.respond` method. - Try to get a answer from :func:`dragonfire.odqa.ODQA.respond` method. - Try to get a response from :func:`dragonfire.deepconv.DeepConversation.respond` method. Args: com (str): User's command. Returns: str: Response. """ if not self.args["server"]: global config_file global e if (e.is_set()): # System Tray Icon exit must trigger this exit(0) args = self.args userin = self.userin user_full_name = self.user_full_name user_prefix = self.user_prefix if self.testing: config_file = self.config_file if isinstance(com, str) and com: com = com.strip() else: return False print("You: " + com.upper()) doc = nlp(com) h = Helper(doc) self.h = h if args["verbose"]: userin.pretty_print_nlp_parsing_results(doc) # Input: DRAGONFIRE | WAKE UP | HEY if self.inactive and not self.check_wake_up_intent: return "" # User is answering to Dragonfire if user_answering['status']: if com.startswith("FIRST") or com.startswith( "THE FIRST") or com.startswith("SECOND") or com.startswith( "THE SECOND") or com.startswith( "THIRD") or com.startswith("THE THIRD"): user_answering['status'] = False selection = None if com.startswith("FIRST") or com.startswith("THE FIRST"): selection = 0 elif com.startswith("SECOND") or com.startswith("THE SECOND"): selection = 1 elif com.startswith("THIRD") or com.startswith("THE THIRD"): selection = 2 if user_answering['for'] == 'wikipedia': with nostderr(): search_query = user_answering['options'][selection] try: return self.wikisearch(search_query) except requests.exceptions.ConnectionError: return self.wikipedia_connection_error() except WikipediaNoResultsFoundError: return self.wikipedia_no_results_found_error( search_query) except Exception: return False # Input: DRAGONFIRE | WAKE UP | HEY if self.check_wake_up_intent(): self.inactive = False return userin.say( choice([ "Yes, " + user_prefix + ".", "Yes. I'm waiting.", "What is your order?", "Ready for the orders!", user_prefix.capitalize() + ", tell me your wish." ])) # Input: GO TO SLEEP if (h.check_verb_lemma("go") and h.check_noun_lemma("sleep")) or ( h.check_verb_lemma("stop") and h.check_verb_lemma("listen")): self.inactive = True userin.execute([ "echo" ], "Dragonfire deactivated. To reactivate say 'Dragonfire!' or 'Wake Up!'" ) return userin.say("I'm going to sleep") # Input: ENOUGH | SHUT UP if h.directly_equal(["enough"]) or (h.check_verb_lemma("shut") and h.check_nth_lemma(-1, "up")): tts_kill() msg = "Dragonfire quiets." print(msg) return msg # Input: WHAT IS YOUR NAME if h.check_wh_lemma("what") and h.check_deps_contains("your name"): return userin.execute([" "], "My name is Dragonfire.", True) # Input: WHAT IS YOUR GENDER if h.check_wh_lemma("what") and h.check_deps_contains("your gender"): return userin.say( "I have a female voice but I don't have a gender identity. I'm a computer program, " + user_prefix + ".") # Input: WHO AM I | SAY MY NAME if (h.check_wh_lemma("who") and h.check_text("I")) or (h.check_verb_lemma("say") and h.check_text("my") and h.check_lemma("name")): userin.execute([" "], user_full_name) return userin.say("Your name is " + user_full_name + ", " + user_prefix + ".") # Input: OPEN [CAMERA, CALENDAR, CALCULATOR, STEAM, BLENDER, WRITER, MATH, IMPRESS, DRAW, TERMINAL] if h.check_verb_lemma("open") or h.check_adj_lemma( "open") or h.check_verb_lemma("run") or h.check_verb_lemma( "start") or h.check_verb_lemma("show"): if h.check_text("blender"): userin.execute(["blender"], "Blender") return userin.say("Blender 3D computer graphics software") if h.check_text("draw"): userin.execute(["libreoffice", "--draw"], "LibreOffice Draw") return userin.say("Opening LibreOffice Draw") if h.check_text("impress"): userin.execute(["libreoffice", "--impress"], "LibreOffice Impress") return userin.say("Opening LibreOffice Impress") if h.check_text("math"): userin.execute(["libreoffice", "--math"], "LibreOffice Math") return userin.say("Opening LibreOffice Math") if h.check_text("writer"): userin.execute(["libreoffice", "--writer"], "LibreOffice Writer") return userin.say("Opening LibreOffice Writer") if h.check_noun_lemma("browser") or h.check_text( "chrome") or h.check_text("firefox"): userin.execute(["sensible-browser"], "Web Browser") return userin.say("Web browser") if h.check_text("steam"): userin.execute(["steam"], "Steam") return userin.say("Opening Steam Game Store") if h.check_text("files"): return self.start_file_manager() if h.check_noun_lemma("camera"): userin.execute(["kamoso"], "Camera") # KDE neon userin.execute(["snap-photobooth"], "Camera") # elementary OS userin.execute(["cheese"], "Camera") # Ubuntu return userin.say("Camera") if h.check_noun_lemma("calendar"): userin.execute(["korganizer"], "Calendar") # KDE neon userin.execute(["maya-calendar"], "Calendar") # elementary OS userin.execute(["orage"], "Calendar") # Ubuntu return userin.say("Calendar") if h.check_noun_lemma("calculator"): userin.execute(["kcalc"], "Calculator") # KDE neon userin.execute(["pantheon-calculator"], "Calculator") # elementary OS userin.execute(["gnome-calculator"], "Calculator") # Ubuntu return userin.say("Calculator") if h.check_noun_lemma("console") or h.check_noun_lemma("terminal"): userin.execute(["konsole"], "Terminal") # KDE neon userin.execute(["gnome-terminal"], "Terminal") # elementary OS & Ubuntu userin.execute(["guake"], "Terminal") # Guake return userin.say("Terminal") # Input FILE MANAGER | FILE EXPLORER if h.check_noun_lemma("file") and (h.check_noun_lemma("manager") or h.check_noun_lemma("explorer")): return self.start_file_manager() # Input: SOFTWARE CENTER if h.check_noun_lemma("software") and h.check_text("center"): userin.execute(["plasma-discover"], "Software Center") # KDE neon userin.execute(["software-center"], "Software Center") # elementary OS & Ubuntu return userin.say("Software Center") # Input: OFFICE SUITE if h.check_noun_lemma("office") and h.check_noun_lemma("suite"): userin.execute(["libreoffice"], "LibreOffice") return userin.say("Opening LibreOffice") # Input: GIMP | PHOTOSHOP | PHOTO EDITOR if h.check_text("gimp") or (h.check_noun_lemma("photo") and (h.check_noun_lemma("editor") or h.check_noun_lemma("shop"))): userin.execute(["gimp"], "GIMP") return userin.say("Opening the photo editor software.") # Input: INKSCAPE | VECTOR GRAPHICS if h.check_text("inkscape") or (h.check_noun_lemma("vector") and h.check_noun_lemma("graphic")) or ( h.check_text("vectorial") and h.check_text("drawing")): userin.execute(["inkscape"], "Inkscape") return userin.say("Opening the vectorial drawing software.") # Input: Kdenlive | VIDEO EDITOR if h.check_text("kdenlive") or (h.check_noun_lemma("video") and h.check_noun_lemma("editor")): userin.execute(["kdenlive"], "Kdenlive") return userin.say("Opening the video editor software.") # Input: MY TITLE IS LADY | I'M A LADY | I'M A WOMAN | I'M A GIRL if h.check_lemma("be") and h.check_lemma( "-PRON-") and h.check_gender_lemmas('female'): return self.gender_update('female') # Input: MY TITLE IS SIR | I'M A MAN | I'M A BOY if h.check_lemma("be") and h.check_lemma( "-PRON-") and h.check_gender_lemmas('male'): return self.gender_update('male') # Input: CALL ME * if h.check_lemma("call") and h.check_lemma("-PRON-"): title = "" for token in doc: if token.pos_ == "NOUN": title += ' ' + token.text title = title.strip() if not args["server"]: callme_config = config_file.search( Query().datatype == 'callme') if callme_config: config_file.update({'title': title}, Query().datatype == 'callme') else: config_file.insert({'datatype': 'callme', 'title': title}) self.user_prefix = title user_prefix = self.user_prefix return userin.say("OK, " + user_prefix + ".") # Input: WHAT'S THE TEMPERATURE IN * if h.is_wh_question() and h.check_lemma("temperature"): city = "" for ent in doc.ents: if ent.label_ == "GPE": city += ' ' + ent.text city = city.strip() if city: owm = pyowm.OWM("16d66c84e82424f0f8e62c3e3b27b574") reg = owm.city_id_registry() try: weather = owm.weather_at_id( reg.ids_for(city)[0][0]).get_weather() fmt = "The temperature in {} is {} degrees celsius" msg = fmt.format( city, weather.get_temperature('celsius')['temp']) userin.execute([" "], msg) return userin.say(msg) except IndexError: msg = "Sorry, " + user_prefix + " but I couldn't find a city named " + city + " on the internet." userin.execute([" "], msg) return userin.say(msg) # Input: WHAT TIME IS IT if h.check_wh_lemma("what") and h.check_noun_lemma( "time") and h.check_verb_lemma("be") and h.check_text("it"): return userin.say("It's " + datetime.datetime.now().strftime("%I:%M %p") + choice([", " + user_prefix + ".", "."])) # Input: KEYBOARD * if (h.check_nth_lemma(0, "keyboard") or h.check_nth_lemma(0, "type")) and not args["server"]: n = len(doc[0].text) + 1 with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: for character in com[n:]: k.tap_key(character) k.tap_key(" ") return "keyboard" # Input: ENTER | NEW TAB | SWITCH TAB | CLOSE | GO BACK | GO FORWARD if (h.directly_equal(["enter"]) or (h.check_adj_lemma("new") and h.check_noun_lemma("line"))) and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.tap_key(k.enter_key) return "enter" if h.check_adj_lemma("new") and h.check_noun_lemma( "tab") and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.press_keys([k.control_l_key, 't']) return "new tab" if h.check_verb_lemma("switch") and h.check_noun_lemma( "tab") and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.press_keys([k.control_l_key, k.tab_key]) return "switch tab" if h.directly_equal(["CLOSE", "ESCAPE"]) and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.press_keys([k.control_l_key, 'w']) k.tap_key(k.escape_key) return "close" if self.check_browser_history_nav_intent("back"): return self.press_browser_history_nav("back") if self.check_browser_history_nav_intent("forward"): return self.press_browser_history_nav("forward") # Input: SCROLL LEFT | SCROLL RIGHT | SCROLL UP | SCROLL DOWN if (h.check_text("swipe") or h.check_text("scroll")) and not args["server"]: if h.check_text("left"): with nostdout(): with nostderr(): m = PyMouse() if not self.testing: m.scroll(0, -5) return "swipe left" if h.check_text("right"): with nostdout(): with nostderr(): m = PyMouse() if not self.testing: m.scroll(0, 5) return "swipe right" if h.check_text("up"): with nostdout(): with nostderr(): m = PyMouse() if not self.testing: m.scroll(5, 0) return "swipe up" if h.check_text("down"): with nostdout(): with nostderr(): m = PyMouse() if not self.testing: m.scroll(-5, 0) return "swipe down" # Input: PLAY | PAUSE | SPACEBAR if h.directly_equal(["PLAY", "PAUSE", "SPACEBAR" ]) and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.tap_key(" ") return "play" # Input: SHUT DOWN THE COMPUTER if ((h.check_text("shut") and h.check_text("down")) or (h.check_text("power") and h.check_text("off")) ) and h.check_text("computer") and not args["server"]: return userin.execute(["sudo", "poweroff"], "Shutting down", True, 3) # Input: GOODBYE | BYE BYE | SEE YOU LATER if h.check_nth_lemma(0, "goodbye") or h.check_nth_lemma( 0, "bye") or (h.check_verb_lemma("see") and h.check_text("you") and h.check_adv_lemma("later")): response = userin.say("Goodbye, " + user_prefix) if not args["server"] and not self.testing: # raise KeyboardInterrupt thread.interrupt_main() return response # Input: (SEARCH|FIND) * (IN|ON|AT|USING) WIKIPEDIA if (h.check_lemma("search") or h.check_lemma("find")) and h.check_lemma("Wikipedia"): with nostderr(): search_query = self.strip_the_search_query_by_intent( doc, "Wikipedia") if search_query: try: return self.wikisearch(search_query) except requests.exceptions.ConnectionError: return self.wikipedia_connection_error() except wikipedia.exceptions.DisambiguationError as disambiguation: user_answering['status'] = True user_answering['for'] = 'wikipedia' user_answering['reason'] = 'disambiguation' user_answering['options'] = disambiguation.options[:3] notify = "Wikipedia disambiguation. Which one of these you meant?:\n - " + disambiguation.options[ 0] msg = user_prefix + ", there is a disambiguation. Which one of these you meant? " + disambiguation.options[ 0] for option in disambiguation.options[1:3]: msg += ", or " + option notify += "\n - " + option notify += '\nSay, for example: "THE FIRST ONE" to choose.' userin.execute([" "], notify) return userin.say(msg) except WikipediaNoResultsFoundError: return self.wikipedia_no_results_found_error( search_query) except Exception: pass # Input: (SEARCH|FIND) * (IN|ON|AT|USING) YOUTUBE if (h.check_lemma("search") or h.check_lemma("find")) and h.check_lemma("YouTube"): with nostdout(): with nostderr(): search_query = self.strip_the_search_query_by_intent( doc, "YouTube") if search_query: info = youtube_dl.YoutubeDL({}).extract_info( 'ytsearch:' + search_query, download=False, ie_key='YoutubeSearch') if len(info['entries']) > 0: youtube_title = info['entries'][0]['title'] youtube_url = "https://www.youtube.com/watch?v=%s" % ( info['entries'][0]['id']) userin.execute(["sensible-browser", youtube_url], youtube_title) youtube_title = TextToAction.fix_the_encoding_in_text_for_tts( youtube_title) response = userin.say( youtube_title, ["sensible-browser", youtube_url]) else: youtube_title = "No video found, " + user_prefix + "." response = userin.say(youtube_title) k = PyKeyboard() if not args["server"] and not self.testing: time.sleep(5) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key('f') return response # Input: (SEARCH|FIND) * (IN|ON|AT|USING) (GOOGLE|WEB) if (h.check_lemma("search") or h.check_lemma("find")) and ( h.check_lemma("Google") or h.check_lemma("web") or h.check_lemma("internet")) and not h.check_lemma("image"): with nostdout(): with nostderr(): search_query = "" for token in doc: if not (token.lemma_ == "search" or token.lemma_ == "find" or token.lemma_ == "Google" or token.lemma_ == "web" or token.lemma_ == "internet" or token.is_stop): search_query += ' ' + token.text search_query = search_query.strip() if search_query: tab_url = "http://google.com/?#q=" + search_query return userin.execute(["sensible-browser", tab_url], search_query, True) # Input: (SEARCH IMAGES OF|FIND IMAGES OF|SEARCH|FIND) * (IN|ON|AT|USING) (GOOGLE|WEB|GOOGLE IMAGES|WEB IMAGES) if (h.check_lemma("search") or h.check_lemma("find")) and ( h.check_lemma("Google") or h.check_lemma("web") or h.check_lemma("internet")) and h.check_lemma("image"): with nostdout(): with nostderr(): search_query = "" for token in doc: if not (token.lemma_ == "search" or token.lemma_ == "find" or token.lemma_ == "Google" or token.lemma_ == "web" or token.lemma_ == "internet" or token.lemma_ == "image" or token.is_stop): search_query += ' ' + token.text search_query = search_query.strip() if search_query: tab_url = "http://google.com/?#q=" + search_query + "&tbm=isch" return userin.execute(["sensible-browser", tab_url], search_query, True) original_com = com com = coref.resolve(com) if args["verbose"]: print("After Coref Resolution: " + com) arithmetic_response = arithmetic_parse(com) if arithmetic_response: return userin.say(arithmetic_response) else: learner_response = learner.respond(com) if learner_response: return userin.say(learner_response) else: odqa_response = odqa.respond(com, not args["silent"], userin, user_prefix, args["server"]) if odqa_response: return userin.say(odqa_response) else: dc_response = dc.respond(original_com, user_prefix) if dc_response: return userin.say(dc_response)
class folishMonkey: def __init__(self): self.keyboard = PyKeyboard() self.mouse = PyMouse() def random_location(self): x = random.randint(0, 1300) y = random.randint(0, 800) return (x, y) #随机移动 def random_move(self): x, y = self.random_location() self.mouse.move(x, y) #单击 def click(self): x, y = self.random_location() self.mouse.click(x, y) #双击 def double_click(self): x, y = self.random_location() self.mouse.click(x, y, n=2) #右击 def right_click(self): x, y = self.random_location() self.mouse.click(x, y, button=2) #随机移动后输入字符串 def input_str(self): self.random_move() self.keyboard.type_string("hello") #随机按键 def presskey(self): key_list = [ self.keyboard.control_key, self.keyboard.shift_key, self.keyboard.backspace_key ] key = random.choice(key_list) self.keyboard.press_key(key) self.keyboard.release_key(key) #组合按键 def presskeys(self): keys_list = [[self.keyboard.control_key, self.keyboard.shift_key], [self.keyboard.enter_key, 'c'], [self.keyboard.tab_key, self.keyboard.alt_key, 'v']] keys = random.choice(keys_list) self.keyboard.press_keys(keys) self.keyboard.release_key(keys) #打开应用 def open_application(self, exe): os.popen(exe) #获取窗口矩形 def get_appRectangle(self, name): window = uiautomation.WindowControl(name) con = window.BoundingRectangle # def write_log(self): #运行函数 def start(self, count): for i in range(0, count): num = random.randint(0, 100) if num < 20: self.random_move() elif num < 40: self.click() elif num < 50: self.double_click() elif num < 60: self.right_click() elif num < 80: self.input_str() else: self.presskey() time.sleep(1)
gameboard[listnumber-1][whichracket]='|' if direction=='down': gameboard[listnumber+1][whichracket]='|' gameboard[listnumber][whichracket]='' def locateball(): listnumber=-1 for elem in gameboard: listnumber=listnumber+1 listindex=-1 for elem in elem: listindex=listindex+1 if elem=='*': return([listnumber,listindex]) def locateracket(whichracket): listnumber=-1 for elem in gameboard: listnumber=listnumber+1 if elem[whichracket]=='|': break return(listnumber) for elem in range(50): updateboard() time.sleep(0.001) moveball([round(elem/7),round(elem/9)]) updateboard() keyboard.press_keys(['Command','k'])
input_file = "web.txt" output_file = "%s/%s.time.txt" % (outputDir, fileName) prefix = "https://www." f_in = open(input_file, 'r') lines = f_in.readlines() with open(output_file, "w") as f_out: for i, line in enumerate(lines): if i not in web_idxs: continue line = line.strip() url = prefix + line # open a web and sleep for a while printline = "Open #" + str(i + 1) + ": " + line print(printline) c_time = str(time.time()) logstamp = "%s %d\n" % (c_time, i + 1) f_out.write(logstamp) os.system("open -a /Applications/" + default_explore + " " + url) time.sleep(openItvl) # close a web and sleep for a while k.press_keys(['Command', 'w']) time.sleep(closeItvl) f_in.close()
def command(com, args, tw_user=None): global e if (e.is_set()): # System Tray Icon exit must trigger this exit(0) if not com.strip() or not isinstance(com, str): return False global inactive global user_full_name global user_prefix global config_file userin.twitter_user = tw_user print("You: " + com.upper()) doc = nlp(com) h = Helper(doc) if args["verbose"]: if len(doc) > 0: print("") print("{:12} {:12} {:12} {:12} {:12} {:12} {:12} {:12}". format("TEXT", "LEMMA", "POS", "TAG", "DEP", "SHAPE", "ALPHA", "STOP")) for token in doc: print( "{:12} {:12} {:12} {:12} {:12} {:12} {:12} {:12}" .format(token.text, token.lemma_, token.pos_, token.tag_, token.dep_, token.shape_, str(token.is_alpha), str(token.is_stop))) print("") if len(list(doc.noun_chunks)) > 0: print("{:12} {:12} {:12} {:12}".format( "TEXT", "ROOT.TEXT", "ROOT.DEP_", "ROOT.HEAD.TEXT")) for chunk in doc.noun_chunks: print("{:12} {:12} {:12} {:12}".format( chunk.text, chunk.root.text, chunk.root.dep_, chunk.root.head.text)) print("") if inactive and not ( h.directly_equal(["dragonfire", "hey"]) or (h.check_verb_lemma("wake") and h.check_nth_lemma(-1, "up")) or (h.check_nth_lemma(0, "dragon") and h.check_nth_lemma(1, "fire") and h.max_word_count(2))): return True if USER_ANSWERING['status']: if com.startswith("FIRST") or com.startswith( "THE FIRST") or com.startswith("SECOND") or com.startswith( "THE SECOND") or com.startswith( "THIRD") or com.startswith("THE THIRD"): USER_ANSWERING['status'] = False selection = None if com.startswith("FIRST") or com.startswith("THE FIRST"): selection = 0 elif com.startswith("SECOND") or com.startswith("THE SECOND"): selection = 1 elif com.startswith("THIRD") or com.startswith("THE THIRD"): selection = 2 if USER_ANSWERING['for'] == 'wikipedia': with nostderr(): search_query = USER_ANSWERING['options'][selection] try: wikiresult = wikipedia.search(search_query) if len(wikiresult) == 0: userin.say( "Sorry, " + user_prefix + ". But I couldn't find anything about " + search_query + " in Wikipedia.") return True wikipage = wikipedia.page(wikiresult[0]) wikicontent = "".join([ i if ord(i) < 128 else ' ' for i in wikipage.content ]) wikicontent = re.sub(r'\([^)]*\)', '', wikicontent) userin.execute(["sensible-browser", wikipage.url], search_query) userin.say(wikicontent) return True except requests.exceptions.ConnectionError: userin.execute([" "], "Wikipedia connection error.") userin.say( "Sorry, " + user_prefix + ". But I'm unable to connect to Wikipedia servers." ) return True except Exception: return True if h.directly_equal([ "dragonfire", "hey" ]) or (h.check_verb_lemma("wake") and h.check_nth_lemma(-1, "up")) or ( h.check_nth_lemma(0, "dragon") and h.check_nth_lemma(1, "fire") and h.max_word_count(2)): inactive = False userin.say( choice([ "Yes, " + user_prefix + ".", "Yes. I'm waiting.", "What is your order?", "Ready for the orders!", user_prefix + ", tell me your wish." ])) return True if (h.check_verb_lemma("go") and h.check_noun_lemma("sleep")) or ( h.check_verb_lemma("stop") and h.check_verb_lemma("listen")): inactive = True userin.execute([ "echo" ], "Dragonfire deactivated. To reactivate say 'Dragonfire!' or 'Wake Up!'" ) userin.say("I'm going to sleep") return True if h.directly_equal(["enough"]) or (h.check_verb_lemma("shut") and h.check_nth_lemma(-1, "up")): tts_kill() print("Dragonfire quiets.") return True if h.check_wh_lemma("what") and h.check_deps_contains("your name"): userin.execute([" "], "My name is Dragonfire.", True) return True if h.check_wh_lemma("what") and h.check_deps_contains("your gender"): userin.say( "I have a female voice but I don't have a gender identity. I'm a computer program, " + user_prefix + ".") return True if (h.check_wh_lemma("who") and h.check_text("I")) or (h.check_verb_lemma("say") and h.check_text("my") and check_lemma("name")): userin.execute([" "], user_full_name) userin.say("Your name is " + user_full_name + ", " + user_prefix + ".") return True if h.check_verb_lemma("open") or h.check_adj_lemma( "open") or h.check_verb_lemma("run") or h.check_verb_lemma( "start") or h.check_verb_lemma("show"): if h.check_text("blender"): userin.execute(["blender"], "Blender") userin.say("Blender 3D computer graphics software") return True if h.check_text("draw"): userin.execute(["libreoffice", "--draw"], "LibreOffice Draw") userin.say("Opening LibreOffice Draw") return True if h.check_text("impress"): userin.execute(["libreoffice", "--impress"], "LibreOffice Impress") userin.say("Opening LibreOffice Impress") return True if h.check_text("math"): userin.execute(["libreoffice", "--math"], "LibreOffice Math") userin.say("Opening LibreOffice Math") return True if h.check_text("writer"): userin.execute(["libreoffice", "--writer"], "LibreOffice Writer") userin.say("Opening LibreOffice Writer") return True if h.check_text("gimp") or (h.check_noun_lemma("photo") and (h.check_noun_lemma("editor") or h.check_noun_lemma("shop"))): userin.execute(["gimp"], "GIMP") userin.say("Opening the photo editor software.") return True if h.check_text("inkscape") or (h.check_noun_lemma("vector") and h.check_noun_lemma("graphic") ) or (h.check_text("vectorial") and h.check_text("drawing")): userin.execute(["inkscape"], "Inkscape") userin.say("Opening the vectorial drawing software.") return True if h.check_noun_lemma("office") and h.check_noun_lemma("suite"): userin.execute(["libreoffice"], "LibreOffice") userin.say("Opening LibreOffice") return True if h.check_text("kdenlive") or (h.check_noun_lemma("video") and h.check_noun_lemma("editor")): userin.execute(["kdenlive"], "Kdenlive") userin.say("Opening the video editor software.") return True if h.check_noun_lemma("browser") or h.check_noun_lemma( "chrome") or h.check_text("firefox"): userin.execute(["sensible-browser"], "Web Browser") userin.say("Web browser") return True if h.check_text("steam"): userin.execute(["steam"], "Steam") userin.say("Opening Steam Game Store") return True if h.check_text("files") or (h.check_noun_lemma("file") and h.check_noun_lemma("manager")): userin.execute(["dolphin"], "File Manager") # KDE neon userin.execute(["pantheon-files"], "File Manager") # elementary OS userin.execute(["nautilus", "--browser"], "File Manager") # Ubuntu userin.say("File Manager") return True if h.check_noun_lemma("camera"): userin.execute(["kamoso"], "Camera") # KDE neon userin.execute(["snap-photobooth"], "Camera") # elementary OS userin.execute(["cheese"], "Camera") # Ubuntu userin.say("Camera") return True if h.check_noun_lemma("calendar"): userin.execute(["korganizer"], "Calendar") # KDE neon userin.execute(["maya-calendar"], "Calendar") # elementary OS userin.execute(["orage"], "Calendar") # Ubuntu userin.say("Calendar") return True if h.check_noun_lemma("calculator"): userin.execute(["kcalc"], "Calculator") # KDE neon userin.execute(["pantheon-calculator"], "Calculator") # elementary OS userin.execute(["gnome-calculator"], "Calculator") # Ubuntu userin.say("Calculator") return True if h.check_noun_lemma("software") and h.check_text("center"): userin.execute(["plasma-discover"], "Software Center") # KDE neon userin.execute(["software-center"], "Software Center") # elementary OS & Ubuntu userin.say("Software Center") return True if h.check_lemma("be") and h.check_lemma("-PRON-") and ( h.check_lemma("lady") or h.check_lemma("woman") or h.check_lemma("girl")): config_file.update({'gender': 'female'}, Query().datatype == 'gender') config_file.remove(Query().datatype == 'callme') user_prefix = "My Lady" userin.say("Pardon, " + user_prefix + ".") return True if h.check_lemma("be") and h.check_lemma("-PRON-") and ( h.check_lemma("sir") or h.check_lemma("man") or h.check_lemma("boy")): config_file.update({'gender': 'male'}, Query().datatype == 'gender') config_file.remove(Query().datatype == 'callme') user_prefix = "Sir" userin.say("Pardon, " + user_prefix + ".") return True if h.check_lemma("call") and h.check_lemma("-PRON-"): title = "" for token in doc: if token.pos_ == "NOUN": title += ' ' + token.text title = title.strip() callme_config = config_file.search(Query().datatype == 'callme') if callme_config: config_file.update({'title': title}, Query().datatype == 'callme') else: config_file.insert({'datatype': 'callme', 'title': title}) user_prefix = title userin.say("OK, " + user_prefix + ".") return True # only for The United States today but prepared for all countries. Also # only for celsius degrees today. --> by Radan Liska :-) if h.is_wh_question() and h.check_lemma("temperature"): city = "" for ent in doc.ents: if ent.label_ == "GPE": city += ' ' + ent.text city = city.strip() if city: owm = pyowm.OWM("16d66c84e82424f0f8e62c3e3b27b574") reg = owm.city_id_registry() weather = owm.weather_at_id( reg.ids_for(city)[0][0]).get_weather() fmt = "The temperature in {} is {} degrees celsius" msg = fmt.format(city, weather.get_temperature('celsius')['temp']) userin.execute([" "], msg) userin.say(msg) return True if h.check_nth_lemma(0, "keyboard") or h.check_nth_lemma(0, "type"): n = len(doc[0].text) + 1 with nostdout(): with nostderr(): k = PyKeyboard() for character in com[n:]: k.tap_key(character) k.tap_key(" ") return True if h.directly_equal(["enter"]) or (h.check_adj_lemma("new") or h.check_noun_lemma("line")): with nostdout(): with nostderr(): k = PyKeyboard() k.tap_key(k.enter_key) return True if h.check_adj_lemma("new") and h.check_noun_lemma("tab"): with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.control_l_key, 't']) return True if h.check_verb_lemma("switch") and h.check_noun_lemma("tab"): with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.control_l_key, k.tab_key]) return True if h.directly_equal(["CLOSE", "ESCAPE"]): with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.control_l_key, 'w']) k.tap_key(k.escape_key) return True if h.check_lemma("back") and h.max_word_count(4): with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.alt_l_key, k.left_key]) return True if h.check_lemma("forward") and h.max_word_count(4): with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.alt_l_key, k.right_key]) return True if h.check_text("swipe") or h.check_text("scroll"): if h.check_text("left"): with nostdout(): with nostderr(): m = PyMouse() m.scroll(0, -5) return True if h.check_text("right"): with nostdout(): with nostderr(): m = PyMouse() m.scroll(0, 5) return True if h.check_text("up"): with nostdout(): with nostderr(): m = PyMouse() m.scroll(5, 0) return True if h.check_text("down"): with nostdout(): with nostderr(): m = PyMouse() m.scroll(-5, 0) return True if h.directly_equal(["PLAY", "PAUSE", "SPACEBAR"]): with nostdout(): with nostderr(): k = PyKeyboard() k.tap_key(" ") return True if ((h.check_text("shut") and h.check_text("down")) or (h.check_text("power") and h.check_text("off"))) and h.check_text("computer"): userin.execute(["sudo", "poweroff"], "Shutting down", True, 3) return True if h.check_nth_lemma(0, "goodbye") or h.check_nth_lemma( 0, "bye") or (h.check_verb_lemma("see") and h.check_noun_lemma("you") and h.check_noun_lemma("later")): userin.say("Goodbye, " + user_prefix) # raise KeyboardInterrupt thread.interrupt_main() return True if (h.check_lemma("search") or h.check_lemma("find")) and h.check_lemma("wikipedia"): with nostderr(): search_query = "" for token in doc: if not (token.lemma_ == "search" or token.lemma_ == "find" or token.lemma_ == "wikipedia" or token.is_stop): search_query += ' ' + token.text search_query = search_query.strip() if search_query: try: wikiresult = wikipedia.search(search_query) if len(wikiresult) == 0: userin.say( "Sorry, " + user_prefix + ". But I couldn't find anything about " + search_query + " in Wikipedia.") return True wikipage = wikipedia.page(wikiresult[0]) wikicontent = "".join([ i if ord(i) < 128 else ' ' for i in wikipage.content ]) wikicontent = re.sub(r'\([^)]*\)', '', wikicontent) userin.execute(["sensible-browser", wikipage.url], search_query) userin.say(wikicontent) return True except requests.exceptions.ConnectionError: userin.execute([" "], "Wikipedia connection error.") userin.say( "Sorry, " + user_prefix + ". But I'm unable to connect to Wikipedia servers." ) return True except wikipedia.exceptions.DisambiguationError as disambiguation: USER_ANSWERING['status'] = True USER_ANSWERING['for'] = 'wikipedia' USER_ANSWERING['reason'] = 'disambiguation' USER_ANSWERING['options'] = disambiguation.options[:3] notify = "Wikipedia disambiguation. Which one of these you meant?:\n - " + disambiguation.options[ 0] message = user_prefix + ", there is a disambiguation. Which one of these you meant? " + disambiguation.options[ 0] for option in disambiguation.options[1:3]: message += ", or " + option notify += "\n - " + option notify += '\nSay, for example: "THE FIRST ONE" to choose.' userin.execute([" "], notify) userin.say(message) return True except BaseException: pass if (h.check_lemma("search") or h.check_lemma("find")) and h.check_lemma("youtube"): with nostdout(): with nostderr(): search_query = "" for token in doc: if not (token.lemma_ == "search" or token.lemma_ == "find" or token.lemma_ == "youtube" or token.is_stop): search_query += ' ' + token.text search_query = search_query.strip() if search_query: info = youtube_dl.YoutubeDL({}).extract_info( 'ytsearch:' + search_query, download=False, ie_key='YoutubeSearch') if len(info['entries']) > 0: youtube_title = info['entries'][0]['title'] youtube_url = "https://www.youtube.com/watch?v=%s" % ( info['entries'][0]['id']) userin.execute(["sensible-browser", youtube_url], youtube_title) youtube_title = "".join([ i if ord(i) < 128 else ' ' for i in youtube_title ]) else: youtube_title = "No video found, " + user_prefix + "." userin.say(youtube_title) time.sleep(5) k = PyKeyboard() k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key('f') return True if (h.check_lemma("search") or h.check_lemma("find")) and ( h.check_lemma("google") or h.check_lemma("web") or h.check_lemma("internet")) and not h.check_lemma("image"): with nostdout(): with nostderr(): search_query = "" for token in doc: if not (token.lemma_ == "search" or token.lemma_ == "find" or token.lemma_ == "google" or token.lemma_ == "web" or token.lemma_ == "internet" or token.is_stop): search_query += ' ' + token.text search_query = search_query.strip() if search_query: tab_url = "http://google.com/?#q=" + search_query userin.execute(["sensible-browser", tab_url], search_query, True) return True if (h.check_lemma("search") or h.check_lemma("find")) and ( h.check_lemma("google") or h.check_lemma("web") or h.check_lemma("internet")) and h.check_lemma("image"): with nostdout(): with nostderr(): search_query = "" for token in doc: if not (token.lemma_ == "search" or token.lemma_ == "find" or token.lemma_ == "google" or token.lemma_ == "web" or token.lemma_ == "internet" or token.lemma_ == "image" or token.is_stop): search_query += ' ' + token.text search_query = search_query.strip() if search_query: tab_url = "http://google.com/?#q=" + search_query + "&tbm=isch" userin.execute(["sensible-browser", tab_url], search_query, True) return True arithmetic_response = arithmetic_parse(com) if arithmetic_response: userin.say(arithmetic_response) else: learnerresponse = learner.respond(com) if learnerresponse: userin.say(learnerresponse) else: if not omniscient.respond(com, not args["silent"], userin, user_prefix, args["server"]): dc_response = dc.respond(com, user_prefix) if dc_response: userin.say(dc_response)
print " .o00@@@@0oo.. ..oo0@@@@00o." time.sleep(0.02) print " .o00@@@@@00oooo.........oooo000@@@@@00o." time.sleep(0.02) print " .oo000@@@@@@@@@@@@@@@@@@@@000oo." time.sleep(0.02) print " ...oooooooooooooo..." for i in range(rows): time.sleep(0.03) print "" k.type_string('htop -d 50') time.sleep(0.1) k.tap_key('Return') time.sleep(0.01) k.press_keys(['Alt_L', '\\']) time.sleep(0.1) k.press_keys(['Alt_L', 'h']) time.sleep(0.01) k.press_keys(['Alt_L', '-']) time.sleep(0.1) k.type_string('mutt') time.sleep(0.01) k.tap_key('Return') time.sleep(0.01) k.press_key('Alt_L') k.tap_key(character='Up', n=8) time.sleep(0.01) k.tap_key('-') time.sleep(0.1) k.tap_key(character='Down', n=2)
def command(self, com): """Function that serves as the entry point for each one of the user commands. This function goes through these steps for each one of user's commands, respectively: - Search across the built-in commands via a simple if-else control flow. - Try to get a response from :func:`dragonfire.arithmetic.arithmetic_parse` function. - Try to get a response from :func:`dragonfire.learn.Learner.respond` method. - Try to get a answer from :func:`dragonfire.omniscient.Omniscient.respond` method. - Try to get a response from :func:`dragonfire.deepconv.DeepConversation.respond` method. Args: com (str): User's command. Returns: str: Response. """ if not self.args["server"]: global config_file global e if (e.is_set()): # System Tray Icon exit must trigger this exit(0) args = self.args userin = self.userin user_full_name = self.user_full_name user_prefix = self.user_prefix if self.testing: config_file = self.config_file if isinstance(com, str) and com: com = com.strip() else: return False print("You: " + com.upper()) doc = nlp(com) h = Helper(doc) if args["verbose"]: userin.pretty_print_nlp_parsing_results(doc) if self.inactive and not (h.directly_equal(["dragonfire", "hey"]) or (h.check_verb_lemma("wake") and h.check_nth_lemma(-1, "up")) or (h.check_nth_lemma(0, "dragon") and h.check_nth_lemma(1, "fire") and h.max_word_count(2))): return "" if USER_ANSWERING['status']: if com.startswith("FIRST") or com.startswith("THE FIRST") or com.startswith("SECOND") or com.startswith("THE SECOND") or com.startswith("THIRD") or com.startswith("THE THIRD"): USER_ANSWERING['status'] = False selection = None if com.startswith("FIRST") or com.startswith("THE FIRST"): selection = 0 elif com.startswith("SECOND") or com.startswith("THE SECOND"): selection = 1 elif com.startswith("THIRD") or com.startswith("THE THIRD"): selection = 2 if USER_ANSWERING['for'] == 'wikipedia': with nostderr(): search_query = USER_ANSWERING['options'][selection] try: wikiresult = wikipedia.search(search_query) if len(wikiresult) == 0: userin.say("Sorry, " + user_prefix + ". But I couldn't find anything about " + search_query + " in Wikipedia.") return True wikipage = wikipedia.page(wikiresult[0]) wikicontent = "".join([i if ord(i) < 128 else ' ' for i in wikipage.content]) wikicontent = re.sub(r'\([^)]*\)', '', wikicontent) userin.execute(["sensible-browser", wikipage.url], search_query) return userin.say(wikicontent, cmd=["sensible-browser", wikipage.url]) except requests.exceptions.ConnectionError: userin.execute([" "], "Wikipedia connection error.") return userin.say("Sorry, " + user_prefix + ". But I'm unable to connect to Wikipedia servers.") except Exception: return False if h.directly_equal(["dragonfire", "hey"]) or (h.check_verb_lemma("wake") and h.check_nth_lemma(-1, "up")) or (h.check_nth_lemma(0, "dragon") and h.check_nth_lemma(1, "fire") and h.max_word_count(2)): self.inactive = False return userin.say(choice([ "Yes, " + user_prefix + ".", "Yes. I'm waiting.", "What is your order?", "Ready for the orders!", user_prefix.capitalize() + ", tell me your wish." ])) if (h.check_verb_lemma("go") and h.check_noun_lemma("sleep")) or (h.check_verb_lemma("stop") and h.check_verb_lemma("listen")): self.inactive = True userin.execute(["echo"], "Dragonfire deactivated. To reactivate say 'Dragonfire!' or 'Wake Up!'") return userin.say("I'm going to sleep") if h.directly_equal(["enough"]) or (h.check_verb_lemma("shut") and h.check_nth_lemma(-1, "up")): tts_kill() msg = "Dragonfire quiets." print(msg) return msg if h.check_wh_lemma("what") and h.check_deps_contains("your name"): return userin.execute([" "], "My name is Dragonfire.", True) if h.check_wh_lemma("what") and h.check_deps_contains("your gender"): return userin.say("I have a female voice but I don't have a gender identity. I'm a computer program, " + user_prefix + ".") if (h.check_wh_lemma("who") and h.check_text("I")) or (h.check_verb_lemma("say") and h.check_text("my") and h.check_lemma("name")): userin.execute([" "], user_full_name) return userin.say("Your name is " + user_full_name + ", " + user_prefix + ".") if h.check_verb_lemma("open") or h.check_adj_lemma("open") or h.check_verb_lemma("run") or h.check_verb_lemma("start") or h.check_verb_lemma("show"): if h.check_text("blender"): userin.execute(["blender"], "Blender") return userin.say("Blender 3D computer graphics software") if h.check_text("draw"): userin.execute(["libreoffice", "--draw"], "LibreOffice Draw") return userin.say("Opening LibreOffice Draw") if h.check_text("impress"): userin.execute(["libreoffice", "--impress"], "LibreOffice Impress") return userin.say("Opening LibreOffice Impress") if h.check_text("math"): userin.execute(["libreoffice", "--math"], "LibreOffice Math") return userin.say("Opening LibreOffice Math") if h.check_text("writer"): userin.execute(["libreoffice", "--writer"], "LibreOffice Writer") return userin.say("Opening LibreOffice Writer") if h.check_text("gimp") or (h.check_noun_lemma("photo") and (h.check_noun_lemma("editor") or h.check_noun_lemma("shop"))): userin.execute(["gimp"], "GIMP") return userin.say("Opening the photo editor software.") if h.check_text("inkscape") or (h.check_noun_lemma("vector") and h.check_noun_lemma("graphic")) or (h.check_text("vectorial") and h.check_text("drawing")): userin.execute(["inkscape"], "Inkscape") return userin.say("Opening the vectorial drawing software.") if h.check_noun_lemma("office") and h.check_noun_lemma("suite"): userin.execute(["libreoffice"], "LibreOffice") return userin.say("Opening LibreOffice") if h.check_text("kdenlive") or (h.check_noun_lemma("video") and h.check_noun_lemma("editor")): userin.execute(["kdenlive"], "Kdenlive") return userin.say("Opening the video editor software.") if h.check_noun_lemma("browser") or h.check_text("chrome") or h.check_text("firefox"): userin.execute(["sensible-browser"], "Web Browser") return userin.say("Web browser") if h.check_text("steam"): userin.execute(["steam"], "Steam") return userin.say("Opening Steam Game Store") if h.check_text("files") or (h.check_noun_lemma("file") and h.check_noun_lemma("manager")): userin.execute(["dolphin"], "File Manager") # KDE neon userin.execute(["pantheon-files"], "File Manager") # elementary OS userin.execute(["nautilus", "--browser"], "File Manager") # Ubuntu return userin.say("File Manager") if h.check_noun_lemma("camera"): userin.execute(["kamoso"], "Camera") # KDE neon userin.execute(["snap-photobooth"], "Camera") # elementary OS userin.execute(["cheese"], "Camera") # Ubuntu return userin.say("Camera") if h.check_noun_lemma("calendar"): userin.execute(["korganizer"], "Calendar") # KDE neon userin.execute(["maya-calendar"], "Calendar") # elementary OS userin.execute(["orage"], "Calendar") # Ubuntu return userin.say("Calendar") if h.check_noun_lemma("calculator"): userin.execute(["kcalc"], "Calculator") # KDE neon userin.execute(["pantheon-calculator"], "Calculator") # elementary OS userin.execute(["gnome-calculator"], "Calculator") # Ubuntu return userin.say("Calculator") if h.check_noun_lemma("software") and h.check_text("center"): userin.execute(["plasma-discover"], "Software Center") # KDE neon userin.execute(["software-center"], "Software Center") # elementary OS & Ubuntu return userin.say("Software Center") if h.check_lemma("be") and h.check_lemma("-PRON-") and (h.check_lemma("lady") or h.check_lemma("woman") or h.check_lemma("girl")): config_file.update({'gender': 'female'}, Query().datatype == 'gender') config_file.remove(Query().datatype == 'callme') user_prefix = "my lady" return userin.say("Pardon, " + user_prefix + ".") if h.check_lemma("be") and h.check_lemma("-PRON-") and (h.check_lemma("sir") or h.check_lemma("man") or h.check_lemma("boy")): config_file.update({'gender': 'male'}, Query().datatype == 'gender') config_file.remove(Query().datatype == 'callme') user_prefix = "sir" return userin.say("Pardon, " + user_prefix + ".") if h.check_lemma("call") and h.check_lemma("-PRON-"): title = "" for token in doc: if token.pos_ == "NOUN": title += ' ' + token.text title = title.strip() if not args["server"]: callme_config = config_file.search(Query().datatype == 'callme') if callme_config: config_file.update({'title': title}, Query().datatype == 'callme') else: config_file.insert({'datatype': 'callme', 'title': title}) user_prefix = title return userin.say("OK, " + user_prefix + ".") if h.is_wh_question() and h.check_lemma("temperature"): city = "" for ent in doc.ents: if ent.label_ == "GPE": city += ' ' + ent.text city = city.strip() if city: owm = pyowm.OWM("16d66c84e82424f0f8e62c3e3b27b574") reg = owm.city_id_registry() try: weather = owm.weather_at_id(reg.ids_for(city)[0][0]).get_weather() fmt = "The temperature in {} is {} degrees celsius" msg = fmt.format(city, weather.get_temperature('celsius')['temp']) userin.execute([" "], msg) return userin.say(msg) except IndexError: msg = "Sorry, " + user_prefix + " but I couldn't find a city named " + city + " on the internet." userin.execute([" "], msg) return userin.say(msg) if (h.check_nth_lemma(0, "keyboard") or h.check_nth_lemma(0, "type")) and not args["server"]: n = len(doc[0].text) + 1 with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: for character in com[n:]: k.tap_key(character) k.tap_key(" ") return "keyboard" if (h.directly_equal(["enter"]) or (h.check_adj_lemma("new") and h.check_noun_lemma("line"))) and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.tap_key(k.enter_key) return "enter" if h.check_adj_lemma("new") and h.check_noun_lemma("tab") and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.press_keys([k.control_l_key, 't']) return "new tab" if h.check_verb_lemma("switch") and h.check_noun_lemma("tab") and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.press_keys([k.control_l_key, k.tab_key]) return "switch tab" if h.directly_equal(["CLOSE", "ESCAPE"]) and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.press_keys([k.control_l_key, 'w']) k.tap_key(k.escape_key) return "close" if h.check_lemma("back") and h.max_word_count(4) and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.press_keys([k.alt_l_key, k.left_key]) return "back" if h.check_lemma("forward") and h.max_word_count(4) and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.press_keys([k.alt_l_key, k.right_key]) return "forward" if (h.check_text("swipe") or h.check_text("scroll")) and not args["server"]: if h.check_text("left"): with nostdout(): with nostderr(): m = PyMouse() if not self.testing: m.scroll(0, -5) return "swipe left" if h.check_text("right"): with nostdout(): with nostderr(): m = PyMouse() if not self.testing: m.scroll(0, 5) return "swipe right" if h.check_text("up"): with nostdout(): with nostderr(): m = PyMouse() if not self.testing: m.scroll(5, 0) return "swipe up" if h.check_text("down"): with nostdout(): with nostderr(): m = PyMouse() if not self.testing: m.scroll(-5, 0) return "swipe down" if h.directly_equal(["PLAY", "PAUSE", "SPACEBAR"]) and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.tap_key(" ") return "play" if ((h.check_text("shut") and h.check_text("down")) or (h.check_text("power") and h.check_text("off"))) and h.check_text("computer") and not args["server"]: return userin.execute(["sudo", "poweroff"], "Shutting down", True, 3) if h.check_nth_lemma(0, "goodbye") or h.check_nth_lemma(0, "bye") or (h.check_verb_lemma("see") and h.check_text("you") and h.check_adv_lemma("later")): response = userin.say("Goodbye, " + user_prefix) if not args["server"] and not self.testing: # raise KeyboardInterrupt thread.interrupt_main() return response if (h.check_lemma("search") or h.check_lemma("find")) and h.check_lemma("wikipedia"): with nostderr(): search_query = "" for token in doc: if not (token.lemma_ == "search" or token.lemma_ == "find" or token.lemma_ == "wikipedia" or token.is_stop): search_query += ' ' + token.text search_query = search_query.strip() if search_query: try: wikiresult = wikipedia.search(search_query) if len(wikiresult) == 0: userin.say("Sorry, " + user_prefix + ". But I couldn't find anything about " + search_query + " in Wikipedia.") return True wikipage = wikipedia.page(wikiresult[0]) wikicontent = "".join([i if ord(i) < 128 else ' ' for i in wikipage.content]) wikicontent = re.sub(r'\([^)]*\)', '', wikicontent) userin.execute(["sensible-browser", wikipage.url], search_query) return userin.say(wikicontent, cmd=["sensible-browser", wikipage.url]) except requests.exceptions.ConnectionError: userin.execute([" "], "Wikipedia connection error.") return userin.say("Sorry, " + user_prefix + ". But I'm unable to connect to Wikipedia servers.") except wikipedia.exceptions.DisambiguationError as disambiguation: USER_ANSWERING['status'] = True USER_ANSWERING['for'] = 'wikipedia' USER_ANSWERING['reason'] = 'disambiguation' USER_ANSWERING['options'] = disambiguation.options[:3] notify = "Wikipedia disambiguation. Which one of these you meant?:\n - " + disambiguation.options[0] msg = user_prefix + ", there is a disambiguation. Which one of these you meant? " + disambiguation.options[0] for option in disambiguation.options[1:3]: msg += ", or " + option notify += "\n - " + option notify += '\nSay, for example: "THE FIRST ONE" to choose.' userin.execute([" "], notify) return userin.say(msg) except BaseException: pass if (h.check_lemma("search") or h.check_lemma("find")) and h.check_lemma("youtube"): with nostdout(): with nostderr(): search_query = "" for token in doc: if not (token.lemma_ == "search" or token.lemma_ == "find" or token.lemma_ == "youtube" or token.is_stop): search_query += ' ' + token.text search_query = search_query.strip() if search_query: info = youtube_dl.YoutubeDL({}).extract_info('ytsearch:' + search_query, download=False, ie_key='YoutubeSearch') if len(info['entries']) > 0: youtube_title = info['entries'][0]['title'] youtube_url = "https://www.youtube.com/watch?v=%s" % (info['entries'][0]['id']) userin.execute(["sensible-browser", youtube_url], youtube_title) youtube_title = "".join([i if ord(i) < 128 else ' ' for i in youtube_title]) response = userin.say(youtube_title, ["sensible-browser", youtube_url]) else: youtube_title = "No video found, " + user_prefix + "." response = userin.say(youtube_title) k = PyKeyboard() if not args["server"] and not self.testing: time.sleep(5) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key('f') return response if (h.check_lemma("search") or h.check_lemma("find")) and (h.check_lemma("google") or h.check_lemma("web") or h.check_lemma("internet")) and not h.check_lemma("image"): with nostdout(): with nostderr(): search_query = "" for token in doc: if not (token.lemma_ == "search" or token.lemma_ == "find" or token.lemma_ == "google" or token.lemma_ == "web" or token.lemma_ == "internet" or token.is_stop): search_query += ' ' + token.text search_query = search_query.strip() if search_query: tab_url = "http://google.com/?#q=" + search_query return userin.execute(["sensible-browser", tab_url], search_query, True) if (h.check_lemma("search") or h.check_lemma("find")) and (h.check_lemma("google") or h.check_lemma("web") or h.check_lemma("internet")) and h.check_lemma("image"): with nostdout(): with nostderr(): search_query = "" for token in doc: if not (token.lemma_ == "search" or token.lemma_ == "find" or token.lemma_ == "google" or token.lemma_ == "web" or token.lemma_ == "internet" or token.lemma_ == "image" or token.is_stop): search_query += ' ' + token.text search_query = search_query.strip() if search_query: tab_url = "http://google.com/?#q=" + search_query + "&tbm=isch" return userin.execute(["sensible-browser", tab_url], search_query, True) original_com = com com = coref.resolve(com) if args["verbose"]: print("After Coref Resolution: " + com) arithmetic_response = arithmetic_parse(com) if arithmetic_response: return userin.say(arithmetic_response) else: learner_response = learner.respond(com) if learner_response: return userin.say(learner_response) else: omniscient_response = omniscient.respond(com, not args["silent"], userin, user_prefix, args["server"]) if omniscient_response: return omniscient_response else: dc_response = dc.respond(original_com, user_prefix) if dc_response: return userin.say(dc_response)
def command(com, args, tw_user=None): global e if (e.is_set()): # System Tray Icon exit must trigger this exit(0) if not com or not isinstance(com, str): return False original_com = com global inactive global user_full_name global user_prefix global config_file userin.twitter_user = tw_user com = com.upper() com = re.sub(r'([^\s\w]|_)+', '', com).strip() print("You: " + com) if inactive and com not in ("DRAGONFIRE", "DRAGON FIRE", "WAKE UP", "HEY"): return True if USER_ANSWERING['status']: if com.startswith("FIRST") or com.startswith( "THE FIRST") or com.startswith("SECOND") or com.startswith( "THE SECOND") or com.startswith( "THIRD") or com.startswith("THE THIRD"): USER_ANSWERING['status'] = False selection = None if com.startswith("FIRST") or com.startswith("THE FIRST"): selection = 0 elif com.startswith("SECOND") or com.startswith("THE SECOND"): selection = 1 elif com.startswith("THIRD") or com.startswith("THE THIRD"): selection = 2 if USER_ANSWERING['for'] == 'wikipedia': with nostderr(): search_query = USER_ANSWERING['options'][selection] try: wikiresult = wikipedia.search(search_query) if len(wikiresult) == 0: userin.say( "Sorry, " + user_prefix + ". But I couldn't find anything about " + search_query + " in Wikipedia.") return True wikipage = wikipedia.page(wikiresult[0]) wikicontent = "".join([ i if ord(i) < 128 else ' ' for i in wikipage.content ]) wikicontent = re.sub(r'\([^)]*\)', '', wikicontent) userin.define_and_execute( ["sensible-browser", wikipage.url], search_query) userin.say(wikicontent) return True except requests.exceptions.ConnectionError: userin.define_and_execute( [" "], "Wikipedia connection error.") userin.say( "Sorry, " + user_prefix + ". But I'm unable to connect to Wikipedia servers.") return True except Exception: return True if com in ("DRAGONFIRE", "DRAGON FIRE", "WAKE UP", "HEY"): tts_kill() inactive = False userin.define([" "], " ") userin.say(choice([ "Yes, " + user_prefix + ".", "Yes. I'm waiting.", "What is your order?", "Ready for the orders!", user_prefix + ", tell me your wish." ])) elif "GO TO SLEEP" == com: tts_kill() inactive = True userin.define_and_execute( ["echo"], "Dragonfire deactivated. To reactivate say 'Dragonfire!' or 'Wake Up!'") userin.say("I'm going to sleep") elif com in ("ENOUGH", "SHUT UP"): print("Dragonfire quiets.") tts_kill() elif VirtualAssistant.exact_match(com): return True # the request has been handled elif VirtualAssistant.in_match(com): return True # the request has been handled elif ("SEARCH" in com or "FIND" in com) and VirtualAssistant.search_command(com): pass # the request has been handled elif com in ("MY TITLE IS LADY", "I'M A LADY", "I'M A WOMAN", "I'M A GIRL"): tts_kill() config_file.update({ 'gender': 'female' }, Query().datatype == 'gender') user_prefix = "My Lady" userin.define([" "], " ") userin.say("Pardon, " + user_prefix + ".") elif com in ("MY TITLE IS SIR", "I'M A MAN", "I'M A BOY"): tts_kill() config_file.update({ 'gender': 'male' }, Query().datatype == 'gender') user_prefix = "Sir" userin.define([" "], " ") userin.say("Pardon, " + user_prefix + ".") elif com.startswith("CALL ME "): tts_kill() callme_config = config_file.search(Query().datatype == 'callme') if callme_config: config_file.update({ 'title': original_com[8:].lower() }, Query().datatype == 'callme') else: config_file.insert({ 'datatype': 'callme', 'title': original_com[8:].lower() }) user_prefix = original_com[8:].lower().encode("utf8") userin.define([" "], " ") userin.say("Pardon, " + user_prefix + ".") # only for The United States today but prepared for all countries. Also # only for celsius degrees today. --> by Radan Liska :-) elif "WHAT" in com and "TEMPERATURE" in com: tts_kill() capture = re.search( "(?:WHAT IS|WHAT'S) THE TEMPERATURE (?:IN|ON|AT|OF)? (?P<city>.*)", com) if capture: city = capture.group('city') owm = pyowm.OWM("16d66c84e82424f0f8e62c3e3b27b574") reg = owm.city_id_registry() weather = owm.weather_at_id( reg.ids_for(city)[0][0]).get_weather() fmt = "The temperature in {} is {} degrees celsius" msg = fmt.format(city, weather.get_temperature('celsius')['temp']) userin.define_and_execute([" "], msg) userin.say(msg) elif "FILE MANAGER" in com or "OPEN FILES" == com: tts_kill() userin.define_and_execute(["dolphin"], "File Manager") # KDE neon userin.define_and_execute(["pantheon-files"], "File Manager") # elementary OS userin.define_and_execute(["nautilus", "--browser"], "File Manager") # Ubuntu userin.say("File Manager") elif "OPEN CAMERA" == com: tts_kill() userin.define_and_execute(["kamoso"], "Camera") # KDE neon userin.define_and_execute(["snap-photobooth"], "Camera") # elementary OS userin.define_and_execute(["cheese"], "Camera") # Ubuntu userin.say("Camera") elif "OPEN CALENDAR" == com: tts_kill() userin.define_and_execute(["korganizer"], "Calendar") # KDE neon userin.define_and_execute(["maya-calendar"], "Calendar") # elementary OS userin.define_and_execute(["orage"], "Calendar") # Ubuntu userin.say("Calendar") elif "OPEN CALCULATOR" == com: tts_kill() userin.define_and_execute(["kcalc"], "Calculator") # KDE neon userin.define_and_execute(["pantheon-calculator"], "Calculator") # elementary OS userin.define_and_execute(["gnome-calculator"], "Calculator") # Ubuntu userin.say("Calculator") elif "SOFTWARE CENTER" in com: tts_kill() userin.define_and_execute(["plasma-discover"], "Software Center") # KDE neon userin.define_and_execute( ["software-center"], "Software Center") # elementary OS & Ubuntu userin.say("Software Center") elif com.startswith("KEYBOARD "): tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() for character in original_com[9:]: k.tap_key(character) k.tap_key(" ") elif com == "ENTER": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.tap_key(k.enter_key) elif com == "NEW TAB": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.control_l_key, 't']) elif com == "SWITCH TAB": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.control_l_key, k.tab_key]) elif com in ("CLOSE", "ESCAPE"): tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.control_l_key, 'w']) k.tap_key(k.escape_key) elif com == "GO BACK": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.alt_l_key, k.left_key]) elif com == "GO FORWARD": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.alt_l_key, k.right_key]) elif com == "SCROLL LEFT": tts_kill() with nostdout(): with nostderr(): m = PyMouse() m.scroll(0, -5) elif com == "SCROLL RIGHT": tts_kill() with nostdout(): with nostderr(): m = PyMouse() m.scroll(0, 5) elif com == "SCROLL UP": tts_kill() with nostdout(): with nostderr(): m = PyMouse() m.scroll(5, 0) elif com == "SCROLL DOWN": tts_kill() with nostdout(): with nostderr(): m = PyMouse() m.scroll(-5, 0) elif com in ("PLAY", "PAUSE", "SPACEBAR"): tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.tap_key(" ") elif "SHUTDOWN THE COMPUTER" == com: tts_kill() userin.define(["sudo", "poweroff"], "Shutting down") userin.say("Shutting down") userin.execute(3) elif com in ("GOODBYE", "GOOD BYE", "BYE BYE", "SEE YOU LATER", "CATCH YOU LATER"): tts_kill() userin.define([" "], " ") userin.say("Goodbye, " + user_prefix) # raise KeyboardInterrupt thread.interrupt_main() elif "WIKIPEDIA" in com and ("SEARCH" in com or "FIND" in com): tts_kill() with nostderr(): capture = re.search( "(?:SEARCH|FIND) (?P<query>.*) (?:IN|ON|AT|USING)? WIKIPEDIA", com) if capture: search_query = capture.group('query') try: wikiresult = wikipedia.search(search_query) if len(wikiresult) == 0: userin.say("Sorry, " + user_prefix + ". But I couldn't find anything about " + search_query + " in Wikipedia.") return True wikipage = wikipedia.page(wikiresult[0]) wikicontent = "".join([ i if ord(i) < 128 else ' ' for i in wikipage.content ]) wikicontent = re.sub(r'\([^)]*\)', '', wikicontent) userin.define_and_execute( ["sensible-browser", wikipage.url], search_query) userin.say(wikicontent) except requests.exceptions.ConnectionError: userin.define_and_execute( [" "], "Wikipedia connection error.") userin.say( "Sorry, " + user_prefix + ". But I'm unable to connect to Wikipedia servers." ) except wikipedia.exceptions.DisambiguationError as disambiguation: USER_ANSWERING['status'] = True USER_ANSWERING['for'] = 'wikipedia' USER_ANSWERING['reason'] = 'disambiguation' USER_ANSWERING['options'] = disambiguation.options[:3] notify = "Wikipedia disambiguation. Which one of these you meant?:\n - " + \ disambiguation.options[0] message = user_prefix + ", there is a disambiguation. Which one of these you meant? " + \ disambiguation.options[0] for option in disambiguation.options[1:3]: message += ", or " + option notify += "\n - " + option notify += '\nSay, for example: "THE FIRST ONE" to choose.' userin.define_and_execute([" "], notify) userin.say(message) except BaseException: pass elif "YOUTUBE" in com and ("SEARCH" in com or "FIND" in com): tts_kill() with nostdout(): with nostderr(): capture = re.search( "(?:SEARCH|FIND) (?P<query>.*) (?:IN|ON|AT|USING)? YOUTUBE", com) if capture: search_query = capture.group('query') info = youtube_dl.YoutubeDL({}).extract_info( 'ytsearch:' + search_query, download=False, ie_key='YoutubeSearch') if len(info['entries']) > 0: youtube_title = info['entries'][0]['title'] youtube_url = "https://www.youtube.com/watch?v=%s" % ( info['entries'][0]['id']) userin.define(["sensible-browser", youtube_url], youtube_title) youtube_title = "".join([ i if ord(i) < 128 else ' ' for i in youtube_title ]) else: youtube_title = "No video found, " + user_prefix + "." userin.define(" ", " ") userin.execute(0) userin.say(youtube_title) time.sleep(5) k = PyKeyboard() k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key('f') elif ("GOOGLE" in com or "WEB" in com) and "IMAGE" not in com and ("SEARCH" in com or "FIND" in com): tts_kill() with nostdout(): with nostderr(): capture = re.search( "(?:SEARCH|FIND) (?P<query>.*) (?:IN|ON|AT|USING)? (?:GOOGLE|WEB)?", com) if capture: search_query = capture.group('query') tab_url = "http://google.com/?#q=" + search_query userin.define_and_execute( ["sensible-browser", tab_url], search_query) userin.say(search_query) elif ("GOOGLE" in com or "WEB" in com) and "IMAGE" in com and ("SEARCH" in com or "FIND" in com): tts_kill() with nostdout(): with nostderr(): capture = re.search("(?:SEARCH IMAGES OF|FIND IMAGES OF|SEARCH|FIND) " "(?P<query>.*) (?:IN|ON|AT|USING)? " "(?:GOOGLE|WEB|GOOGLE IMAGES|WEB IMAGES)?", com) if capture: search_query = capture.group('query') tab_url = "http://google.com/?#q=" + search_query + "&tbm=isch" userin.define(["sensible-browser", tab_url], search_query) userin.execute(0) userin.say(search_query) else: tts_kill() arithmetic_response = arithmeticParser(com) if arithmetic_response: userin.define([" "], " ") userin.say(arithmetic_response) else: learn_response = learn_.respond(original_com) if learn_response: userin.define([" "], " ") userin.say(learn_response) else: if not omniscient_.respond(original_com, not args["silent"], userin, user_prefix, args["server"]): dc_response = dc.respond(original_com, user_prefix) if dc_response: userin.define([" "], " ") userin.say(dc_response)
''' 窗口标题:你一定爱读的极简未来史-克里斯托弗·巴纳特-微信读书 窗口类名:MozillaWindowClass 窗口句柄:16125580 ''' import win32con import win32gui from pymouse import PyMouse from pykeyboard import PyKeyboard k = PyKeyboard() k.press_keys([k.control_l_key, 'd'])
#logFile = "%r.pcap" %fileName #cmd = "sudo ./flow_track.sh %r %r" %(outputDir, logFile) #os.system(cmd) with open(open_file, "w") as myfile: for run in xrange(0, iterNum): # for wi in xrange(0,len(websites)): for wi in xrange(0+startNum-1, webNum+startNum-1): website = websites[wi] c_time = datetime.datetime.now().time() c_time = c_time.isoformat() logstamp = "%s,%s\n" % (c_time, website) if DEBUG3: print "%s-run%d-web%d: %s" % (c_time, run, wi, website) myfile.write(logstamp) #cmd = "sudo ./flow_track.sh %s %s %d %s" %(outputDir,fileName, openItvl, website) #os.system(cmd) cmd = "open -a /Applications/%s http://%s &" %(default_explore, website) os.system(cmd) time.sleep(openItvl) k.press_keys(['Command','w']) time.sleep(closeItvl) #if DEBUG2: print "Kill tcpdump" #cmd = "pkill -9 -f tcpdump" #os.system(cmd)
def command(com, args): if (type(com) is not str) or com == "": return False original_com = com global inactive global user_full_name global user_prefix global config_file global e com = com.upper() print("You: " + com) if inactive == 1 and com not in ("DRAGONFIRE", "DRAGON FIRE", "WAKE UP", "HEY"): return True if USER_ANSWERING['status']: if com.startswith("FIRST") or com.startswith( "THE FIRST") or com.startswith("SECOND") or com.startswith( "THE SECOND") or com.startswith( "THIRD") or com.startswith("THE THIRD"): USER_ANSWERING['status'] = False choice = None if com.startswith("FIRST") or com.startswith("THE FIRST"): choice = 0 elif com.startswith("SECOND") or com.startswith("THE SECOND"): choice = 1 elif com.startswith("THIRD") or com.startswith("THE THIRD"): choice = 2 if USER_ANSWERING['for'] == 'wikipedia': with nostderr(): search_query = USER_ANSWERING['options'][choice] try: wikiresult = wikipedia.search(search_query) if len(wikiresult) == 0: userin.say( "Sorry, " + user_prefix + ". But I couldn't find anything about " + search_query + " in Wikipedia.") return True wikipage = wikipedia.page(wikiresult[0]) wikicontent = "".join([ i if ord(i) < 128 else ' ' for i in wikipage.content ]) wikicontent = re.sub(r'\([^)]*\)', '', wikicontent) userin.define(["sensible-browser", wikipage.url], search_query) userin.execute(0) userin.say(wikicontent) return True except requests.exceptions.ConnectionError: userin.define([" "], "Wikipedia connection error.") userin.execute(0) userin.say( "Sorry, " + user_prefix + ". But I'm unable to connect to Wikipedia servers." ) return True except: return True if com in ("DRAGONFIRE", "DRAGON FIRE", "WAKE UP", "HEY"): tts_kill() inactive = 0 userin.define([" "], " ") words_dragonfire = { 0: "Yes, " + user_prefix + ".", 1: "Yes. I'm waiting.", 2: "What is your orders?" } userin.say(words_dragonfire[randint(0, 2)]) elif "GO TO SLEEP" == com: tts_kill() inactive = 1 userin.define([ "echo" ], "Dragonfire deactivated. To reactivate say 'Dragonfire!' or 'Wake Up!'" ) userin.execute(0) userin.say("I'm going to sleep") elif com in ("ENOUGH", "SHUT UP"): print("Dragonfire quiets.") tts_kill() elif com in ("WHO AM I", "SAY MY NAME"): tts_kill() userin.define([" "], user_full_name) userin.execute(0) userin.say("Your name is " + user_full_name + ", " + user_prefix + ".") elif com in ("MY TITLE IS LADY", "I'M A LADY", "I'M A WOMAN", "I'M A GIRL"): tts_kill() config_file.update({'gender': 'female'}, Query().datatype == 'gender') user_prefix = "My Lady" userin.define([" "], " ") userin.say("Pardon, " + user_prefix + ".") elif com in ("MY TITLE IS SIR", "I'M A MAN", "I'M A BOY"): tts_kill() config_file.update({'gender': 'male'}, Query().datatype == 'gender') user_prefix = "Sir" userin.define([" "], " ") userin.say("Pardon, " + user_prefix + ".") elif com.startswith("CALL ME "): tts_kill() callme_config = config_file.search(Query().datatype == 'callme') if callme_config: config_file.update({'title': original_com[8:].lower()}, Query().datatype == 'callme') else: config_file.insert({ 'datatype': 'callme', 'title': original_com[8:].lower() }) user_prefix = original_com[8:].lower().encode("utf8") userin.define([" "], " ") userin.say("Pardon, " + user_prefix + ".") elif "WHAT IS YOUR NAME" == com: tts_kill() userin.define([" "], "My name is Dragonfire.") userin.execute(0) userin.say("My name is Dragon Fire.") elif "WHAT" in com and "TEMPERATURE" in com: # only for The United States today but prepared for all countries. Also only for celsius degrees today. --> by Radan Liska :-) tts_kill() capture = re.search( "(?:WHAT IS|WHAT'S) THE TEMPERATURE (?:IN|ON|AT|OF)? (?P<city>.*)", com) if capture: city = capture.group('city') owm = pyowm.OWM("16d66c84e82424f0f8e62c3e3b27b574") reg = owm.city_id_registry() weather = owm.weather_at_id( reg.ids_for(city)[0][0]).get_weather() userin.define([" "], "The temperature in " + city + " is " + str(weather.get_temperature('celsius')['temp']) + " degrees celsius") userin.execute(0) userin.say("The temperature in " + city + " is " + str(weather.get_temperature('celsius')['temp']) + " degrees celsius") elif "WHAT IS YOUR GENDER" == com: tts_kill() userin.define([" "], " ") userin.say( "I have a female voice but I don't have a gender identity. I'm a computer program, " + user_prefix + ".") elif "FILE MANAGER" in com or "OPEN FILES" == com: tts_kill() userin.define(["dolphin"], "File Manager") # KDE neon userin.execute(0) userin.define(["pantheon-files"], "File Manager") # elementary OS userin.execute(0) userin.define(["nautilus", "--browser"], "File Manager") # Ubuntu userin.execute(0) userin.say("File Manager") elif "WEB BROWSER" in com: tts_kill() userin.define(["sensible-browser"], "Web Browser") userin.execute(0) userin.say("Web Browser") elif "OPEN BLENDER" == com: tts_kill() userin.define(["blender"], "Blender") userin.execute(0) userin.say("Blender 3D computer graphics software") elif "PHOTO SHOP" in com or "PHOTO EDITOR" in com or "GIMP" in com: tts_kill() userin.define(["gimp"], "GIMP") userin.execute(0) userin.say("Photo editor") elif "INKSCAPE" in com or "VECTOR GRAPHICS" in com or "VECTORIAL DRAWING" in com: tts_kill() userin.define(["inkscape"], "Inkscape") userin.execute(0) userin.say("Inkscape") elif "VIDEO EDITOR" in com: tts_kill() #userin.define(["openshot"],"Openshot") #userin.execute(0) #userin.define(["lightworks"],"Lightworks") #userin.execute(0) userin.define(["kdenlive"], "Kdenlive") userin.execute(0) userin.say("Video editor") elif "OPEN CAMERA" == com: tts_kill() userin.define(["kamoso"], "Camera") # KDE neon userin.execute(0) userin.define(["snap-photobooth"], "Camera") # elementary OS userin.execute(0) userin.define(["cheese"], "Camera") # Ubuntu userin.execute(0) userin.say("Camera") elif "OPEN CALENDAR" == com: tts_kill() userin.define(["korganizer"], "Calendar") # KDE neon userin.execute(0) userin.define(["maya-calendar"], "Calendar") # elementary OS userin.execute(0) userin.define(["orage"], "Calendar") # Ubuntu userin.execute(0) userin.say("Calendar") elif "OPEN CALCULATOR" == com: tts_kill() userin.define(["kcalc"], "Calculator") # KDE neon userin.execute(0) userin.define(["pantheon-calculator"], "Calculator") # elementary OS userin.execute(0) userin.define(["gnome-calculator"], "Calculator") # Ubuntu userin.execute(0) userin.say("Calculator") elif "OPEN STEAM" == com: tts_kill() userin.define(["steam"], "Steam") userin.execute(0) userin.say("Steam Game Store") elif "SOFTWARE CENTER" in com: tts_kill() userin.define(["plasma-discover"], "Software Center") # KDE neon userin.execute(0) userin.define(["software-center"], "Software Center") # elementary OS & Ubuntu userin.execute(0) userin.say("Software Center") elif "OFFICE SUITE" in com: tts_kill() userin.define(["libreoffice"], "LibreOffice") userin.execute(0) userin.say("Office Suite") elif "OPEN WRITER" == com: tts_kill() userin.define(["libreoffice", "--writer"], "LibreOffice Writer") userin.execute(0) userin.say("Writer") elif "OPEN MATH" == com: tts_kill() userin.define(["libreoffice", "--math"], "LibreOffice Math") userin.execute(0) userin.say("Math") elif "OPEN IMPRESS" == com: tts_kill() userin.define(["libreoffice", "--impress"], "LibreOffice Impress") userin.execute(0) userin.say("Impress") elif "OPEN DRAW" == com: tts_kill() userin.define(["libreoffice", "--draw"], "LibreOffice Draw") userin.execute(0) userin.say("Draw") elif com.startswith("KEYBOARD "): tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() for character in original_com[9:]: k.tap_key(character) k.tap_key(" ") elif com == "ENTER": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.tap_key(k.enter_key) elif com == "NEW TAB": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.control_l_key, 't']) elif com == "SWITCH TAB": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.control_l_key, k.tab_key]) elif com in ("CLOSE", "ESCAPE"): tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.control_l_key, 'w']) k.tap_key(k.escape_key) elif com == "GO BACK": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.alt_l_key, k.left_key]) elif com == "GO FORWARD": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.alt_l_key, k.right_key]) elif com == "SCROLL LEFT": tts_kill() with nostdout(): with nostderr(): m = PyMouse() m.scroll(0, -5) elif com == "SCROLL RIGHT": tts_kill() with nostdout(): with nostderr(): m = PyMouse() m.scroll(0, 5) elif com == "SCROLL UP": tts_kill() with nostdout(): with nostderr(): m = PyMouse() m.scroll(5, 0) elif com == "SCROLL DOWN": tts_kill() with nostdout(): with nostderr(): m = PyMouse() m.scroll(-5, 0) elif com in ("PLAY", "PAUSE", "SPACEBAR"): tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.tap_key(" ") elif "SHUTDOWN THE COMPUTER" == com: tts_kill() userin.define(["sudo", "poweroff"], "Shutting down") userin.say("Shutting down") userin.execute(3) elif com in ("GOODBYE", "BYE BYE", "SEE YOU LATER"): tts_kill() userin.define([" "], " ") userin.say("Goodbye, " + user_prefix) #raise KeyboardInterrupt thread.interrupt_main() elif "WIKIPEDIA" in com and ("SEARCH" in com or "FIND" in com): tts_kill() with nostderr(): capture = re.search( "(?:SEARCH|FIND) (?P<query>.*) (?:IN|ON|AT|USING)? WIKIPEDIA", com) if capture: search_query = capture.group('query') try: wikiresult = wikipedia.search(search_query) if len(wikiresult) == 0: userin.say( "Sorry, " + user_prefix + ". But I couldn't find anything about " + search_query + " in Wikipedia.") return True wikipage = wikipedia.page(wikiresult[0]) wikicontent = "".join([ i if ord(i) < 128 else ' ' for i in wikipage.content ]) wikicontent = re.sub(r'\([^)]*\)', '', wikicontent) userin.define(["sensible-browser", wikipage.url], search_query) userin.execute(0) userin.say(wikicontent) except requests.exceptions.ConnectionError: userin.define([" "], "Wikipedia connection error.") userin.execute(0) userin.say( "Sorry, " + user_prefix + ". But I'm unable to connect to Wikipedia servers." ) except wikipedia.exceptions.DisambiguationError as disambiguation: USER_ANSWERING['status'] = True USER_ANSWERING['for'] = 'wikipedia' USER_ANSWERING['reason'] = 'disambiguation' USER_ANSWERING['options'] = disambiguation.options[:3] notify = "Wikipedia disambiguation. Which one of these you meant?:\n - " + disambiguation.options[ 0] message = user_prefix + ", there is a disambiguation. Which one of these you meant? " + disambiguation.options[ 0] for option in disambiguation.options[1:3]: message += ", or " + option notify += "\n - " + option notify += '\nSay, for example: "THE FIRST ONE" to choose.' userin.define([" "], notify) userin.execute(0) userin.say(message) except: pass elif "YOUTUBE" in com and ("SEARCH" in com or "FIND" in com): tts_kill() with nostdout(): with nostderr(): capture = re.search( "(?:SEARCH|FIND) (?P<query>.*) (?:IN|ON|AT|USING)? YOUTUBE", com) if capture: search_query = capture.group('query') info = youtube_dl.YoutubeDL({}).extract_info( 'ytsearch:' + search_query, download=False, ie_key='YoutubeSearch') if len(info['entries']) > 0: youtube_title = info['entries'][0]['title'] youtube_url = "https://www.youtube.com/watch?v=%s" % ( info['entries'][0]['id']) userin.define(["sensible-browser", youtube_url], youtube_title) youtube_title = "".join([ i if ord(i) < 128 else ' ' for i in youtube_title ]) else: youtube_title = "No video found, " + user_prefix + "." userin.define(" ", " ") userin.execute(0) userin.say(youtube_title) time.sleep(5) k = PyKeyboard() k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key('f') elif ("GOOGLE" in com or "WEB" in com) and "IMAGE" not in com and ("SEARCH" in com or "FIND" in com): tts_kill() with nostdout(): with nostderr(): capture = re.search( "(?:SEARCH|FIND) (?P<query>.*) (?:IN|ON|AT|USING)? (?:GOOGLE|WEB)?", com) if capture: search_query = capture.group('query') tab_url = "http://google.com/?#q=" + search_query userin.define(["sensible-browser", tab_url], search_query) userin.execute(0) userin.say(search_query) elif ("GOOGLE" in com or "WEB" in com) and "IMAGE" in com and ("SEARCH" in com or "FIND" in com): tts_kill() with nostdout(): with nostderr(): capture = re.search( "(?:SEARCH IMAGES OF|FIND IMAGES OF|SEARCH|FIND) (?P<query>.*) (?:IN|ON|AT|USING)? (?:GOOGLE|WEB|GOOGLE IMAGES|WEB IMAGES)?", com) if capture: search_query = capture.group('query') tab_url = "http://google.com/?#q=" + search_query + "&tbm=isch" userin.define(["sensible-browser", tab_url], search_query) userin.execute(0) userin.say(search_query) else: tts_kill() learn_response = learn_.respond(com) if learn_response: userin.define([" "], " ") userin.say(learn_response) else: omniscient_.respond(original_com, user_prefix, not args["silent"], userin)
k.tap_key('l',n=2,interval=5) # and you can send a string if needed too k.type_string('o World!') 并且它还支持很多特殊按键: #Create an Alt+Tab combo k.press_key(k.alt_key) k.tap_key(k.tab_key) k.release_key(k.alt_key) k.tap_key(k.function_keys[5]) # Tap F5 k.tap_key(k.numpad_keys['Home']) # Tap 'Home' on the numpad k.tap_key(k.numpad_keys[5], n=3) # Tap 5 on the numpad, thrice 注意,你也可以使用press_keys方法将多个键一起发送(例如,使用某些组合键): # Mac example k.press_keys(['Command','shift','3']) # Windows example k.press_keys([k.windows_l_key,'d']) 平台之间的一致性是一个很大的挑战,请参考你使用的操作系统对应的源码,来理解你需要使用的按键格式。例如: # Windows k.tap_key(k.alt_key) # Mac k.tap_key('Alternate') 我还想特别说明一下PyMouseEvent和PyKeyboardEvent的使用。 这些对象是一个架构用于监听鼠标和键盘的输入;他们除了监听之外不会做任何事,除非你继承他们【注1】。PyKeyboardEvent为编写完成,所以这里是一个继承PyMouseEvent的例子: from pymouse import PyMouseEvent def fibo():
def combo(*arg): k = PyKeyboard() k.press_keys(arg)
def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() image = get_file_content('photo.jpg') """ 调用手势识别 """ answer = client.gesture(image) try: result = answer['result'][0]['classname'] except: result = 'xxx' hand = sign_dic[result] print('\r'+hand+' ',end='') if hand == '5': kb.press_keys([kb.menu_key,'q']) kb.release_key('q') kb.release_key(kb.space_key) elif hand == '2': kb.press_keys([kb.menu_key,kb.right_key]) kb.release_key(kb.right_key) kb.release_key(kb.menu_key) elif hand == 'OK': kb.press_keys([kb.menu_key,kb.left_key]) kb.release_key(kb.left_key) kb.release_key(kb.menu_key) elif hand == '赞': kb.press_keys([kb.menu_key,kb.up_key]) kb.release_key(kb.up_key) kb.release_key(kb.menu_key)