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 wikipedia
|
2021-10-06 21:48:16 +02:00
|
|
|
import configparser
|
|
|
|
|
import os
|
2021-10-06 22:12:24 +02:00
|
|
|
import requests
|
|
|
|
|
|
|
|
|
|
import speech_recognition as sr
|
|
|
|
|
|
|
|
|
|
from pygame import mixer
|
2021-10-03 15:22:09 +05:30
|
|
|
|
2021-10-06 21:48:16 +02:00
|
|
|
mixer.init()
|
2021-10-03 15:22:09 +05:30
|
|
|
|
2021-10-06 08:14:43 +02:00
|
|
|
engine = pyttsx3.init("sapi5")
|
|
|
|
|
voices = engine.getProperty("voices")
|
|
|
|
|
engine.setProperty("voice", voices[0].id)
|
2021-10-04 07:56:18 +05:30
|
|
|
|
2021-10-06 22:12:24 +02:00
|
|
|
|
2021-10-07 16:09:11 +02:00
|
|
|
<<<<<<< HEAD
|
|
|
|
|
# this funcition checks wich search engine is selected in config file.
|
|
|
|
|
def search_engine_selector():
|
|
|
|
|
=======
|
2021-10-06 22:23:57 +02:00
|
|
|
def search_engine_selector(): # this funcition checks which search engine is selected in config file.
|
2021-10-07 16:09:11 +02:00
|
|
|
>>>>>>> 05cd5b49df1eb083cc6e39fb9b246fd6630aa75f
|
2021-10-06 21:48:16 +02:00
|
|
|
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"
|
2021-10-07 16:09:11 +02:00
|
|
|
else:
|
|
|
|
|
# If none of default ones selected
|
2021-10-06 22:12:24 +02:00
|
|
|
try:
|
2021-10-07 16:09:11 +02:00
|
|
|
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"
|
|
|
|
|
|
2021-10-04 07:56:18 +05:30
|
|
|
|
|
|
|
|
def open_url(url):
|
2021-10-07 16:09:11 +02:00
|
|
|
webbrowser.open(url)
|
|
|
|
|
chrome_path = r"open -a /Applications/Google\ Chrome.app %s"
|
|
|
|
|
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):
|
2021-10-06 21:48:16 +02:00
|
|
|
open_url(f"{search_engine}/search?q={search_query}")
|
2021-10-03 15:22:09 +05:30
|
|
|
|
2021-10-07 16:09:11 +02:00
|
|
|
|
2021-10-03 15:22:09 +05:30
|
|
|
def speak(text):
|
2021-10-07 16:09:11 +02:00
|
|
|
engine.say(text)
|
|
|
|
|
engine.runAndWait()
|
|
|
|
|
|
2021-10-06 21:48:16 +02:00
|
|
|
|
|
|
|
|
def wishMe(MASTER):
|
2021-10-07 16:09:11 +02:00
|
|
|
hour = datetime.datetime.now().hour
|
|
|
|
|
# print(hour)
|
|
|
|
|
if hour >= 0 and hour < 12:
|
|
|
|
|
speak("Good Morning" + MASTER)
|
2021-10-06 21:48:16 +02:00
|
|
|
|
2021-10-07 16:09:11 +02:00
|
|
|
elif hour >= 12 and hour < 18:
|
|
|
|
|
speak("Good Afternoon" + MASTER)
|
2021-10-06 21:48:16 +02:00
|
|
|
|
2021-10-07 16:09:11 +02:00
|
|
|
else:
|
|
|
|
|
speak("Good Evening" + MASTER)
|
|
|
|
|
|
|
|
|
|
# speak("Hey I am Jarvis. How may I help you")
|
2021-10-06 21:48:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
MASTER = config['DEFAULT']['MASTER']
|
2021-10-03 15:22:09 +05:30
|
|
|
|
2021-10-06 21:48:16 +02:00
|
|
|
popular_websites = {
|
|
|
|
|
"google": "https://www.google.com",
|
|
|
|
|
"youtube": "https://www.youtube.com",
|
|
|
|
|
"wikipedia": "https://www.wikipedia.org",
|
|
|
|
|
"amazon": "https://www.amazon.com",
|
|
|
|
|
}
|
2021-10-03 15:22:09 +05:30
|
|
|
|
2021-10-06 21:48:16 +02:00
|
|
|
search_engine = search_engine_selector()
|
2021-10-03 15:22:09 +05:30
|
|
|
|
2021-10-06 21:48:16 +02:00
|
|
|
debug = config['DEFAULT']['debug']
|
2021-10-03 15:22:09 +05:30
|
|
|
|
2021-10-06 21:48:16 +02:00
|
|
|
if debug == "True":
|
|
|
|
|
def takeCommand():
|
|
|
|
|
query = input("Command |--> ")
|
|
|
|
|
return query
|
2021-10-03 15:22:09 +05:30
|
|
|
else:
|
2021-10-06 21:48:16 +02:00
|
|
|
def takeCommand():
|
|
|
|
|
r = sr.Recognizer()
|
|
|
|
|
with sr.Microphone() as source:
|
2021-10-07 16:09:11 +02:00
|
|
|
print("Listening....")
|
2021-10-06 21:48:16 +02:00
|
|
|
r.pause_threshold = 0.5
|
|
|
|
|
audio = r.listen(source)
|
|
|
|
|
|
|
|
|
|
query = " "
|
2021-10-03 15:22:09 +05:30
|
|
|
try:
|
2021-10-07 16:09:11 +02:00
|
|
|
print("Recognizing....")
|
2021-10-06 21:48:16 +02:00
|
|
|
query = r.recognize_google(audio, language="en-in")
|
2021-10-07 16:09:11 +02:00
|
|
|
print("user said: " + query)
|
2021-10-06 21:48:16 +02:00
|
|
|
|
|
|
|
|
except sr.UnknownValueError:
|
2021-10-07 16:09:11 +02:00
|
|
|
if debug == "True":
|
|
|
|
|
print("Sorry Could You please try again")
|
|
|
|
|
else:
|
|
|
|
|
pass
|
2021-10-06 21:48:16 +02:00
|
|
|
speak("Sorry Could You please try again")
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
if debug == "True":
|
|
|
|
|
print(e)
|
|
|
|
|
print("Say That Again Please")
|
2021-10-07 16:09:11 +02:00
|
|
|
else:
|
|
|
|
|
pass
|
2021-10-06 21:48:16 +02:00
|
|
|
query = None
|
|
|
|
|
|
|
|
|
|
return query
|
|
|
|
|
|
|
|
|
|
speak("Initializing Jarvis....")
|
|
|
|
|
wishMe(MASTER)
|
|
|
|
|
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)
|
2021-10-07 16:09:11 +02:00
|
|
|
if debug == "True":
|
|
|
|
|
print(results)
|
|
|
|
|
else:
|
|
|
|
|
pass
|
2021-10-06 21:48:16 +02:00
|
|
|
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 KeyError: # If the website is unknown
|
2021-10-07 16:09:11 +02:00
|
|
|
if debug == "True":
|
|
|
|
|
print(f"Unknown website: {website}")
|
|
|
|
|
else:
|
|
|
|
|
pass
|
2021-10-06 21:48:16 +02:00
|
|
|
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":
|
2021-10-07 16:09:11 +02:00
|
|
|
search(website, search_engine)
|
2021-10-06 21:48:16 +02:00
|
|
|
else:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
2021-10-07 16:09:11 +02:00
|
|
|
email = config['EMAIL']
|
|
|
|
|
server = smtplib.SMTP(email['server'], email['port'])
|
2021-10-06 21:48:16 +02:00
|
|
|
server.ehlo()
|
|
|
|
|
server.starttls()
|
2021-10-07 16:09:11 +02:00
|
|
|
server.login(email['username'], email['password'])
|
|
|
|
|
server.sendmail(email['username'], recipient, content)
|
2021-10-06 21:48:16 +02:00
|
|
|
server.close()
|
|
|
|
|
speak("Email sent!")
|
|
|
|
|
except Exception:
|
2021-10-07 16:09:11 +02:00
|
|
|
speak("Sorry Sir!")
|
|
|
|
|
speak("I am unable to send your message at this moment!")
|
2021-10-06 21:48:16 +02:00
|
|
|
|
|
|
|
|
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!")
|
|
|
|
|
|
2021-10-07 16:09:11 +02:00
|
|
|
<<<<<<< HEAD
|
|
|
|
|
|
|
|
|
|
if os.path.isfile('./config.ini'): # Checks if config.ini exists.
|
|
|
|
|
config = configparser.ConfigParser() # if exists loads library.
|
|
|
|
|
config.read('config.ini') # and also the file.
|
|
|
|
|
main() # Then launchs the main program
|
|
|
|
|
else:
|
|
|
|
|
# if it doesn't exist it drops an error message and exits.
|
|
|
|
|
print('You need a config.ini file.')
|
|
|
|
|
print('Check the documentation in the Github Repository.')
|
|
|
|
|
=======
|
2021-10-06 22:12:24 +02:00
|
|
|
if os.path.isfile('./config.ini'): #Checks if config.ini exists.
|
|
|
|
|
config = configparser.ConfigParser() #if exists loads library.
|
|
|
|
|
config.read('config.ini') #and also the file.
|
2021-10-06 22:23:57 +02:00
|
|
|
main() #Then launches the main program
|
2021-10-06 21:48:16 +02:00
|
|
|
else:
|
2021-10-06 22:23:57 +02:00
|
|
|
print('You need a config.ini file. Check the documentation in the Github Repository.') #if it doesn't exist it drops an error message and exits.
|
2021-10-07 16:09:11 +02:00
|
|
|
>>>>>>> 05cd5b49df1eb083cc6e39fb9b246fd6630aa75f
|