From e855ba0d3263213a8754a9adba92a2809d66571f Mon Sep 17 00:00:00 2001 From: Volkan Aydogdu <119025324+itsvolkan@users.noreply.github.com> Date: Sat, 8 Jul 2023 04:21:04 -0400 Subject: [PATCH] Update app v2.py The code correctly imports the time module for time-related operations but it doesn't need to import the ctypes library since it's not used and Instead of opening the hosts file in append mode ("a"), we should open it in write mode ("w") to replace the existing content. This ensures that the file is correctly updated with the blocked websites. The code uses the current_time.tm_hour attribute to check the current hour. but this attribute represents the hour in 24-hour format. To check if the current hour is between 9 AM and 5 PM , we need to modify the condition to current_time.tm_hour >= 9 and current_time.tm_hour < 18. --- app v2.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/app v2.py b/app v2.py index 7055aa3..40bc8aa 100644 --- a/app v2.py +++ b/app v2.py @@ -1,5 +1,4 @@ import time -import ctypes # Add the websites that you want to block to this list websites_to_block = ["www.facebook.com", "www.twitter.com", "www.instagram.com"] @@ -8,17 +7,13 @@ current_time = time.localtime() # Set the time range in which the websites should be blocked if current_time.tm_hour >= 9 and current_time.tm_hour < 18: - for website in websites_to_block: - # Use the Windows hosts file to block the website - # This will only work on Windows systems - ctypes.windll.wininet.InternetSetOptionW(0, 39, ctypes.c_void_p(0), 0) - with open(r"C:\Windows\System32\drivers\etc\hosts", "a") as file: + with open(r"C:\Windows\System32\drivers\etc\hosts", "w") as file: + for website in websites_to_block: file.write(f"127.0.0.1 {website}\n") else: # Remove the website block if it's outside the set time range - with open(r"C:\Windows\System32\drivers\etc\hosts", "r+") as file: + with open(r"C:\Windows\System32\drivers\etc\hosts", "w+") as file: lines = file.readlines() - file.seek(0) for line in lines: if not any(website in line for website in websites_to_block): file.write(line)