Jarvis: The Trilogy (Part 2)


 Jarvis Part 2 : Source Code


1. Open and Close apps/websites : Open and close apps like word, paint and various websites.

elif "open" in query:
    from Dictapp import openappweb
    openappweb(query)
elif "close" in query:
    from Dictapp import closeappweb
    closeappweb(query)


Make a new file & name it as Dictapp and paste the following code:-

import os
import pyautogui
import webbrowser
import pyttsx3
from time import sleep

engine = pyttsx3.init("sapi5")
voices = engine.getProperty("voices")
engine.setProperty("voice", voices[0].id)
engine.setProperty("rate",200)

def speak(audio):
    engine.say(audio)
    engine.runAndWait()

dictapp = {"commandprompt":"cmd","paint":"paint","word":"winword","excel":"excel","chrome":"chrome","vscode":"code","powerpoint":"powerpnt"}

def openappweb(query):
    speak("Launching, sir")
    if ".com" in query or ".co.in" in query or ".org" in query:
        query = query.replace("open","")
        query = query.replace("jarvis","")
        query = query.replace("launch","")
        query = query.replace(" ","")
        webbrowser.open(f"https://www.{query}")
    else:
        keys = list(dictapp.keys())
        for app in keys:
            if app in query:
                os.system(f"start {dictapp[app]}")

def closeappweb(query):
    speak("Closing,sir")
    if "one tab" in query or "1 tab" in query:
        pyautogui.hotkey("ctrl","w")
        speak("All tabs closed")
    elif "2 tab" in query:
        pyautogui.hotkey("ctrl","w")
        sleep(0.5)
        pyautogui.hotkey("ctrl","w")
        speak("All tabs closed")
    elif "3 tab" in query:
        pyautogui.hotkey("ctrl","w")
        sleep(0.5)
        pyautogui.hotkey("ctrl","w")
        sleep(0.5)
        pyautogui.hotkey("ctrl","w")
        speak("All tabs closed")
       
    elif "4 tab" in query:
        pyautogui.hotkey("ctrl","w")
        sleep(0.5)
        pyautogui.hotkey("ctrl","w")
        sleep(0.5)
        pyautogui.hotkey("ctrl","w")
        sleep(0.5)
        pyautogui.hotkey("ctrl","w")
        speak("All tabs closed")
    elif "5 tab" in query:
        pyautogui.hotkey("ctrl","w")
        sleep(0.5)
        pyautogui.hotkey("ctrl","w")
        sleep(0.5)
        pyautogui.hotkey("ctrl","w")
        sleep(0.5)
        pyautogui.hotkey("ctrl","w")
        sleep(0.5)
        pyautogui.hotkey("ctrl","w")
        speak("All tabs closed")

    else:
        keys = list(dictapp.keys())
        for app in keys:
            if app in query:
                os.system(f"taskkill /f /im {dictapp[app]}.exe")

   

2. Alarm 

elif "set an alarm" in query:
    print("input time example:- 10 and 10 and 10")
    speak("Set the time")
    a = input("Please tell the time :- ")
    alarm(a)
    speak("Done,sir")


Above (if __name__ = "__main__")  write this function :-

def alarm(query):
    timehere = open("Alarmtext.txt","a")
    timehere.write(query)
    timehere.close()
    os.startfile("alarm.py")


Now make a new file & name it alarm.py and paste the following code:-

import pyttsx3
import datetime
import os

engine = pyttsx3.init("sapi5")
voices = engine.getProperty("voices")
engine.setProperty("voice", voices[0].id)
engine.setProperty("rate",200)

def speak(audio):
    engine.say(audio)
    engine.runAndWait()

extractedtime = open("Alarmtext.txt","rt")
time = extractedtime.read()
Time = str(time)
extractedtime.close()

deletetime = open("Alarmtext.txt","r+")
deletetime.truncate(0)
deletetime.close()

def ring(time):
    timeset = str(time)
    timenow = timeset.replace("jarvis","")
    timenow = timenow.replace("set an alarm","")
    timenow = timenow.replace(" and ",":")
    Alarmtime = str(timenow)
    print(Alarmtime)
    while True:
        currenttime = datetime.datetime.now().strftime("%H:%M:%S")
        if currenttime == Alarmtime:
            speak("Alarm ringing,sir")
            os.startfile("music.mp3") #You can choose any music or ringtone
        elif currenttime + "00:00:30" == Alarmtime:
            exit()

ring(time)


3. Youtube Controls like Play, Pause , Mute , Volume up and down 

elif "pause" in query:
    pyautogui.press("k")
    speak("video paused")
elif "play" in query:
    pyautogui.press("k")
    speak("video played")
elif "mute" in query:
    pyautogui.press("m")
    speak("video muted")

elif "volume up" in query:
    from keyboard import volumeup
    speak("Turning volume up,sir")
    volumeup()
elif "volume down" in query:
    from keyboard import volumedown
    speak("Turning volume down, sir")
    volumedown()

Now make a new file & name it keyboard.py and paste the following code:-

from pynput.keyboard import Key,Controller

from time import sleep

keyboard = Controller()

def volumeup():
    for i in range(5):
        keyboard.press(Key.media_volume_up)
        keyboard.release(Key.media_volume_up)
        sleep(0.1)
def volumedown():
    for i in range(5):
        keyboard.press(Key.media_volume_down)
        keyboard.release(Key.media_volume_down)
        sleep(0.1)


4. Reminder

elif "remember that" in query:
    rememberMessage = query.replace("remember that","")
    rememberMessage = query.replace("jarvis","")
    speak("You told me to remember that"+rememberMessage)
    remember = open("Remember.txt","a")
    remember.write(rememberMessage)
    remember.close()
elif "what do you remember" in query:
    remember = open("Remember.txt","r")
    speak("You told me to remember that" + remember.read())


Now make a new file & name it "Remember.txt" :- it will make a new text file

5. Personalized Playlist : Make your own playlist and let jarvis play a random song from it...

elif "tired" in query:
    speak("Playing your favourite songs, sir")
    a = (1,2,3) # You can choose any number of songs (I have only choosen 3)
    b = random.choice(a)
    if b==1:
    webbrowser.open(#Here put the link of your video)


That's IT !

Thanks for Reading , Go and check out the Jarvis (Part 3) !


No comments:

Post a Comment