-
-
Notifications
You must be signed in to change notification settings - Fork 506
Expand file tree
/
Copy pathweb_blocker.py
More file actions
45 lines (33 loc) · 1.07 KB
/
web_blocker.py
File metadata and controls
45 lines (33 loc) · 1.07 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
import time
import platform
from datetime import datetime as dt
# Detect OS and set hosts path
if platform.system() == "Windows":
hosts_path = r"C:\Windows\System32\drivers\etc\hosts"
else:
hosts_path = "/etc/hosts"
redirect = "127.0.0.1"
website_list = [
"www.facebook.com", "facebook.com"]
START_HOUR = 8
END_HOUR = 16
while True:
now = dt.now()
start = dt(now.year, now.month, now.day, START_HOUR)
end = dt(now.year, now.month, now.day, END_HOUR)
if start < now < end:
print("Working hours...")
with open(hosts_path, "r+") as file:
content = file.read()
for website in website_list:
if website not in content:
file.write(f"{redirect} {website}\n")
else:
with open(hosts_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()
time.sleep(3600) # Check every hour