import speech_recognition as sr from pynput.keyboard import Key, Controller import webbrowser from urllib.parse import urlencode for index, name in enumerate(sr.Microphone.list_microphone_names()): print("Microphone with name \"{1}\" found for `Microphone(device_index={0})`".format(index, name)) MICROPHONE_INDEX = 0 # MICROPHONE_INDEX = 1 import pyttsx3 speak = pyttsx3.init() voices = speak.getProperty('voices') speak.setProperty('voice', voices[0].id) def say(text): print(text) speak.say(text) speak.runAndWait() r = sr.Recognizer() r.energy_threshold = 500 h_id = "JQHKdCaW0xqZz24PRlw3lg==" h_key = "ZIdFJmzY6DKiwhsHohxGJpd0mcCMHHetH1dJxOxyPybVfv4GOT1zH9rJ-c1LoRLE--olxlR36uKD483pkSjjqQ==" search_prompt = "search" lucky_prompt = "lucky" music_prompt = "play" keyboard = Controller() def swipe_right(): keyboard.press(Key.cmd) keyboard.press(Key.ctrl) keyboard.press(Key.right) keyboard.release(Key.right) keyboard.release(Key.ctrl) keyboard.release(Key.cmd) def swipe_left(): keyboard.press(Key.cmd) keyboard.press(Key.ctrl) keyboard.press(Key.left) keyboard.release(Key.left) keyboard.release(Key.ctrl) keyboard.release(Key.cmd) def rickroll(): webbrowser.open('https://www.youtube.com/watch?v=oHg5SJYRHA0', new=2) def google(query, lucky=False): params = {'q':query} if lucky: params['btnI'] = 1 url = 'https://www.google.com/search?{}'.format(urlencode(params)) print(url) webbrowser.open(url, new=2) def music(query): params = {'q':query} # url = 'https://music.youtube.com/search?{}'.format(urlencode(params)) url = 'https://www.youtube.com/results?{}'.format(urlencode(params)) print(url) webbrowser.open(url, new=2) def listen(): text = None while not text: try: print('Speak anything') audio = r.listen(source) # text = r.recognize_sphinx(audio) text = r.recognize_google(audio) # text = r.recognize_houndify(audio, h_id, h_key) print('you said :', text) except sr.UnknownValueError: pass return text def listen_and_obey(): text = listen() if text == "right": swipe_right() elif text == "left": swipe_left() elif text == "Rick Roll" or text == "rickroll": rickroll() elif text.startswith(search_prompt): q = text.split(' ', 1)[1] google(q) elif text.startswith(lucky_prompt): q = text.split(' ', 1)[1] google(q, lucky=True) elif text.startswith(music_prompt): q = text.split(' ', 1)[1] music(q) with sr.Microphone(device_index=MICROPHONE_INDEX) as source: say('Hello there, what is your name?') name = listen() say('Hello {}, nice to meet you.'.format(name)) while True: listen_and_obey()