2021-10-03 15:22:09 +05:30
|
|
|
import datetime
|
2021-10-06 08:14:43 +02:00
|
|
|
import random
|
2021-10-03 15:22:09 +05:30
|
|
|
import smtplib
|
|
|
|
|
import sys
|
2021-10-06 08:14:43 +02:00
|
|
|
import webbrowser
|
|
|
|
|
|
|
|
|
|
import pyttsx3
|
|
|
|
|
import speech_recognition as sr
|
|
|
|
|
import wikipedia
|
2021-10-03 15:22:09 +05:30
|
|
|
from pygame import mixer
|
2021-10-06 08:14:43 +02:00
|
|
|
|
2021-10-03 15:22:09 +05:30
|
|
|
mixer.init()
|
|
|
|
|
|
|
|
|
|
print("Initializing Jarvis....")
|
|
|
|
|
MASTER = "Tony Stark"
|
|
|
|
|
|
2021-10-06 22:50:53 +05:30
|
|
|
|
2021-10-03 15:22:09 +05:30
|
|
|
engine = pyttsx3.init('sapi5') #
|
|
|
|
|
voices = engine.getProperty('voices')
|
2021-10-05 01:13:33 +05:30
|
|
|
engine.setProperty('voice', voices[0].id) #for male voices
|
|
|
|
|
# engine.setProperty('voice', voices[1].id) #for female voice
|
|
|
|
|
|
|
|
|
|
""" RATE"""
|
|
|
|
|
rate = engine.getProperty('rate') # getting details of current speaking rate
|
|
|
|
|
print ("current voice rate is: ",rate) #printing current voice rate
|
|
|
|
|
engine.setProperty('rate',170) # setting up new voice rate
|
|
|
|
|
engine.runAndWait()
|
|
|
|
|
|
|
|
|
|
"""VOLUME"""
|
|
|
|
|
volume = engine.getProperty('volume') #getting to know current volume level (min=0 and max=1)
|
|
|
|
|
print ("volume level is at : ",volume) #printing current volume level
|
|
|
|
|
engine.setProperty('volume',1.0) # setting up volume level between 0 and 1
|
|
|
|
|
|
2021-10-04 07:56:18 +05:30
|
|
|
popular_websites = {'google': 'https://www.google.com',
|
|
|
|
|
'youtube': 'https://www.youtube.com',
|
|
|
|
|
'wikipedia': 'https://www.wikipedia.org',
|
|
|
|
|
'amazon': 'https://www.amazon.com'}
|
|
|
|
|
search_engines = {'google': 'https://www.google.com',
|
|
|
|
|
'youtube': 'https://www.youtube.com',
|
|
|
|
|
'bing': 'https://www.bing.com'}
|
|
|
|
|
|
2021-10-06 08:14:43 +02:00
|
|
|
engine = pyttsx3.init("sapi5")
|
|
|
|
|
voices = engine.getProperty("voices")
|
|
|
|
|
engine.setProperty("voice", voices[0].id)
|
|
|
|
|
popular_websites = {
|
|
|
|
|
"google": "https://www.google.com",
|
|
|
|
|
"youtube": "https://www.youtube.com",
|
|
|
|
|
"wikipedia": "https://www.wikipedia.org",
|
|
|
|
|
"amazon": "https://www.amazon.com",
|
2021-10-06 18:50:55 -04:00
|
|
|
"GitHub": "https://www.github.com",
|
2021-10-06 08:14:43 +02:00
|
|
|
}
|
|
|
|
|
search_engines = {
|
|
|
|
|
"google": "https://www.google.com",
|
|
|
|
|
"youtube": "https://www.youtube.com",
|
|
|
|
|
"bing": "https://www.bing.com",
|
|
|
|
|
}
|
2021-10-04 07:56:18 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def open_url(url):
|
|
|
|
|
webbrowser.open(url)
|
2021-10-06 08:14:43 +02:00
|
|
|
chrome_path = r"open -a /Applications/Google\ Chrome.app %s"
|
2021-10-04 07:56:18 +05:30
|
|
|
webbrowser.get(chrome_path).open(url)
|
|
|
|
|
|
2021-10-06 08:14:43 +02:00
|
|
|
|
2021-10-04 07:56:18 +05:30
|
|
|
def search(search_query, search_engine):
|
|
|
|
|
try:
|
2021-10-06 08:14:43 +02:00
|
|
|
open_url(f"{search_engines[search_engine]}/search?q={search_query}")
|
2021-10-04 07:56:18 +05:30
|
|
|
except IndexError:
|
2021-10-06 08:14:43 +02:00
|
|
|
open_url(f"https://www.google.com/search?q={search_query}")
|
2021-10-03 15:22:09 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
def speak(text):
|
|
|
|
|
engine.say(text)
|
|
|
|
|
engine.runAndWait()
|
|
|
|
|
|
|
|
|
|
|
2021-10-07 23:20:15 +02:00
|
|
|
def wish_me():
|
2021-10-03 15:22:09 +05:30
|
|
|
hour = datetime.datetime.now().hour
|
|
|
|
|
# print(hour)
|
|
|
|
|
if hour >= 0 and hour < 12:
|
|
|
|
|
speak("Good Morning" + MASTER)
|
|
|
|
|
|
|
|
|
|
elif hour >= 12 and hour < 18:
|
|
|
|
|
speak("Good Afternoon" + MASTER)
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
speak("Good Evening" + MASTER)
|
|
|
|
|
|
|
|
|
|
# speak("Hey I am Jarvis. How may I help you")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# This is where our programme begins....
|
2021-10-07 23:20:15 +02:00
|
|
|
def take_command():
|
2021-10-03 15:22:09 +05:30
|
|
|
r = sr.Recognizer()
|
|
|
|
|
with sr.Microphone() as source:
|
|
|
|
|
print("Listening....")
|
|
|
|
|
r.pause_threshold = 0.5
|
|
|
|
|
audio = r.listen(source)
|
|
|
|
|
|
2021-10-07 23:20:15 +02:00
|
|
|
query = ""
|
2021-10-03 15:22:09 +05:30
|
|
|
try:
|
|
|
|
|
print("Recognizing....")
|
2021-10-06 08:14:43 +02:00
|
|
|
query = r.recognize_google(audio, language="en-in")
|
2021-10-03 15:22:09 +05:30
|
|
|
print("user said: " + query)
|
|
|
|
|
|
|
|
|
|
except sr.UnknownValueError:
|
|
|
|
|
print("Sorry Could You please try again")
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(e)
|
|
|
|
|
print("Say That Again Please")
|
|
|
|
|
|
|
|
|
|
return query
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
speak("Initializing Jarvis....")
|
2021-10-07 23:20:15 +02:00
|
|
|
wish_me()
|
2021-10-03 15:22:09 +05:30
|
|
|
while True:
|
2021-10-07 23:20:15 +02:00
|
|
|
query = take_command()
|
2021-10-03 15:22:09 +05:30
|
|
|
|
|
|
|
|
# logic for executing basic tasks
|
2021-10-06 08:14:43 +02:00
|
|
|
if "wikipedia" in query.lower():
|
|
|
|
|
speak("Searching wikipedia....")
|
2021-10-03 15:22:09 +05:30
|
|
|
query = query.replace("wikipedia", "")
|
|
|
|
|
results = wikipedia.summary(query, sentences=2)
|
|
|
|
|
print(results)
|
|
|
|
|
speak(results)
|
|
|
|
|
|
2021-10-06 08:14:43 +02:00
|
|
|
elif "what's up" in query or "how are you" in query:
|
2021-10-07 23:20:15 +02:00
|
|
|
st_msgs = [
|
2021-10-06 08:14:43 +02:00
|
|
|
"Just doing my thing!",
|
|
|
|
|
"I am fine!",
|
|
|
|
|
"Nice!",
|
|
|
|
|
"I am nice and full of energy",
|
|
|
|
|
]
|
2021-10-07 23:20:15 +02:00
|
|
|
speak(random.choice(st_msgs))
|
2021-10-03 15:22:09 +05:30
|
|
|
|
2021-10-06 08:14:43 +02:00
|
|
|
elif "open" in query.lower():
|
|
|
|
|
website = query.replace("open", "").strip().lower()
|
2021-10-04 07:56:18 +05:30
|
|
|
try:
|
|
|
|
|
open_url(popular_websites[website])
|
2021-10-06 08:14:43 +02:00
|
|
|
except IndexError: # If the website is unknown
|
|
|
|
|
print(f"Unknown website: {website}")
|
|
|
|
|
speak(f"Sorry, i don't know the website {website}")
|
2021-10-04 07:56:18 +05:30
|
|
|
|
2021-10-06 08:14:43 +02:00
|
|
|
elif "search" in query.lower():
|
|
|
|
|
search_query = query.split("for")[-1]
|
|
|
|
|
search_engine = query.split("for")[0].replace("search", "").strip().lower()
|
2021-10-04 07:56:18 +05:30
|
|
|
search(search_query, search_engine)
|
2021-10-03 15:22:09 +05:30
|
|
|
|
2021-10-06 08:14:43 +02:00
|
|
|
elif "mail" in query:
|
|
|
|
|
speak("Who is the recipient? ")
|
2021-10-07 23:20:15 +02:00
|
|
|
recipient = take_command()
|
2021-10-03 15:22:09 +05:30
|
|
|
|
2021-10-06 08:14:43 +02:00
|
|
|
if "me" in recipient:
|
2021-10-03 15:22:09 +05:30
|
|
|
try:
|
2021-10-06 08:14:43 +02:00
|
|
|
speak("What should I say? ")
|
2021-10-07 23:20:15 +02:00
|
|
|
content = take_command()
|
2021-10-03 15:22:09 +05:30
|
|
|
|
2021-10-06 08:14:43 +02:00
|
|
|
server = smtplib.SMTP("smtp.gmail.com", 587)
|
2021-10-03 15:22:09 +05:30
|
|
|
server.ehlo()
|
|
|
|
|
server.starttls()
|
2021-10-06 08:14:43 +02:00
|
|
|
server.login("Your_Username", "Your_Password")
|
|
|
|
|
server.sendmail("Your_Username", "Recipient_Username", content)
|
2021-10-03 15:22:09 +05:30
|
|
|
server.close()
|
2021-10-06 08:14:43 +02:00
|
|
|
speak("Email sent!")
|
|
|
|
|
except Exception:
|
|
|
|
|
speak("Sorry Sir! I am unable to send your message at this moment!")
|
2021-10-03 15:22:09 +05:30
|
|
|
|
2021-10-06 08:14:43 +02:00
|
|
|
elif "nothing" in query or "abort" in query or "stop" in query:
|
|
|
|
|
speak("okay")
|
|
|
|
|
speak("Bye Sir, have a good day.")
|
2021-10-03 15:22:09 +05:30
|
|
|
sys.exit()
|
|
|
|
|
|
2021-10-06 08:14:43 +02:00
|
|
|
elif "hello" in query:
|
|
|
|
|
speak("Hello Sir")
|
2021-10-03 15:22:09 +05:30
|
|
|
|
2021-10-06 08:14:43 +02:00
|
|
|
elif "bye" in query:
|
|
|
|
|
speak("Bye Sir, have a good day.")
|
2021-10-03 15:22:09 +05:30
|
|
|
sys.exit()
|
|
|
|
|
|
2021-10-06 08:14:43 +02:00
|
|
|
elif "play music" in query:
|
2021-10-03 15:22:09 +05:30
|
|
|
music_folder = "Your_music_folder_path(absolute_path)"
|
2021-10-06 08:14:43 +02:00
|
|
|
music = ("music1", "music2", "music3", "music4")
|
|
|
|
|
random_music = music_folder + random.choice(music) + ".mp3"
|
|
|
|
|
speak("Playing your request")
|
2021-10-03 15:22:09 +05:30
|
|
|
mixer.music.load(random_music)
|
|
|
|
|
mixer.music.play()
|
|
|
|
|
|
2021-10-06 08:14:43 +02:00
|
|
|
elif "pause music" in query:
|
2021-10-03 15:22:09 +05:30
|
|
|
mixer.music.pause()
|
|
|
|
|
|
2021-10-06 08:14:43 +02:00
|
|
|
elif "stop music" in query:
|
2021-10-03 15:22:09 +05:30
|
|
|
mixer.music.stop()
|
|
|
|
|
|
2021-10-06 08:14:43 +02:00
|
|
|
elif "unpause" in query:
|
2021-10-03 15:22:09 +05:30
|
|
|
mixer.music.unpause()
|
|
|
|
|
|
2021-10-06 08:14:43 +02:00
|
|
|
speak("Next Command! Sir!")
|