From fd81b57ee2b46545d023999aec131463e854aab1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Gaspar?= Date: Sun, 5 Apr 2026 17:52:08 +0100 Subject: [PATCH] Fix: improve web blocker platform compatibility --- Website Blocker/{read.me => README.md} | 17 ++++--- Website Blocker/cross-platform/web_blocker.py | 45 +++++++++++++++++++ 2 files changed, 56 insertions(+), 6 deletions(-) rename Website Blocker/{read.me => README.md} (51%) create mode 100644 Website Blocker/cross-platform/web_blocker.py diff --git a/Website Blocker/read.me b/Website Blocker/README.md similarity index 51% rename from Website Blocker/read.me rename to Website Blocker/README.md index a782bcc1..cf2197a1 100644 --- a/Website Blocker/read.me +++ b/Website Blocker/README.md @@ -1,12 +1,17 @@ +# Description This is real world program which blocks certain distracting website like Facebook, Youtube etc during your work hours. About the program : What we are going to in this program is that we will pass the link of websites which you think is distracting and the time that you are working on your computer and program will block those website. -Program Architecture: - for mac:- -1. Every system have host file whether it is Mac, Windows or Linux. -Host file in Mac and Linux : "/etc/hosts"\ +>Program Architecture: +Every system have host file whether it is Mac, Windows or Linux. + 1. for mac: Host file in Mac and Linux : "/etc/hosts"\ +2. for windows:- +C:\Windows\System32\drivers\etc\hosts + +## Usage +Insert working hours, and blacklisted websties in the script. The run: + +> python web_blocker.py -for windows:- -C:\Windows\System32\drivers\etc diff --git a/Website Blocker/cross-platform/web_blocker.py b/Website Blocker/cross-platform/web_blocker.py new file mode 100644 index 00000000..33ac918c --- /dev/null +++ b/Website Blocker/cross-platform/web_blocker.py @@ -0,0 +1,45 @@ +import time +import platform +from datetime import datetime as dt + +# Detect OS and set hosts path +if platform.system() == "Windows": + hosts_path = r"C:\Windows\System32\drivers\etc\hosts" +else: + hosts_path = "/etc/hosts" + +redirect = "127.0.0.1" + +website_list = [ + "www.facebook.com", "facebook.com"] + +START_HOUR = 8 +END_HOUR = 16 + +while True: + now = dt.now() + start = dt(now.year, now.month, now.day, START_HOUR) + end = dt(now.year, now.month, now.day, END_HOUR) + + if start < now < end: + print("Working hours...") + + with open(hosts_path, "r+") as file: + content = file.read() + + for website in website_list: + if website not in content: + file.write(f"{redirect} {website}\n") + + else: + with open(hosts_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() + + time.sleep(3600) # Check every hour \ No newline at end of file