May-06-2020, 02:04 AM
Does anyone know how to get google to say back what the search result was? code provided:
import pyttsx3 #pip install pyttsx3
import speech_recognition as sr #pip install speechRecognition
import datetime
import wikipedia #pip install wikipedia
import webbrowser
import os
import smtplib
from googlesearch import *
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
# print(voices[1].id)
engine.setProperty('voice', voices[0].id)
def speak(audio):
engine.say(audio)
engine.runAndWait()
def wishMe():
hour = int(datetime.datetime.now().hour)
if hour>=0 and hour<12:
print("Good Morning.")
speak("Good Morning!")
elif hour>=12 and hour<18:
print("Good Afternoon.")
speak("Good Afternoon!")
else:
print("Good Evening.")
speak("Good Evening!")
print("Initializing SAM...")
speak("Initializing SAM...")
print("Waiting for your command...")
speak("Waiting for your command...")
def takeCommand():
#It takes microphone input from the user and returns string output
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language='en-in')
print(f"User said: {query}\n")
except Exception as e:
# print(e)
print("Say that again please...")
return "None"
return query
def sendEmail(to, content):
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login('', '')
server.sendmail('', to, content)
server.close()
if __name__ == "__main__":
wishMe()
while True:
# if 1:
query = takeCommand().lower()
# Logic for executing tasks based on query
if 'who' in query:
speak('Searching Google...')
query = query.replace("google", "")
chrome_path = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe %s'
for url in search(query, tld="co.in", num=1, stop = 1, pause = 2):
webbrowser.open("https://google.com/search?q=%s" % query)
print("This is what I found according to your search")
speak("This is what I found according to your search")
elif 'what' in query:
speak('Searching Google...')
query = query.replace("google", "")
chrome_path = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe %s'
for url in search(query, tld="co.in", num=1, stop = 1, pause = 2):
webbrowser.open("https://google.com/search?q=%s" % query)
print("This is what I found according to your search")
speak("This is what I found according to your search")
elif 'when' in query:
speak('Searching Google...')
query = query.replace("google", "")
chrome_path = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe %s'
for url in search(query, tld="co.in", num=1, stop = 1, pause = 2):
webbrowser.open("https://google.com/search?q=%s" % query)
print("This is what I found according to your search")
speak("This is what I found according to your search")
elif 'where' in query:
speak('Searching Google...')
query = query.replace("google", "")
chrome_path = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe %s'
for url in search(query, tld="co.in", num=1, stop = 1, pause = 2):
webbrowser.open("https://google.com/search?q=%s" % query)
print("This is what I found according to your search")
speak("This is what I found according to your search")
elif 'why' in query:
speak('Searching Google...')
query = query.replace("google", "")
chrome_path = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe %s'
for url in search(query, tld="co.in", num=1, stop = 1, pause = 2):
webbrowser.open("https://google.com/search?q=%s" % query)
print("This is what I found according to your search")
speak("This is what I found according to your search")
elif 'open youtube' in query:
webbrowser.open("https://www.youtube.com/")
elif 'open google' in query:
webbrowser.open("google.com")
elif 'goodbye' in query:
speak('Goodbye Justin')
quit()
elif 'play music' in query:
webbrowser.open("")
elif 'what\'s the time' in query:
strTime = datetime.datetime.now().strftime("%H:%M:%S")
speak(f"Sir, the time is {strTime}")
elif 'open code' in query:
codePath = "C:\\Users\\Owner\\Desktop\\SAM"
os.startfile(codePath)
elif 'email danny' in query:
try:
speak("What should I say?")
content = takeCommand()
to = ""
sendEmail(to, content)
speak("Email sent.")
except Exception as e:
print(e)
speak("Sorry Justin. I was not able to send this email")
