-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockapp.py
More file actions
83 lines (35 loc) · 2.08 KB
/
blockapp.py
File metadata and controls
83 lines (35 loc) · 2.08 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
46
47
48
# Importing modules to be used
import time as t
from datetime import datetime as dt
# Creating the necessary instances for the various path and https.
host_temp = "hosts" # temp file to check if program works
host_path_mac = "\etc\hosts" # True path to the file running the program on mac[ absolute path]
host_path_windows = "" # True path to the file running the program on windows[ absolute path]
redirect = "127.0.0.1" # site to redirect the websites to if searched
site_list = ["facebook.com", "youtube.com", "gmail.com", "freecoursesite.com", "www.facebook.com", "www.youtube.com", "www.gmail.com", "www.freecoursesite.com",
"https://mail.google.com/mail/u/0/#inbox", "www.twitter.com", "twitter.com", "netflix.com", "www.netflix.com", "www.yahoo.com", "yahoo.com"] # lists of websites to block
# Creating a loop to run the neccesary commands
while True:
# calling a time range for working hours.
if dt(dt.now().year, dt.now().month, dt.now().day, 9, 30) < dt.now() < dt(dt.now().year, dt.now().month, dt.now().day, 16):
print("Working hours.....")
# opening the file from the file path, reading and performing operations on it
with open(host_temp, "r+") as file:
content = file.read()
# reading through the lines and appending the sites that are not in the file
for websites in site_list:
if websites in content:
pass
else:
file.write(redirect + " " + websites + "\n")
else: # Calls to time outside the working hours
with open(host_temp, "r+") as file:
contents = file.readlines() # Reading the lines in the file and not word by word
file.seek(0) # Places the cursor back to the start of the file
#
for lines in contents:
if not any(website in lines for website in site_list):
file.write(lines)
file.truncate() # Deletes all that prints after the first print
print("Time to sleep!")
t.sleep(20)