diff --git a/website_blocker/.gitignore b/website_blocker/.gitignore new file mode 100644 index 0000000..6ff331c --- /dev/null +++ b/website_blocker/.gitignore @@ -0,0 +1 @@ +hosts diff --git a/website_blocker/blocker.py b/website_blocker/blocker.py new file mode 100644 index 0000000..f007ccd --- /dev/null +++ b/website_blocker/blocker.py @@ -0,0 +1,37 @@ +import time +from datetime import datetime as dt + +hosts_path='./hosts' +website_list = ['www.facebook.com','facebook.com', 'mail.yahoo.com'] +REDIRECT_IP = '127.0.0.1' + +def is_working_hours(): + # return True + now = dt.now() + return dt(now.year,now.month, now.day, 8) < dt.now() and dt.now() < dt(now.year,now.month,now.day, 16) + +def update_hosts_for_working(): + with open(hosts_path, 'r+') as file: + content = file.read() + for website in website_list: + if website in content: + pass + else: + file.write(REDIRECT_IP + " " + website + "\n") + +def update_hosts_for_offhours(): + with open(hosts_path, 'r+') as file: + content = file.readlines() + file.seek(0) + for line in content: + # use of the 'any' keyword + if not any(website in line for website in website_list): + file.write(line) + file.truncate() + +while True: + if is_working_hours(): + update_hosts_for_working() + else: + update_hosts_for_offhours() + time.sleep(5)