Improving Code to met flake 8 requirements

This commit is contained in:
TallGorilla 2021-10-07 16:05:57 +02:00
parent 750a84706d
commit d6004eac28
2 changed files with 69 additions and 49 deletions

View File

@ -20,7 +20,8 @@ voices = engine.getProperty("voices")
engine.setProperty("voice", voices[0].id) engine.setProperty("voice", voices[0].id)
def search_engine_selector(): # this funcition checks wich search engine is selected in config file. # this funcition checks wich search engine is selected in config file.
def search_engine_selector():
if config['DEFAULT']['search_engine'] == 'Google': if config['DEFAULT']['search_engine'] == 'Google':
return "https://www.google.com" return "https://www.google.com"
elif config['DEFAULT']['search_engine'] == 'Bing': elif config['DEFAULT']['search_engine'] == 'Bing':
@ -29,40 +30,52 @@ def search_engine_selector(): # this funcition checks wich search engine is sel
return "https://www.duckduckgo.com" return "https://www.duckduckgo.com"
elif config['DEFAULT']['search_engine'] == 'Youtube': elif config['DEFAULT']['search_engine'] == 'Youtube':
return "https://www.youtube.com" return "https://www.youtube.com"
else: #If none of default ones selected it tries to use https://{config['DEFAULT']['search_engine'].lower()}.com as search engine. else:
#if its a valid url, it returns it as the search engine. # If none of default ones selected
try: try:
if requests.get(f"https://{config['DEFAULT']['search_engine'].lower()}.com", params= {'q':'example'}).status_code == 200: if requests.get(
return f"https://{config['DEFAULT']['search_engine'].lower()}.com" f"https://{config['DEFAULT']['search_engine'].lower()}.com",
params={'q': 'example'}
else: return "https://www.google.com" ).status_code == 200:
except: return "https://www.google.com" 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): 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):
open_url(f"{search_engine}/search?q={search_query}") open_url(f"{search_engine}/search?q={search_query}")
def speak(text): def speak(text):
engine.say(text) engine.say(text)
engine.runAndWait() engine.runAndWait()
def wishMe(MASTER): def wishMe(MASTER):
hour = datetime.datetime.now().hour hour = datetime.datetime.now().hour
# print(hour) # print(hour)
if hour >= 0 and hour < 12: if hour >= 0 and hour < 12:
speak("Good Morning" + MASTER) speak("Good Morning" + MASTER)
elif hour >= 12 and hour < 18: elif hour >= 12 and hour < 18:
speak("Good Afternoon" + MASTER) speak("Good Afternoon" + MASTER)
else: else:
speak("Good Evening" + MASTER) speak("Good Evening" + MASTER)
# speak("Hey I am Jarvis. How may I help you")
# speak("Hey I am Jarvis. How may I help you")
def main(): def main():
MASTER = config['DEFAULT']['MASTER'] MASTER = config['DEFAULT']['MASTER']
@ -86,29 +99,29 @@ def main():
def takeCommand(): def takeCommand():
r = sr.Recognizer() r = sr.Recognizer()
with sr.Microphone() as source: with sr.Microphone() as source:
if debug == "True":print("Listening....") print("Listening....")
else:pass
r.pause_threshold = 0.5 r.pause_threshold = 0.5
audio = r.listen(source) audio = r.listen(source)
query = " " query = " "
try: try:
if debug == "True":print("Recognizing....") print("Recognizing....")
else:pass
query = r.recognize_google(audio, language="en-in") query = r.recognize_google(audio, language="en-in")
if debug == "True":print("user said: " + query) print("user said: " + query)
else:pass
except sr.UnknownValueError: except sr.UnknownValueError:
if debug == "True":print("Sorry Could You please try again") if debug == "True":
else:pass print("Sorry Could You please try again")
else:
pass
speak("Sorry Could You please try again") speak("Sorry Could You please try again")
except Exception as e: except Exception as e:
if debug == "True": if debug == "True":
print(e) print(e)
print("Say That Again Please") print("Say That Again Please")
else:pass else:
pass
query = None query = None
return query return query
@ -123,8 +136,10 @@ def main():
speak("Searching wikipedia....") speak("Searching wikipedia....")
query = query.replace("wikipedia", "") query = query.replace("wikipedia", "")
results = wikipedia.summary(query, sentences=2) results = wikipedia.summary(query, sentences=2)
if debug == "True":print(results) if debug == "True":
else:pass print(results)
else:
pass
speak(results) 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:
@ -141,16 +156,17 @@ def main():
try: try:
open_url(popular_websites[website]) open_url(popular_websites[website])
except KeyError: # If the website is unknown except KeyError: # If the website is unknown
if debug == "True":print(f"Unknown website: {website}") if debug == "True":
else:pass print(f"Unknown website: {website}")
else:
pass
speak(f"Sorry, i don't know the website {website}") speak(f"Sorry, i don't know the website {website}")
speak(f"¿Do you want me to search {website} in the web?") speak(f"¿Do you want me to search {website} in the web?")
if takeCommand() == "yes": if takeCommand() == "yes":
search(website, search_engine ) search(website, search_engine)
else: else:
pass pass
elif "search" in query.lower(): elif "search" in query.lower():
search_query = query.split("for")[-1] search_query = query.split("for")[-1]
search(search_query, search_engine) search(search_query, search_engine)
@ -164,15 +180,17 @@ def main():
speak("What should I say? ") speak("What should I say? ")
content = takeCommand() content = takeCommand()
server = smtplib.SMTP(config['EMAIL']['server'], config['EMAIL']['port']) email = config['EMAIL']
server = smtplib.SMTP(email['server'], email['port'])
server.ehlo() server.ehlo()
server.starttls() server.starttls()
server.login(config['EMAIL']['username'], config['EMAIL']['password']) server.login(email['username'], email['password'])
server.sendmail(config['EMAIL']['username'], recipient, content) server.sendmail(email['username'], recipient, content)
server.close() server.close()
speak("Email sent!") speak("Email sent!")
except Exception: except Exception:
speak("Sorry Sir! I am unable to send your message at this moment!") 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") speak("okay")
@ -197,7 +215,6 @@ def main():
except Exception as e: except Exception as e:
speak(e) speak(e)
elif "pause music" in query: elif "pause music" in query:
mixer.music.pause() mixer.music.pause()
@ -209,9 +226,12 @@ def main():
speak("Next Command! Sir!") speak("Next Command! Sir!")
if os.path.isfile('./config.ini'): #Checks if config.ini exists.
config = configparser.ConfigParser() #if exists loads library. if os.path.isfile('./config.ini'): # Checks if config.ini exists.
config.read('config.ini') #and also the file. config = configparser.ConfigParser() # if exists loads library.
main() #Then launchs the main program config.read('config.ini') # and also the file.
main() # Then launchs the main program
else: else:
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. # 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.')

View File

@ -1,8 +1,8 @@
[DEFAULT] [DEFAULT]
MASTER = YourName MASTER = YourName
search_engine = Google search_engine = Google
#Google/Bing/DuckDuckGo/Youtube #Google/Bing/DuckDuckGo/Youtube
debug = True debug = False
#True/False #True/False
[EMAIL] [EMAIL]
server = smtp.gmail.com server = smtp.gmail.com