-
Notifications
You must be signed in to change notification settings - Fork 11
/
website_blocker.py
29 lines (28 loc) · 1.24 KB
/
website_blocker.py
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
import time
from datetime import datetime as dt
hosts_path = r"/etc/hosts" # r is for raw string
hosts_temp = "hosts"
redirect = "127.0.0.1"
web_sites_list = ["www.facebook.com", "facebook.com"] # users can modify the list of the websites they want to block
while True:
if dt(dt.now().year, dt.now().month, dt.now().day, 9) < dt.now() < dt(dt.now().year, dt.now().month, dt.now().day,22):
print("Working hours")
with open(hosts_path, "r+") as file:
content = file.read()
for website in web_sites_list:
if website in content:
pass
else:
file.write(redirect+" "+website+"\n")
else:
print("Fun time")
with open(hosts_path, "r+") as file:
content = file.readlines()
file.seek(0) # reset the pointer to the top of the text file
for line in content:
# here comes the tricky line, basically we overwrite the whole file
if not any(website in line for website in web_sites_list):
file.write(line)
# do nothing otherwise
file.truncate() # this line is used to delete the trailing lines (that contain DNS)
time.sleep(5)