From 9c8f79fbe379ca2352f9030e1ee6bb25bf41cdb6 Mon Sep 17 00:00:00 2001 From: Parag Date: Sun, 23 Oct 2022 17:43:06 +0530 Subject: [PATCH] New python project added --- python-codes/Website Blocker.py | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 python-codes/Website Blocker.py diff --git a/python-codes/Website Blocker.py b/python-codes/Website Blocker.py new file mode 100644 index 0000000..f0b8dc7 --- /dev/null +++ b/python-codes/Website Blocker.py @@ -0,0 +1,45 @@ +# """This is a simple website blocker project implemented in Python, It can be used to block certain websites during working time to reduce distraction thus improving productivity""" + + +import time +from datetime import datetime as dt + +sites_to_block = [ + "www.facebook.com", + "facebook.com", + "www.youtube.com", + "youtube.com", + "www.gmail.com", + "gmail.com", +] + +Linux_host = "/etc/hosts" +Window_host = r"C:\Windows\System32\drivers\etc\hosts" +default_hoster = Window_host +redirect = "127.0.0.1" + + +def block_websites(start_hour, end_hour): + while True: + if (dt(dt.now().year, dt.now().month, dt.now().day, start_hour) < dt.now() < dt(dt.now().year, dt.now().month, dt.now().day, end_hour)): + print("Do the work ....") + with open(default_hoster, "r+") as hostfile: + hosts = hostfile.read() + for site in sites_to_block: + if site not in hosts: + hostfile.write(redirect + " " + site + "\n") + else: + with open(default_hoster, "r+") as hostfile: + hosts = hostfile.readlines() + hostfile.seek(0) + for host in hosts: + if not any(site in host for site in sites_to_block): + hostfile.write(host) + hostfile.truncate() + print("Good Time") + time.sleep(3) + + +if __name__ == "__main__": + # block_websites(start_working_time, end_working_time) + block_websites(11, 2) \ No newline at end of file