import time
from datetime import datetime as dt
start_time = 8
end_time =18
def calculate_sleep_time():
if dt(dt.now().year, dt.now().month, dt.now().day, start_time) >= dt.now():
print("if block")
diff = dt(dt.now().year, dt.now().month, dt.now().day, start_time)-dt.now()
else:
print("else block")
diff = dt(dt.now().year, dt.now().month, dt.now().day, end_time) - dt.now()
print("utility will sleep for %d seconds" % diff.total_seconds())
return diff.total_seconds()
tmep_host = r'hosts'
hosts = r'/etc/hosts'
redirect = '127.0.0.1'
websites = ['www.facebook.com','facebook.com'] # list of website to block
while True:
if (dt(dt.now().year, dt.now().month, dt.now().day, start_time) < dt.now() < dt(dt.now().year, dt.now().month, dt.now().day, end_time)):
print("Working Hours")
with open(tmep_host,'r+') as file:
content = file.read()
for link in websites:
if link in content:
pass
else:
file.write(redirect + " " + link + "\n")
pass
pass
else:
print("Fun time")
with open(tmep_host,'r+') as file:
content = file.readlines()
file.seek(0)
for line in content:
if not any(website in line for website in websites):
file.write(line)
file.truncate()
time.sleep(calculate_sleep_time())
pass