Jarvis : The Trilogy 2.0 [Part 3]


Jarvis 2.0 [Part 3]

Video Link :-




Source Code : -

1. FOCUS MODE FUNCTION :-

elif "focus mode" in query:
                    a = int(input("Are you sure that you want to enter focus mode :- [1 for YES / 2 for NO "))
                    if (a==1):
                        speak("Entering the focus mode....")
                        os.startfile("D:\\Coding\\Youtube\\Jarvis\\FocusMode.py")
                        exit()

                   
                    else:
                        pass

Open a New File and Name it as FocusMode.py and paste the following code: -

import time
import datetime
import ctypes,sys
def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False
if is_admin():
    current_time = datetime.datetime.now().strftime("%H:%M")
    Stop_time = input("Enter time example:- [10:10]:- ")
    a = current_time.replace(":",".")
    a = float(a)
    b = Stop_time.replace(":",".")
    b = float(b)
    Focus_Time = b-a
    Focus_Time = round(Focus_Time,3)
    host_path ='C:\Windows\System32\drivers\etc\hosts'
    redirect = '127.0.0.1'

   
    print(current_time)
    time.sleep(2)
    website_list = ["www.facebook.com","facebook.com"] #Enter the websites that you want to block
    if (current_time < Stop_time):
        with open(host_path,"r+") as file: #r+ is writing+ reading
            content = file.read()
            time.sleep(2)
            for website in website_list:    
                if website in content:
                    pass
                else:
                    file.write(f"{redirect} {website}\n")
                    print("DONE")
                    time.sleep(1)
            print("FOCUS MODE TURNED ON !!!!")


    while True:    
       
        current_time = datetime.datetime.now().strftime("%H:%M")
        website_list = ["www.facebook.com","facebook.com"]    #Enter the websites that you want to block
        if (current_time >= Stop_time):
            with open(host_path,"r+") as file:
                content = file.readlines()
                file.seek(0)

                for line in content:
                    if not any(website in line for website in website_list):
                        file.write(line)

                file.truncate()

                print("Websites are unblocked !!")
                file = open("focus.txt","a")
                file.write(f",{Focus_Time}")        #Write a 0 in focus.txt before starting
                file.close()
                break

else:
    ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)





2. FOCUS GRAPH FUNCTION :-


elif "show my focus" in query:
                    from FocusGraph import focus_graph
                    focus_graph()

Open a New File and Name it as FocusGraph.py and paste the following code: -


#pip install matplotlib
import matplotlib.pyplot as pt

def focus_graph():
    file = open("focus.txt","r")
    content = file.read()
    file.close()

    content = content.split(",")
    x1 = []
    for i in range(0,len(content)):
        content[i] = float(content[i])
        x1.append(i)

    print(content)
    y1 = content

    pt.plot(x1,y1,color = "red",marker = "o")
    pt.title("YOUR FOCUSED TIME",fontsize = 16)
    pt.xlabel("Times",fontsize = 14)
    pt.ylabel("Focus Time", fontsize = 14)
    pt.grid()
    pt.show()

Open a New File and Name it as "focus.txt" and write "0" in it: -


3. TRANSLATOR FUNCTION :-

elif "translate" in query:
                    from Translator import translategl
                    query = query.replace("jarvis","")
                    query = query.replace("translate","")
                    translategl(query)

Open a New File and Name it as Translator.py and paste the following code: -

from fnmatch import translate
from time import sleep
from googletrans import Translator
import googletrans #pip install googletrans
from gtts import gTTS
import googletrans
import pyttsx3
import speech_recognition
import os
from playsound import playsound
import time

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

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

def takeCommand():
    r = speech_recognition.Recognizer()
    with speech_recognition.Microphone() as source:
        print("Listening.....")
        r.pause_threshold = 1
        r.energy_threshold = 300
        audio = r.listen(source,0,4)

    try:
        print("Understanding..")
        query  = r.recognize_google(audio,language='en-in')
        print(f"You Said: {query}\n")
    except Exception as e:
        print("Say that again")
        return "None"
    return query

def translategl(query):
    speak("SURE SIR")
    print(googletrans.LANGUAGES)
    translator = Translator()
    speak("Choose the language in which you want to translate")
    b = input("To_Lang :- ")  
    text_to_translate = translator.translate(query,src = "auto",dest= b,)
    text = text_to_translate.text
    try :
        speakgl = gTTS(text=text, lang=b, slow= False)
        speakgl.save("voice.mp3")
        playsound("voice.mp3")
       
        time.sleep(5)
        os.remove("voice.mp3")
    except:
        print("Unable to translate")







THAT'S ALL

HOPE YOU LIKED IT !


No comments:

Post a Comment