Skip to content

Commit 5ca558a

Browse files
authored
Merge pull request #142 from DeepNinja07x/DeepNinja07x-patch-2
Create website-blocker.py
2 parents 31e9d47 + 2bebe63 commit 5ca558a

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

Automation/website-blocker.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import time
2+
from datetime import datetime as dt
3+
4+
sites_to_block = [
5+
"www.facebook.com",
6+
"facebook.com",
7+
"www.youtube.com",
8+
"youtube.com",
9+
"www.gmail.com",
10+
"gmail.com",
11+
]
12+
13+
Linux_host = "/etc/hosts"
14+
Window_host = r"C:\Windows\System32\drivers\etc\hosts"
15+
default_hoster = Linux_host
16+
redirect = "127.0.0.1"
17+
18+
19+
def block_websites(start_hour, end_hour):
20+
while True:
21+
if (
22+
dt(dt.now().year, dt.now().month, dt.now().day, start_hour)
23+
< dt.now()
24+
< dt(dt.now().year, dt.now().month, dt.now().day, end_hour)
25+
):
26+
print("Do the work ....")
27+
with open(default_hoster, "r+") as hostfile:
28+
hosts = hostfile.read()
29+
for site in sites_to_block:
30+
if site not in hosts:
31+
hostfile.write(redirect + " " + site + "\n")
32+
else:
33+
with open(default_hoster, "r+") as hostfile:
34+
hosts = hostfile.readlines()
35+
hostfile.seek(0)
36+
for host in hosts:
37+
if not any(site in host for site in sites_to_block):
38+
hostfile.write(host)
39+
hostfile.truncate()
40+
print("Good Time")
41+
time.sleep(3)
42+
43+
44+
if __name__ == "__main__":
45+
block_websites(9, 21)

0 commit comments

Comments
 (0)