-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathapp.py
More file actions
59 lines (51 loc) · 1.9 KB
/
app.py
File metadata and controls
59 lines (51 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import time
from datetime import datetime as dt
import os
# ✅ Pull Request Change: Added logging and improved structure
# Enter the site name which you want to block
sites_to_block = [
"www.facebook.com",
"facebook.com",
"www.youtube.com",
"youtube.com",
"www.gmail.com",
"gmail.com",
]
# Different hosts for different OS
Linux_host = "/etc/hosts"
Window_host = r"C:\Windows\System32\drivers\etc\hosts"
redirect = "127.0.0.1"
# Detect OS and set appropriate host file path
if os.name == 'posix':
default_hoster = Linux_host
elif os.name == 'nt':
default_hoster = Window_host
else:
print("❌ OS Unknown")
exit()
def block_websites(start_hour, end_hour):
while True:
try:
current_time = dt.now()
if dt(current_time.year, current_time.month, current_time.day, start_hour) < current_time < dt(current_time.year, current_time.month, current_time.day, end_hour):
print("⛔ Blocking websites. Stay focused...")
with open(default_hoster, "r+") as hostfile:
content = hostfile.read()
for site in sites_to_block:
if site not in content:
hostfile.write(redirect + " " + site + "\n")
else:
print("✅ Outside working hours. Unblocking websites...")
with open(default_hoster, "r+") as hostfile:
lines = hostfile.readlines()
hostfile.seek(0)
for line in lines:
if not any(site in line for site in sites_to_block):
hostfile.write(line)
hostfile.truncate()
time.sleep(3)
except PermissionError as e:
print(f"⚠️ Caught a permission error: Try Running as Admin — {e}")
break
if __name__ == "__main__":
block_websites(9, 21)