-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsite-blocker.py
105 lines (93 loc) · 3.72 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Program Architecture
# Mac and Linux: /etc/hosts
# Windows: c:\windows\Systems32\drivers\etc
# for windows machines...
#hosts_path="c:\windows\Systems32\drivers\etc\hosts"
#hosts_path=r"c:\windows\Systems32\drivers\etc\hosts" #... using the r flag to tell python you are passing a real string not a /w special char...
# or use this alternate solution:
#hosts_path="c:\\windows\\Systems32\\drivers\\etc\\hosts" # using two back slashes...
# for mac and linux machines...
import time
from datetime import datetime as dt # using the namespace dt for a more concise reference name in the code...
hosts_paths= "/etc/hosts"
hosts_temp="hosts_copy.txt"
# redirects to hosts machine...
redirect="127.0.0.1"
# websites to block during specific times of the day...
website_list=["www.facebook.com","facebook.com", "www.twitter.com", "twitter.com"]
# datetime.datetime(2018, 7, 10, 15, 59, 21, 697959) date, hour, minutes, seconds, micro seconds...
# can break script with ctrl c for mac...
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): # checking for working hours...8am to 16:00(4:00pm)
print("Working Hours... ")
with open(hosts_temp, 'r+') as file: # r+ allows for reading and appending to a file... the w flag would erase previous data in the file...
content=file.read()
#print(content)
for website in website_list:
if website in content:
pass # this will make python skip to time.sleep(5) line of code...
else:
file.write(redirect +" "+ website+"\n") # write this into the hosts_copy.txt file...
else:
with open(hosts_temp, 'r+') as file:
content=file.readlines() # better than read() in this case cuz it will produce lines instead of just one stream of text...
# there is now a pointer at the end of the content variable...
file.seek(0) # pointer is set to first char of the first line in the content variable
for line in content:
if not any(website in line for website in website_list):
file.write(line) # rewrite the content in the host file line by line.
file.truncate() # truncates everything after last line previously read or written...thus truncating the websites...
print("Fun Hours... ")
time.sleep(5)
print("Five seconds have past... " + str(dt.now().second)) # <----TypeError: must be str, not int due to passing dt.now().second)
# fixed it with str(dt.now().second))...converted int to string with str()...
"""
File "app3-website-blocker.py", line 28, in <module>
print("Five seconds have past... " + dt.now().second)
TypeError: must be str, not int
"""
"""------------------------PYTHON SHELL OUTPUT-----------------------------"""
""" python interactive shell
OUTPUT:
>>> from datetime import datetime as dt
>>> dt.now()
datetime.datetime(2018, 7, 10, 15, 59, 21, 697959)
>>>
>>> dt.now() < (2016,5,5,8)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'datetime.datetime' and 'tuple'
>>>
"""
"""
OUTPUT:
>>> dt.now() < dt(2016,5,5,8)
False
>>>
"""
"""
OUTPUT:
>>> dt.now().year
2018
>>> type(dt.now().year)
<class 'int'>
>>>
"""
"""
OUTPUT:
Five seconds have past... 50
Fun Hours...
Five seconds have past... 55
Fun Hours...
Five seconds have past... 0
Fun Hours...
Five seconds have past... 5
Fun Hours...
Five seconds have past... 10
Fun Hours...
^CTraceback (most recent call last):
File "app3-website-blocker.py", line 27, in <module>
time.sleep(5)
KeyboardInterrupt <-----typed ctrl c...same for Windows...
Martins-iMac:mapping martinbatista$
"""