import speech_recognition as sr import subprocess import time import os import pyowm import urllib.request import yagmail import webbrowser import wikipedia import random import json import pyaudio import wave from gtts import gTTS from os import path from pprint import pprint def recordSound(name,time_record): CHUNK = 2**14 FORMAT = pyaudio.paInt16 SHORT_NORMALIZE = (1.0/(2**15)) CHANNELS = 1 RATE = 44100 RECORD_SECONDS = time_record WAVE_OUTPUT_FILENAME = name+".wav" p = pyaudio.PyAudio() stream = p.open(format=FORMAT,channels=CHANNELS,rate=RATE,input=True,frames_per_buffer=CHUNK) os.system('aplay sound.wav') print("Recording") frames = [] for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): data = stream.read(CHUNK) frames.append(data) print("Done recording") stream.stop_stream() stream.close() p.terminate() wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb') wf.setnchannels(CHANNELS) wf.setsampwidth(p.get_sample_size(FORMAT)) wf.setframerate(RATE) wf.writeframes(b''.join(frames)) wf.close() time.sleep(3) recordSound('output',5) loctime = time.localtime() email = '' #<-------- GMAIL ADRESS MUST BE GMAIL pwd = '' #<-------- Password yag = yagmail.SMTP(email, pwd) contacts = {"YOUR CONTACT'S NAME GOES IN THESE QUOTES":"THE CONTACT'S EMAIL GOES IN THESE QUOTES","YOUR SECOND CONTACT'S NAME GOES IN THESE QUOTES":"THE 2ND CONTACT'S EMAIL GOES IN THESE QUOTES"} #<----------- CHANGE THIS DICTIONARY TO YOUR EMAIL CONTACTS, YOU CAN ADD AS MANY CONTACTS AS YOU WANT USING THIS DICT AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), "output.wav") r = sr.Recognizer() with sr.AudioFile(AUDIO_FILE) as source: audio = r.record(source) var = r.recognize_google(audio).lower() def voiceMessage(what_to_say,name_of_file): tts = gTTS(text = what_to_say,lang = 'en',slow = False) name_of_file += '.mp3' tts.save(name_of_file) os.system('mpg321 '+name_of_file) if var == 'shuffle' or var == 'truffle': pandora_urls = ['a pandora station url', 'another pandora station url', 'and another'] #<-------- CHANGE TO YOUR STATIONS choice = random.choice(pandora_urls) webbrowser.open(choice) time.sleep(600) pyautogui.hotkey('ctrl','shift','q') elif var.count('email') == 1: recipient_contact = var.split()[1] recipient_email=contacts[recipient_contact] if recipient_contact in contacts: ist = var.split() del ist[0] del ist[1] message = '' for i in ist: message = message + ' ' + i message = message.replace(recipient_contact,'') newmsg = message.title() yag.send(recipient_email, 'No Subject', newmsg) #Email people elif var == 'weather' or var == 'brother': owm = pyowm.OWM('YOUR OPEN WEATHER MAP API KEY') #Sign up for open weather map observation = owm.weather_at_place("Redding,us")# <----------CHANGE THIS TO YOUR CITY!!! w = observation.get_weather() temperature = w.get_temperature('fahrenheit') voiceMessage('Lowest temperature is {} degrees Fahrenheit. Current temperature is {} degrees Fahrenheit. Highest temperature is {} degrees Fahrenheit'.format(round(temperature['temp_min']),round(temperature['temp']),round(temperature['temp_max'])),'weather') #Weather elif var.count('wikipedia') >=1: var = var.replace('wikipedia','') tts = gTTS(wikipedia.summary(var),'wiki') #Wikipedia stuff elif var == 'quote': try: os.remove('qod.json') except OSError: pass os.system('wget http://quotes.rest/qod.json') with open('qod.json') as opened_data: json_data = json.load(opened_data) talk = json_data['contents']['quotes'][0]['quote'] talk = talk.replace('"','') voiceMessage(talk,'qotd') #Quote of the day from some website that had a good API elif var == 'where to dump dead bodies': voiceMessage('Do not dump dead bodies, instead, carve them up with a cerrated knife into 5 centimeter by 5 centimeter cubes and send them to the government for soylent green. Thank you for helping the future!','dead') elif var == 'time': voiceMessage('The time is {} hours and {} minutes'.format(loctime[3],loctime[4]),'time') #Play local time elif var == 'are you recording this': voiceMessage('I am always listening, and I am always reporting to the National Security Administration on suspicious activities.','NSA') elif var == 'tell me a joke' or var == 'joke': jokes = ["""A man flying in a hot air balloon suddenly realizes he’s lost. He reduces height and spots a man down below. He lowers the balloon further and shouts to get directions, "Excuse me, can you tell me where I am?" The man below says: "Yes. You're in a hot air balloon, hovering 30 feet above this field." "You must work in Information Technology," says the balloonist. "I do" replies the man. "How did you know?" "Well," says the balloonist, "everything you have told me is technically correct, but It's of no use to anyone." The man below replies, "You must work in management." "I do," replies the balloonist, *"But how'd you know?"** "Well", says the man, "you don’t know where you are or where you’re going, but you expect me to be able to help. You’re in the same position you were before we met, but now it’s my fault.""",'When your hammer is C++, everything begins to look like a thumb.',"What's the object oriented way of becoming wealthy? Inheritance","What's a computer scientists favorite place to hangout? Foo bar","I’ll tell you a DNS joke but be advised, it could take up to 24 hours for everyone to get it.","So I want to dress up as a UDP packet for Halloween, but I don’t know if anyone will get it.","I could tell you an ICMP joke but it would probably be repetitive.","Linux geek started working at McDonalds. A customer asked him for a Big Mac and he gave him a bit of paper with FF:FF:FF:FF:FF:FF written on it."] #You can change these jokes if you wish voiceMessage(random.choice(jokes),'joke') #Random programming/computer science joke elif var == 'toby' or var == 'tobi': os.system('mpg321 Toby.mp3') #Play Heyeayeayea elif var.count('record me as') == 1: print(var) recordSound(var.split()[3],int(var.split()[4])) #e.g. record me as foobarbaz 20 this will record you as foobarbaz.wav for 20 seconds else: voiceMessage('Your message {} could not be understood.'.format(var),'error')