windows version upgraded.
Organized code, added config file and config reading, fixed 2 bugs and added a debug mode in wich you can type commands in the console instead of saying them.
This commit is contained in:
parent
011c1b1448
commit
ef9defa759
@ -9,166 +9,202 @@ import speech_recognition as sr
|
|||||||
import wikipedia
|
import wikipedia
|
||||||
from pygame import mixer
|
from pygame import mixer
|
||||||
|
|
||||||
mixer.init()
|
import configparser
|
||||||
|
import os
|
||||||
|
|
||||||
print("Initializing Jarvis....")
|
mixer.init()
|
||||||
MASTER = "Tony Stark"
|
|
||||||
|
|
||||||
engine = pyttsx3.init("sapi5")
|
engine = pyttsx3.init("sapi5")
|
||||||
voices = engine.getProperty("voices")
|
voices = engine.getProperty("voices")
|
||||||
engine.setProperty("voice", voices[0].id)
|
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",
|
|
||||||
}
|
|
||||||
search_engines = {
|
|
||||||
"google": "https://www.google.com",
|
|
||||||
"youtube": "https://www.youtube.com",
|
|
||||||
"bing": "https://www.bing.com",
|
|
||||||
}
|
|
||||||
|
|
||||||
|
def search_engine_selector():
|
||||||
|
|
||||||
|
if config['DEFAULT']['search_engine'] == 'Google':
|
||||||
|
return "https://www.google.com"
|
||||||
|
elif config['DEFAULT']['search_engine'] == 'Bing':
|
||||||
|
return "https://www.bing.com"
|
||||||
|
elif config['DEFAULT']['search_engine'] == 'DuckDuckGo':
|
||||||
|
return "https://www.duckduckgo.com"
|
||||||
|
elif config['DEFAULT']['search_engine'] == 'Youtube':
|
||||||
|
return "https://www.youtube.com"
|
||||||
|
else:
|
||||||
|
return f"https://www.{config['DEFAULT']['search_engine'].lower()}.com"
|
||||||
|
|
||||||
def open_url(url):
|
def open_url(url):
|
||||||
webbrowser.open(url)
|
webbrowser.open(url)
|
||||||
chrome_path = r"open -a /Applications/Google\ Chrome.app %s"
|
chrome_path = r"open -a /Applications/Google\ Chrome.app %s"
|
||||||
webbrowser.get(chrome_path).open(url)
|
webbrowser.get(chrome_path).open(url)
|
||||||
|
|
||||||
|
|
||||||
def search(search_query, search_engine):
|
def search(search_query, search_engine):
|
||||||
try:
|
open_url(f"{search_engine}/search?q={search_query}")
|
||||||
open_url(f"{search_engines[search_engine]}/search?q={search_query}")
|
|
||||||
except IndexError:
|
|
||||||
open_url(f"https://www.google.com/search?q={search_query}")
|
|
||||||
|
|
||||||
|
|
||||||
def speak(text):
|
def speak(text):
|
||||||
engine.say(text)
|
engine.say(text)
|
||||||
engine.runAndWait()
|
engine.runAndWait()
|
||||||
|
|
||||||
|
def wishMe(MASTER):
|
||||||
|
hour = datetime.datetime.now().hour
|
||||||
|
# print(hour)
|
||||||
|
if hour >= 0 and hour < 12:
|
||||||
|
speak("Good Morning" + MASTER)
|
||||||
|
|
||||||
def wishMe():
|
elif hour >= 12 and hour < 18:
|
||||||
hour = datetime.datetime.now().hour
|
speak("Good Afternoon" + MASTER)
|
||||||
# print(hour)
|
|
||||||
if hour >= 0 and hour < 12:
|
|
||||||
speak("Good Morning" + MASTER)
|
|
||||||
|
|
||||||
elif hour >= 12 and hour < 18:
|
else:
|
||||||
speak("Good Afternoon" + MASTER)
|
speak("Good Evening" + MASTER)
|
||||||
|
|
||||||
|
# speak("Hey I am Jarvis. How may I help you")
|
||||||
|
|
||||||
|
def main():
|
||||||
|
MASTER = config['DEFAULT']['MASTER']
|
||||||
|
|
||||||
|
popular_websites = {
|
||||||
|
"google": "https://www.google.com",
|
||||||
|
"youtube": "https://www.youtube.com",
|
||||||
|
"wikipedia": "https://www.wikipedia.org",
|
||||||
|
"amazon": "https://www.amazon.com",
|
||||||
|
}
|
||||||
|
|
||||||
|
search_engine = search_engine_selector()
|
||||||
|
|
||||||
|
debug = config['DEFAULT']['debug']
|
||||||
|
|
||||||
|
if debug == "True":
|
||||||
|
def takeCommand():
|
||||||
|
query = input("Command |--> ")
|
||||||
|
return query
|
||||||
else:
|
else:
|
||||||
speak("Good Evening" + MASTER)
|
def takeCommand():
|
||||||
|
r = sr.Recognizer()
|
||||||
|
with sr.Microphone() as source:
|
||||||
|
if debug == "True":print("Listening....")
|
||||||
|
else:pass
|
||||||
|
r.pause_threshold = 0.5
|
||||||
|
audio = r.listen(source)
|
||||||
|
|
||||||
# speak("Hey I am Jarvis. How may I help you")
|
query = " "
|
||||||
|
|
||||||
|
|
||||||
# This is where our programme begins....
|
|
||||||
def takeCommand():
|
|
||||||
r = sr.Recognizer()
|
|
||||||
with sr.Microphone() as source:
|
|
||||||
print("Listening....")
|
|
||||||
r.pause_threshold = 0.5
|
|
||||||
audio = r.listen(source)
|
|
||||||
|
|
||||||
query = " "
|
|
||||||
try:
|
|
||||||
print("Recognizing....")
|
|
||||||
query = r.recognize_google(audio, language="en-in")
|
|
||||||
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")
|
|
||||||
query = None
|
|
||||||
|
|
||||||
return query
|
|
||||||
|
|
||||||
|
|
||||||
speak("Initializing Jarvis....")
|
|
||||||
wishMe()
|
|
||||||
while True:
|
|
||||||
query = takeCommand()
|
|
||||||
|
|
||||||
# logic for executing basic tasks
|
|
||||||
if "wikipedia" in query.lower():
|
|
||||||
speak("Searching wikipedia....")
|
|
||||||
query = query.replace("wikipedia", "")
|
|
||||||
results = wikipedia.summary(query, sentences=2)
|
|
||||||
print(results)
|
|
||||||
speak(results)
|
|
||||||
|
|
||||||
elif "what's up" in query or "how are you" in query:
|
|
||||||
stMsgs = [
|
|
||||||
"Just doing my thing!",
|
|
||||||
"I am fine!",
|
|
||||||
"Nice!",
|
|
||||||
"I am nice and full of energy",
|
|
||||||
]
|
|
||||||
speak(random.choice(stMsgs))
|
|
||||||
|
|
||||||
elif "open" in query.lower():
|
|
||||||
website = query.replace("open", "").strip().lower()
|
|
||||||
try:
|
|
||||||
open_url(popular_websites[website])
|
|
||||||
except IndexError: # If the website is unknown
|
|
||||||
print(f"Unknown website: {website}")
|
|
||||||
speak(f"Sorry, i don't know the website {website}")
|
|
||||||
|
|
||||||
elif "search" in query.lower():
|
|
||||||
search_query = query.split("for")[-1]
|
|
||||||
search_engine = query.split("for")[0].replace("search", "").strip().lower()
|
|
||||||
search(search_query, search_engine)
|
|
||||||
|
|
||||||
elif "mail" in query:
|
|
||||||
speak("Who is the recipient? ")
|
|
||||||
recipient = takeCommand()
|
|
||||||
|
|
||||||
if "me" in recipient:
|
|
||||||
try:
|
try:
|
||||||
speak("What should I say? ")
|
if debug == "True":print("Recognizing....")
|
||||||
content = takeCommand()
|
else:pass
|
||||||
|
query = r.recognize_google(audio, language="en-in")
|
||||||
|
if debug == "True":print("user said: " + query)
|
||||||
|
else:pass
|
||||||
|
|
||||||
server = smtplib.SMTP("smtp.gmail.com", 587)
|
except sr.UnknownValueError:
|
||||||
server.ehlo()
|
if debug == "True":print("Sorry Could You please try again")
|
||||||
server.starttls()
|
else:pass
|
||||||
server.login("Your_Username", "Your_Password")
|
speak("Sorry Could You please try again")
|
||||||
server.sendmail("Your_Username", "Recipient_Username", content)
|
|
||||||
server.close()
|
|
||||||
speak("Email sent!")
|
|
||||||
except Exception:
|
|
||||||
speak("Sorry Sir! I am unable to send your message at this moment!")
|
|
||||||
|
|
||||||
elif "nothing" in query or "abort" in query or "stop" in query:
|
except Exception as e:
|
||||||
speak("okay")
|
if debug == "True":
|
||||||
speak("Bye Sir, have a good day.")
|
print(e)
|
||||||
sys.exit()
|
print("Say That Again Please")
|
||||||
|
else:pass
|
||||||
|
query = None
|
||||||
|
|
||||||
elif "hello" in query:
|
return query
|
||||||
speak("Hello Sir")
|
|
||||||
|
|
||||||
elif "bye" in query:
|
speak("Initializing Jarvis....")
|
||||||
speak("Bye Sir, have a good day.")
|
wishMe(MASTER)
|
||||||
sys.exit()
|
while True:
|
||||||
|
query = takeCommand()
|
||||||
|
|
||||||
elif "play music" in query:
|
# logic for executing basic tasks
|
||||||
music_folder = "Your_music_folder_path(absolute_path)"
|
if "wikipedia" in query.lower():
|
||||||
music = ("music1", "music2", "music3", "music4")
|
speak("Searching wikipedia....")
|
||||||
random_music = music_folder + random.choice(music) + ".mp3"
|
query = query.replace("wikipedia", "")
|
||||||
speak("Playing your request")
|
results = wikipedia.summary(query, sentences=2)
|
||||||
mixer.music.load(random_music)
|
if debug == "True":print(results)
|
||||||
mixer.music.play()
|
else:pass
|
||||||
|
speak(results)
|
||||||
|
|
||||||
elif "pause music" in query:
|
elif "what's up" in query or "how are you" in query:
|
||||||
mixer.music.pause()
|
stMsgs = [
|
||||||
|
"Just doing my thing!",
|
||||||
|
"I am fine!",
|
||||||
|
"Nice!",
|
||||||
|
"I am nice and full of energy",
|
||||||
|
]
|
||||||
|
speak(random.choice(stMsgs))
|
||||||
|
|
||||||
elif "stop music" in query:
|
elif "open" in query.lower():
|
||||||
mixer.music.stop()
|
website = query.replace("open", "").strip().lower()
|
||||||
|
try:
|
||||||
|
open_url(popular_websites[website])
|
||||||
|
except KeyError: # If the website is unknown
|
||||||
|
if debug == "True":print(f"Unknown website: {website}")
|
||||||
|
else:pass
|
||||||
|
speak(f"Sorry, i don't know the website {website}")
|
||||||
|
speak(f"¿Do you want me to search {website} in the web?")
|
||||||
|
if takeCommand() == "yes":
|
||||||
|
search(website, search_engine )
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
elif "unpause" in query:
|
|
||||||
mixer.music.unpause()
|
|
||||||
|
|
||||||
speak("Next Command! Sir!")
|
elif "search" in query.lower():
|
||||||
|
search_query = query.split("for")[-1]
|
||||||
|
search(search_query, search_engine)
|
||||||
|
|
||||||
|
elif "mail" in query:
|
||||||
|
speak("Who is the recipient? ")
|
||||||
|
recipient = takeCommand()
|
||||||
|
|
||||||
|
if "me" in recipient:
|
||||||
|
try:
|
||||||
|
speak("What should I say? ")
|
||||||
|
content = takeCommand()
|
||||||
|
|
||||||
|
server = smtplib.SMTP(config['EMAIL']['server'], config['EMAIL']['port'])
|
||||||
|
server.ehlo()
|
||||||
|
server.starttls()
|
||||||
|
server.login(config['EMAIL']['username'], config['EMAIL']['password'])
|
||||||
|
server.sendmail(config['EMAIL']['username'], recipient, content)
|
||||||
|
server.close()
|
||||||
|
speak("Email sent!")
|
||||||
|
except Exception:
|
||||||
|
speak("Sorry Sir! I am unable to send your message at this moment!")
|
||||||
|
|
||||||
|
elif "nothing" in query or "abort" in query or "stop" in query:
|
||||||
|
speak("okay")
|
||||||
|
speak("Bye Sir, have a good day.")
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
elif "hello" in query:
|
||||||
|
speak("Hello Sir")
|
||||||
|
|
||||||
|
elif "bye" in query:
|
||||||
|
speak("Bye Sir, have a good day.")
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
elif "play music" in query:
|
||||||
|
try:
|
||||||
|
music_folder = "Your_music_folder_path(absolute_path)"
|
||||||
|
music = ("music1", "music2", "music3", "music4")
|
||||||
|
random_music = music_folder + random.choice(music) + ".mp3"
|
||||||
|
speak("Playing your request")
|
||||||
|
mixer.music.load(random_music)
|
||||||
|
mixer.music.play()
|
||||||
|
except Exception as e:
|
||||||
|
speak(e)
|
||||||
|
|
||||||
|
|
||||||
|
elif "pause music" in query:
|
||||||
|
mixer.music.pause()
|
||||||
|
|
||||||
|
elif "stop music" in query:
|
||||||
|
mixer.music.stop()
|
||||||
|
|
||||||
|
elif "unpause" in query:
|
||||||
|
mixer.music.unpause()
|
||||||
|
|
||||||
|
speak("Next Command! Sir!")
|
||||||
|
|
||||||
|
if os.path.isfile('./config.ini'):
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read('config.ini')
|
||||||
|
main()
|
||||||
|
else:
|
||||||
|
print('You need a config.ini file. Check the documentation in the Github Repository.')
|
||||||
13
config.ini
Normal file
13
config.ini
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
[DEFAULT]
|
||||||
|
MASTER = YourName
|
||||||
|
search_engine = Google
|
||||||
|
#Google/Bing/DuckDuckGo/Youtube
|
||||||
|
debug = True
|
||||||
|
#True/False
|
||||||
|
[EMAIL]
|
||||||
|
server = smtp.gmail.com
|
||||||
|
#You can use any email service provider that allows SMTP. Check docs to see how to configure your email provider here.
|
||||||
|
port = 587
|
||||||
|
#In most cases you will need this port.
|
||||||
|
username =
|
||||||
|
password =
|
||||||
Loading…
x
Reference in New Issue
Block a user