From c11968cd91f11aeda6cbe6cabfa9100f1ac5f544 Mon Sep 17 00:00:00 2001 From: TallGorilla <24dasemu@gmail.com> Date: Thu, 7 Oct 2021 17:27:42 +0200 Subject: [PATCH] Changed execution estructure and fixed bugs --- Jarvis2_4windows.py | 181 +++++--------------- __pycache__/Jarvis2_4windows.cpython-37.pyc | Bin 0 -> 3577 bytes __pycache__/actions.cpython-37.pyc | Bin 0 -> 1709 bytes __pycache__/commands.cpython-37.pyc | Bin 0 -> 3414 bytes actions.py | 64 +++++++ commands.py | 115 +++++++++++++ config.ini | 3 +- 7 files changed, 222 insertions(+), 141 deletions(-) create mode 100644 __pycache__/Jarvis2_4windows.cpython-37.pyc create mode 100644 __pycache__/actions.cpython-37.pyc create mode 100644 __pycache__/commands.cpython-37.pyc create mode 100644 actions.py create mode 100644 commands.py diff --git a/Jarvis2_4windows.py b/Jarvis2_4windows.py index 50eec1b..8f58f86 100644 --- a/Jarvis2_4windows.py +++ b/Jarvis2_4windows.py @@ -1,23 +1,27 @@ -import datetime -import random -import smtplib -import sys -import webbrowser -import pyttsx3 -import wikipedia import configparser import os -import requests - import speech_recognition as sr from pygame import mixer -mixer.init() +from actions import search_engine_selector, speak, wishMe -engine = pyttsx3.init("sapi5") -voices = engine.getProperty("voices") -engine.setProperty("voice", voices[0].id) +from commands import ( + command_bye, + 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 = { "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): 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) - if debug == "True": - print(results) - else: - pass - speak(results) + command_wikipedia( + speak, + debug, + query + ) 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)) + command_whatsup() elif "open" in query.lower(): - 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 + command_open( + query, + popular_websites, + debug, + search_engine, + takeCommand + ) elif "search" in query.lower(): - search_query = query.split("for")[-1] - search(search_query, search_engine) + command_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() - - 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!") + command_mail(takeCommand) elif "nothing" in query or "abort" in query or "stop" in query: - speak("okay") - speak("Bye Sir, have a good day.") - sys.exit() + command_nothing() elif "hello" in query: - speak("Hello Sir") + command_hello() elif "bye" in query: - speak("Bye Sir, have a good day.") - sys.exit() + command_bye() 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) + command_playMusic() elif "pause music" in query: - mixer.music.pause() + command_pauseMusic() elif "stop music" in query: - mixer.music.stop() + command_stopMusic elif "unpause" in query: - mixer.music.unpause() + command_unpauseMusic() speak("Next Command! Sir!") @@ -187,7 +88,7 @@ def main(search_engine, takeCommand, debug): def run(): MASTER = config['DEFAULT']['MASTER'] - search_engine = search_engine_selector() + search_engine = search_engine_selector(config) debug = config['DEFAULT']['debug'] @@ -226,7 +127,7 @@ def run(): return query - speak("Initializing Jarvis....") + speak(text="Initializing Jarvis....") wishMe(MASTER) main(search_engine, takeCommand, debug) diff --git a/__pycache__/Jarvis2_4windows.cpython-37.pyc b/__pycache__/Jarvis2_4windows.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..c7c10263557dc2096f3acc7e25b339f9a1146b5c GIT binary patch literal 3577 zcmZ8kOOG4J5$<`A96t7;)oLHIqKq6{HVuoiWyJvuLzcXfBRSSa(XJzz5U`nLx5yEP zGjw;;6bGaak&`%po%kH&z&+-claU{gV@^Ky$v}Vr`3pYftLA7Un~SOHuBxuCM|V~4 zyGu(ohTj*BgXniB8T&U)p8iT0T*asU3BVb564qW|r|sCe+jcGX+MdOJ+qbyXE(5zs zWe~Ijht_yWbx>>92K9D*&}cU-%}RT`5K#4?jbS6zn4Mo&9<#DWf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IOUgEC z*C?2{VSbE^43Lo-L+&$c*u)KFZjER3M8nu0oX^hYrQfb-@|$2I#m~`BJPqD6g~`tWbDOwqm; zxW38UMS`p{NvX8(KB+7cWCa~a3eT}~H(i{ZG|ZmanLR)L9(&e`?3>TJF+b}}$?Po8?0gsP))n(UmG7fHO%)1A z1zMx0_8^vUWaSUaE{J>zidx(uzEtL=U@p)p6F2XhC6CP#gQ?1uBIf3mY2IVz`Zo8Z z;XlJKChbkGnUG70fFzg5yHuSQ!{DQS&WluhOqZ=9!-=iT&u-cqAl!x%ECr2VE%1U% MfgiY#P1NrH06Yu~NdN!< literal 0 HcmV?d00001 diff --git a/actions.py b/actions.py new file mode 100644 index 0000000..956db21 --- /dev/null +++ b/actions.py @@ -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") diff --git a/commands.py b/commands.py new file mode 100644 index 0000000..47185f3 --- /dev/null +++ b/commands.py @@ -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() diff --git a/config.ini b/config.ini index e4e5d32..b8f9bd0 100644 --- a/config.ini +++ b/config.ini @@ -2,8 +2,9 @@ MASTER = YourName search_engine = Google #Google/Bing/DuckDuckGo/Youtube -debug = False +debug = True #True/False +musicPath = [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.