-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsite_blocker.py
33 lines (31 loc) · 1.21 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
30
31
32
33
import time
import re
from datetime import datetime as dt
host_test = r"E:\IT Ebooks\code\web_blocker\hosts"
host_path = r"C:\Windows\System32\drivers\etc"
redirect = "127.0.0.1"
block_file = r"E:\IT Ebooks\code\web_blocker\block.txt"
with open(block_file,"r") as file:
website_list = [re.sub(r'https?://', '', web).strip().strip('/') if web.startswith('http') else web.strip() for web in file.readlines()]
# print(website_list)
while True:
if dt(dt.now().year, dt.now().month, dt.now().day, 8) < dt.now() < dt(dt.now().year, dt.now().month, dt.now().day, 16):
print("Working hour...")
with open(host_path, 'r+') as file:
content = file.read()
# print(content)
for website in website_list:
if website in content:
pass
else:
file.write(redirect + " " + website + "\n")
else:
with open(host_path, "r+") as file:
content = file.readlines()
file.seek(0)
for line in content:
if not any(website in line for website in website_list):
file.write(line)
file.truncate()
print("Relax time.")
time.sleep(5)