Created
April 15, 2018 11:29
-
-
Save ns23/2d479b1338177f47f9a7110120ae6be2 to your computer and use it in GitHub Desktop.
A website blocker using python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment