Changed execution estructure and fixed bugs
This commit is contained in:
parent
cb3cb604b1
commit
c11968cd91
@ -1,23 +1,27 @@
|
|||||||
import datetime
|
|
||||||
import random
|
|
||||||
import smtplib
|
|
||||||
import sys
|
|
||||||
import webbrowser
|
|
||||||
import pyttsx3
|
|
||||||
import wikipedia
|
|
||||||
import configparser
|
import configparser
|
||||||
import os
|
import os
|
||||||
import requests
|
|
||||||
|
|
||||||
import speech_recognition as sr
|
import speech_recognition as sr
|
||||||
|
|
||||||
from pygame import mixer
|
from pygame import mixer
|
||||||
|
|
||||||
mixer.init()
|
from actions import search_engine_selector, speak, wishMe
|
||||||
|
|
||||||
engine = pyttsx3.init("sapi5")
|
from commands import (
|
||||||
voices = engine.getProperty("voices")
|
command_bye,
|
||||||
engine.setProperty("voice", voices[0].id)
|
command_hello,
|
||||||
|
command_mail,
|
||||||
|
command_nothing,
|
||||||
|
command_pauseMusic,
|
||||||
|
command_playMusic,
|
||||||
|
command_stopMusic,
|
||||||
|
command_unpauseMusic,
|
||||||
|
command_whatsup,
|
||||||
|
command_wikipedia,
|
||||||
|
command_open,
|
||||||
|
command_search
|
||||||
|
)
|
||||||
|
|
||||||
|
mixer.init()
|
||||||
|
|
||||||
popular_websites = {
|
popular_websites = {
|
||||||
"google": "https://www.google.com",
|
"google": "https://www.google.com",
|
||||||
@ -27,159 +31,56 @@ popular_websites = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# this funcition checks which search engine is selected in config file.
|
|
||||||
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:
|
|
||||||
# If none of default ones selected
|
|
||||||
try:
|
|
||||||
if requests.get(
|
|
||||||
f"https://{config['DEFAULT']['search_engine'].lower()}.com",
|
|
||||||
params={'q': 'example'}
|
|
||||||
).status_code == 200:
|
|
||||||
return (
|
|
||||||
f"https://{config['DEFAULT']['search_engine'].lower()}.com"
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
return "https://www.google.com"
|
|
||||||
except Exception as e:
|
|
||||||
print(e)
|
|
||||||
return "https://www.google.com"
|
|
||||||
|
|
||||||
|
|
||||||
def open_url(url):
|
|
||||||
webbrowser.open(url)
|
|
||||||
chrome_path = r"open -a /Applications/Google\ Chrome.app %s"
|
|
||||||
webbrowser.get(chrome_path).open(url)
|
|
||||||
|
|
||||||
|
|
||||||
def search(search_query, search_engine):
|
|
||||||
open_url(f"{search_engine}/search?q={search_query}")
|
|
||||||
|
|
||||||
|
|
||||||
def speak(text):
|
|
||||||
engine.say(text)
|
|
||||||
engine.runAndWait()
|
|
||||||
|
|
||||||
|
|
||||||
def wishMe(MASTER):
|
|
||||||
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")
|
|
||||||
|
|
||||||
|
|
||||||
def main(search_engine, takeCommand, debug):
|
def main(search_engine, takeCommand, debug):
|
||||||
while True:
|
while True:
|
||||||
query = takeCommand()
|
query = takeCommand()
|
||||||
|
|
||||||
# logic for executing basic tasks
|
# logic for executing basic tasks
|
||||||
if "wikipedia" in query.lower():
|
if "wikipedia" in query.lower():
|
||||||
speak("Searching wikipedia....")
|
command_wikipedia(
|
||||||
query = query.replace("wikipedia", "")
|
speak,
|
||||||
results = wikipedia.summary(query, sentences=2)
|
debug,
|
||||||
if debug == "True":
|
query
|
||||||
print(results)
|
)
|
||||||
else:
|
|
||||||
pass
|
|
||||||
speak(results)
|
|
||||||
|
|
||||||
elif "what's up" in query or "how are you" in query:
|
elif "what's up" in query or "how are you" in query:
|
||||||
stMsgs = [
|
command_whatsup()
|
||||||
"Just doing my thing!",
|
|
||||||
"I am fine!",
|
|
||||||
"Nice!",
|
|
||||||
"I am nice and full of energy",
|
|
||||||
]
|
|
||||||
speak(random.choice(stMsgs))
|
|
||||||
|
|
||||||
elif "open" in query.lower():
|
elif "open" in query.lower():
|
||||||
website = query.replace("open", "").strip().lower()
|
command_open(
|
||||||
try:
|
query,
|
||||||
open_url(popular_websites[website])
|
popular_websites,
|
||||||
except KeyError: # If the website is unknown
|
debug,
|
||||||
if debug == "True":
|
search_engine,
|
||||||
print(f"Unknown website: {website}")
|
takeCommand
|
||||||
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 "search" in query.lower():
|
elif "search" in query.lower():
|
||||||
search_query = query.split("for")[-1]
|
command_search(query, search_engine)
|
||||||
search(search_query, search_engine)
|
|
||||||
|
|
||||||
elif "mail" in query:
|
elif "mail" in query:
|
||||||
speak("Who is the recipient? ")
|
command_mail(takeCommand)
|
||||||
recipient = takeCommand()
|
|
||||||
|
|
||||||
if "me" in recipient:
|
|
||||||
try:
|
|
||||||
speak("What should I say? ")
|
|
||||||
content = takeCommand()
|
|
||||||
|
|
||||||
email = config['EMAIL']
|
|
||||||
server = smtplib.SMTP(email['server'], email['port'])
|
|
||||||
server.ehlo()
|
|
||||||
server.starttls()
|
|
||||||
server.login(email['username'], email['password'])
|
|
||||||
server.sendmail(email['username'], recipient, content)
|
|
||||||
server.close()
|
|
||||||
speak("Email sent!")
|
|
||||||
except Exception:
|
|
||||||
speak("Sorry Sir!")
|
|
||||||
speak("I am unable to send your message at this moment!")
|
|
||||||
|
|
||||||
elif "nothing" in query or "abort" in query or "stop" in query:
|
elif "nothing" in query or "abort" in query or "stop" in query:
|
||||||
speak("okay")
|
command_nothing()
|
||||||
speak("Bye Sir, have a good day.")
|
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
elif "hello" in query:
|
elif "hello" in query:
|
||||||
speak("Hello Sir")
|
command_hello()
|
||||||
|
|
||||||
elif "bye" in query:
|
elif "bye" in query:
|
||||||
speak("Bye Sir, have a good day.")
|
command_bye()
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
elif "play music" in query:
|
elif "play music" in query:
|
||||||
try:
|
command_playMusic()
|
||||||
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:
|
elif "pause music" in query:
|
||||||
mixer.music.pause()
|
command_pauseMusic()
|
||||||
|
|
||||||
elif "stop music" in query:
|
elif "stop music" in query:
|
||||||
mixer.music.stop()
|
command_stopMusic
|
||||||
|
|
||||||
elif "unpause" in query:
|
elif "unpause" in query:
|
||||||
mixer.music.unpause()
|
command_unpauseMusic()
|
||||||
|
|
||||||
speak("Next Command! Sir!")
|
speak("Next Command! Sir!")
|
||||||
|
|
||||||
@ -187,7 +88,7 @@ def main(search_engine, takeCommand, debug):
|
|||||||
def run():
|
def run():
|
||||||
MASTER = config['DEFAULT']['MASTER']
|
MASTER = config['DEFAULT']['MASTER']
|
||||||
|
|
||||||
search_engine = search_engine_selector()
|
search_engine = search_engine_selector(config)
|
||||||
|
|
||||||
debug = config['DEFAULT']['debug']
|
debug = config['DEFAULT']['debug']
|
||||||
|
|
||||||
@ -226,7 +127,7 @@ def run():
|
|||||||
|
|
||||||
return query
|
return query
|
||||||
|
|
||||||
speak("Initializing Jarvis....")
|
speak(text="Initializing Jarvis....")
|
||||||
wishMe(MASTER)
|
wishMe(MASTER)
|
||||||
main(search_engine, takeCommand, debug)
|
main(search_engine, takeCommand, debug)
|
||||||
|
|
||||||
|
|||||||
BIN
__pycache__/Jarvis2_4windows.cpython-37.pyc
Normal file
BIN
__pycache__/Jarvis2_4windows.cpython-37.pyc
Normal file
Binary file not shown.
BIN
__pycache__/actions.cpython-37.pyc
Normal file
BIN
__pycache__/actions.cpython-37.pyc
Normal file
Binary file not shown.
BIN
__pycache__/commands.cpython-37.pyc
Normal file
BIN
__pycache__/commands.cpython-37.pyc
Normal file
Binary file not shown.
64
actions.py
Normal file
64
actions.py
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
import webbrowser
|
||||||
|
import requests
|
||||||
|
import datetime
|
||||||
|
import pyttsx3
|
||||||
|
|
||||||
|
engine = pyttsx3.init("sapi5")
|
||||||
|
voices = engine.getProperty("voices")
|
||||||
|
engine.setProperty("voice", voices[0].id)
|
||||||
|
|
||||||
|
|
||||||
|
def search_engine_selector(config):
|
||||||
|
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:
|
||||||
|
# If none of default ones selected
|
||||||
|
try:
|
||||||
|
if requests.get(
|
||||||
|
f"https://{config['DEFAULT']['search_engine'].lower()}.com",
|
||||||
|
params={'q': 'example'}
|
||||||
|
).status_code == 200:
|
||||||
|
return (
|
||||||
|
f"https://{config['DEFAULT']['search_engine'].lower()}.com"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return "https://www.google.com"
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
return "https://www.google.com"
|
||||||
|
|
||||||
|
|
||||||
|
def open_url(url):
|
||||||
|
webbrowser.open(url)
|
||||||
|
chrome_path = r"open -a /Applications/Google\ Chrome.app %s"
|
||||||
|
webbrowser.get(chrome_path).open(url)
|
||||||
|
|
||||||
|
|
||||||
|
def search(search_query, search_engine):
|
||||||
|
open_url(f"{search_engine}/search?q={search_query}")
|
||||||
|
|
||||||
|
|
||||||
|
def speak(text):
|
||||||
|
engine.say(text)
|
||||||
|
engine.runAndWait()
|
||||||
|
|
||||||
|
|
||||||
|
def wishMe(MASTER):
|
||||||
|
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")
|
||||||
115
commands.py
Normal file
115
commands.py
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
import wikipedia
|
||||||
|
import smtplib
|
||||||
|
import sys
|
||||||
|
import random
|
||||||
|
import configparser
|
||||||
|
|
||||||
|
from pygame import mixer
|
||||||
|
|
||||||
|
from actions import open_url, search, speak
|
||||||
|
|
||||||
|
config = configparser.ConfigParser() # if exists loads library.
|
||||||
|
config.read('config.ini')
|
||||||
|
|
||||||
|
|
||||||
|
def command_wikipedia(debug, query):
|
||||||
|
speak("Searching wikipedia....")
|
||||||
|
query = query.replace("wikipedia", "")
|
||||||
|
results = wikipedia.summary(query, sentences=2)
|
||||||
|
if debug == "True":
|
||||||
|
print(results)
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
speak(results)
|
||||||
|
|
||||||
|
|
||||||
|
def command_whatsup():
|
||||||
|
stMsgs = [
|
||||||
|
"Just doing my thing!",
|
||||||
|
"I am fine!",
|
||||||
|
"Nice!",
|
||||||
|
"I am nice and full of energy",
|
||||||
|
]
|
||||||
|
speak(random.choice(stMsgs))
|
||||||
|
|
||||||
|
|
||||||
|
def command_open(query, popular_websites, debug, search_engine, takeCommand):
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
def command_search(query, search_engine):
|
||||||
|
search_query = query.split("for")[-1]
|
||||||
|
search(search_query, search_engine)
|
||||||
|
|
||||||
|
|
||||||
|
def command_mail(takeCommand):
|
||||||
|
speak("Who is the recipient? ")
|
||||||
|
recipient = takeCommand()
|
||||||
|
|
||||||
|
try:
|
||||||
|
speak("What should I say? ")
|
||||||
|
content = takeCommand()
|
||||||
|
|
||||||
|
email = config['EMAIL']
|
||||||
|
server = smtplib.SMTP(email['server'], email['port'])
|
||||||
|
server.ehlo()
|
||||||
|
server.starttls()
|
||||||
|
server.login(email['username'], email['password'])
|
||||||
|
server.sendmail(email['username'], recipient, content)
|
||||||
|
server.close()
|
||||||
|
speak("Email sent!")
|
||||||
|
except Exception:
|
||||||
|
speak("Sorry Sir!")
|
||||||
|
speak("I am unable to send your message at this moment!")
|
||||||
|
|
||||||
|
|
||||||
|
def command_nothing():
|
||||||
|
speak("okay")
|
||||||
|
speak("Bye Sir, have a good day.")
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
|
||||||
|
def command_hello():
|
||||||
|
speak("Hello Sir")
|
||||||
|
|
||||||
|
|
||||||
|
def command_bye():
|
||||||
|
speak("Bye Sir, have a good day.")
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
|
||||||
|
def command_playMusic():
|
||||||
|
try:
|
||||||
|
music_folder = config['DEFAULT']['musicPath']
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
def command_pauseMusic():
|
||||||
|
mixer.music.pause()
|
||||||
|
|
||||||
|
|
||||||
|
def command_stopMusic():
|
||||||
|
mixer.music.stop()
|
||||||
|
|
||||||
|
|
||||||
|
def command_unpauseMusic():
|
||||||
|
mixer.music.unpause()
|
||||||
@ -2,8 +2,9 @@
|
|||||||
MASTER = YourName
|
MASTER = YourName
|
||||||
search_engine = Google
|
search_engine = Google
|
||||||
#Google/Bing/DuckDuckGo/Youtube
|
#Google/Bing/DuckDuckGo/Youtube
|
||||||
debug = False
|
debug = True
|
||||||
#True/False
|
#True/False
|
||||||
|
musicPath =
|
||||||
[EMAIL]
|
[EMAIL]
|
||||||
server = smtp.gmail.com
|
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.
|
#You can use any email service provider that allows SMTP. Check docs to see how to configure your email provider here.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user