From 4b0c5d89f0d5a7e36deba2fc0f5576234bfbdbe0 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 7 Oct 2021 23:20:15 +0200 Subject: [PATCH] Date, time, and pep8-naming --- .github/workflows/lint_python.yml | 4 +-- Jarvis2.py | 60 +++++++++++++++++-------------- Jarvis2_4windows.py | 19 +++++----- 3 files changed, 45 insertions(+), 38 deletions(-) mode change 100644 => 100755 Jarvis2.py diff --git a/.github/workflows/lint_python.yml b/.github/workflows/lint_python.yml index 335a50d..d850a6f 100644 --- a/.github/workflows/lint_python.yml +++ b/.github/workflows/lint_python.yml @@ -7,8 +7,8 @@ jobs: - uses: actions/checkout@v2 - uses: actions/setup-python@v2 - run: pip install --upgrade pip wheel - - run: pip install bandit black codespell flake8 flake8-bugbear - flake8-comprehensions isort mypy pytest pyupgrade safety + - run: pip install bandit black codespell flake8 flake8-bugbear flake8-comprehensions + flake8-return isort mypy pep8-naming pytest pyupgrade safety - run: bandit --recursive --skip B311,B605 . - run: black --check . || true - run: codespell --ignore-words-list=ans diff --git a/Jarvis2.py b/Jarvis2.py old mode 100644 new mode 100755 index e2710c8..b37d890 --- a/Jarvis2.py +++ b/Jarvis2.py @@ -1,4 +1,7 @@ +#!/usr/bin/env python3 + import datetime +import getpass import os import random import smtplib @@ -10,12 +13,12 @@ import speech_recognition as sr import wikipedia print("Initializing Jarvis....") -MASTER = "Harsha" - +MASTER = getpass.getuser() engine = pyttsx3.init("nsss") voices = engine.getProperty("voices") engine.setProperty("voice", voices[0].id) + popular_websites = { "google": "https://www.google.com", "youtube": "https://www.youtube.com", @@ -47,15 +50,17 @@ def speak(text): engine.runAndWait() -def wishMe(): +def print_and_speak(text): + print(text) + speak(text) + + +def wish_me(): hour = datetime.datetime.now().hour - # print(hour) - if hour >= 0 and hour < 12: + if hour < 12: speak("Good Morning" + MASTER) - - elif hour >= 12 and hour < 18: + elif hour < 18: speak("Good Afternoon" + MASTER) - else: speak("Good Evening" + MASTER) @@ -65,50 +70,53 @@ def wishMe(): # This is where our programme begins.... -def takeCommand(): +def take_command(): r = sr.Recognizer() with sr.Microphone() as source: print("Listening....") r.pause_threshold = 0.5 audio = r.listen(source) - query = " " + print("Recognizing....") + query = "" try: - print("Recognizing....") query = r.recognize_google(audio, language="en-in") - print("user said: " + query) + print("User said: " + query) except sr.UnknownValueError: - print("Sorry Could You please try again") + print("Sorry could you please try again?") except Exception as e: print(e) - print("Say That Again Please") - query = None + print("Say that again, please?") return query speak("Initializing Jarvis....") -wishMe() -query = takeCommand() +wish_me() +query = take_command().lower() # 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) + print_and_speak(wikipedia.summary(query, sentences=2)) elif "what's up" in query or "how are you" in query: - stMsgs = [ + st_msgs = ( "Just doing my thing!", "I am fine!", "Nice!", "I am nice and full of energy", - ] - speak(random.choice(stMsgs)) + ) + speak(random.choice(st_msgs)) + +elif "date" in query: + print_and_speak(f"{datetime.datetime.now():%A, %B %d, %Y}") + +elif "time" in query: + print_and_speak(f"{datetime.datetime.now():%I %M %p}") elif "open" in query.lower(): website = query.replace("open", "").strip().lower() @@ -116,7 +124,7 @@ elif "open" in query.lower(): 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}") + speak(f"Sorry, I don't know the website {website}") elif "search" in query.lower(): search_query = query.split("for")[-1] @@ -125,12 +133,12 @@ elif "search" in query.lower(): elif "email" in query: speak("Who is the recipient? ") - recipient = takeCommand() + recipient = take_command() if "me" in recipient: try: speak("What should I say? ") - content = takeCommand() + content = take_command() server = smtplib.SMTP("smtp.gmail.com", 587) server.ehlo() diff --git a/Jarvis2_4windows.py b/Jarvis2_4windows.py index ef5c3d5..0294c51 100644 --- a/Jarvis2_4windows.py +++ b/Jarvis2_4windows.py @@ -49,7 +49,7 @@ def speak(text): engine.runAndWait() -def wishMe(): +def wish_me(): hour = datetime.datetime.now().hour # print(hour) if hour >= 0 and hour < 12: @@ -65,14 +65,14 @@ def wishMe(): # This is where our programme begins.... -def takeCommand(): +def take_command(): r = sr.Recognizer() with sr.Microphone() as source: print("Listening....") r.pause_threshold = 0.5 audio = r.listen(source) - query = " " + query = "" try: print("Recognizing....") query = r.recognize_google(audio, language="en-in") @@ -84,15 +84,14 @@ def takeCommand(): except Exception as e: print(e) print("Say That Again Please") - query = None return query speak("Initializing Jarvis....") -wishMe() +wish_me() while True: - query = takeCommand() + query = take_command() # logic for executing basic tasks if "wikipedia" in query.lower(): @@ -103,13 +102,13 @@ while True: speak(results) elif "what's up" in query or "how are you" in query: - stMsgs = [ + st_msgs = [ "Just doing my thing!", "I am fine!", "Nice!", "I am nice and full of energy", ] - speak(random.choice(stMsgs)) + speak(random.choice(st_msgs)) elif "open" in query.lower(): website = query.replace("open", "").strip().lower() @@ -126,12 +125,12 @@ while True: elif "mail" in query: speak("Who is the recipient? ") - recipient = takeCommand() + recipient = take_command() if "me" in recipient: try: speak("What should I say? ") - content = takeCommand() + content = take_command() server = smtplib.SMTP("smtp.gmail.com", 587) server.ehlo()