Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update app v2.py #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions app v2.py
Original file line number Diff line number Diff line change
@@ -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"]
Expand All @@ -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)
Expand Down