Skip to content

Commit 2b2f351

Browse files
committed
Added WebsiteBlocker
1 parent 3bba4ab commit 2b2f351

File tree

5 files changed

+31
-41
lines changed

5 files changed

+31
-41
lines changed

Diff for: AutomationScripts/Website Blocker/README.md

+6-11
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22
## Introduction:
33
This is a script that aims to implement a website blocking utility for Windows-based systems. It makes use of the computer's hosts files and runs it as a background process, preventing access to the sites entered by the user in list format.
44
## Third-party libraries required:
5-
The project requires Python's datetime library only
6-
## Importing the Libraries:
7-
Open Command Prompt on your computer and type the following:
8-
On the script's console, type: <br>
9-
`import time` <br>
10-
`from datetime import datetime as dt`
5+
The project does not require any third party libraries
116

127
## Running the Script:
138
After opening the script in your Python IDE, execute the code so that you get the UI output window. Open your browser and try to visit the websites you blocked. When the script runs successfully, you will see `This site can't be reached` error or similar 404 error's on the browser.
@@ -20,10 +15,10 @@ After opening the script in your Python IDE, execute the code so that you get th
2015
2116
## Output:
2217
#### The output UI will appear as shown below:
23-
![Output 1](https://raw.githubusercontent.com/Rutuj-Runwal/Img/master/assets/UI1.png?token=AOFO32AFMK2ZEB3T4IKLGW3AZ6G6Q)
18+
![Output 1](./Images/UI1.png)
2419
#### You can add new site as shown below then click on "Add Website" button.You can add as many as you want:
25-
![Output 2](https://raw.githubusercontent.com/Rutuj-Runwal/Img/master/assets/UI2.png?token=AOFO32GHV3ZGY7QR27FDTA3AZ6HIE)
26-
#### If you entered anything that's not a proper website.An error willshow up to guide you:
27-
![Output 3](https://raw.githubusercontent.com/Rutuj-Runwal/Img/master/assets/UI3.png?token=AOFO32A2EMFCJ2WLIA7WCGTAZ6HNC)
20+
![Output 2](./Images/UI2.png)
21+
#### If you entered anything that's not a proper website. An error will show up to guide you:
22+
![Output 3](./Images/UI3.png)
2823

29-
#### Once you are done with adding websites, you canclick on Block-Sites button to begin blocking
24+
#### Once you are done with adding websites, you can click on Block-Sites button to begin blocking

Diff for: AutomationScripts/Website Blocker/Website-Blocker.py renamed to AutomationScripts/Website Blocker/website_blocker.py

+25-30
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,30 @@
77
from tkinter import *
88
from tkinter import messagebox
99

10+
#Create tkinter root/GUI
1011
root = Tk()
1112
root.title("Website Blocker by Rutuj Runwal")
1213
root.eval('tk::PlaceWindow %s center' % root.winfo_pathname(root.winfo_id()))
1314
root.geometry("600x300")
1415
ASADMIN = 'asadmin'
15-
# Host's Path
16+
# Host's Path - All the website info that is to be blocked is saved here
1617
hosts_path = r"C:\Windows\System32\drivers\etc\hosts"
17-
# # localhost's IP
18+
# localhost's IP
1819
redirect = "127.0.0.1"
1920
website_list = []
2021

2122

2223
def AddWebsite():
23-
site = Input.get()
24+
site = Input.get() # Get the site-input given by the user and check if it is
2425
print("You Entered: "+site)
2526
if("www" not in site or "." not in site):
2627
messagebox.showinfo(
27-
"Invalid Website", "Make sure the site is of this format 'www.google.com' OR 'google.com'")
28+
"Invalid Website", "Make sure the site is of this format 'www.google.com' `")
2829
else:
2930
website_list.append(site)
3031
print(website_list)
3132

32-
33+
# Function to check for admin access
3334
def is_admin():
3435
try:
3536
return ctypes.windll.shell32.IsUserAnAdmin()
@@ -39,9 +40,8 @@ def is_admin():
3940

4041
def Block():
4142
if is_admin():
42-
# Code of your program here
4343
while True:
44-
# time of your work
44+
# Time of your work
4545
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):
4646
print("Access Denied")
4747
with open(hosts_path, 'r+') as file:
@@ -50,7 +50,7 @@ def Block():
5050
if website in content:
5151
pass
5252
else:
53-
# mapping hostnames to your localhost IP address
53+
# Mapping hostnames to your localhost IP address
5454
file.write(redirect + " " + website + "\n")
5555
else:
5656
with open(hosts_path, 'r+') as file:
@@ -59,17 +59,17 @@ def Block():
5959
for line in content:
6060
if not any(website in line for website in website_list):
6161
file.write(line)
62-
# removing hostnmes from host file
62+
# Removing hostnames from host file
6363
file.truncate()
6464

6565
print("Access Granted")
6666
time.sleep(5)
6767
else:
68+
# Retry getting admin access to modify hosts file
6869
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)
6970
if is_admin():
70-
# Code of your program here
7171
while True:
72-
# time of your work
72+
# Time of your work
7373
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):
7474
print("Access Denied")
7575
with open(hosts_path, 'r+') as file:
@@ -87,29 +87,26 @@ def Block():
8787
for line in content:
8888
if not any(website in line for website in website_list):
8989
file.write(line)
90-
# removing hostnmes from host file
90+
# removing hostnames from host file
9191
file.truncate()
9292

9393
print("Access Granted")
9494
time.sleep(5)
95-
# except(Exception):
96-
# print("ERRROR!")
97-
9895

96+
# The BlockSites function is used to handle no. of sites to block
9997
def BlockSites():
10098
if(len(website_list) == 1):
101-
Val = messagebox.askquestion(
102-
"", "You are about to block 1 website.Are you sure you dont want to add more?")
103-
if Val == "yes":
99+
Val = messagebox.askquestion("", "You are about to block 1 website.Are you sure you dont want to add more?")
100+
if Val == "yes": #If user says "yes" , block the site provided
104101
print("Blocking One Site...")
105102
Block()
106103
else:
107-
messagebox.showinfo(
108-
"", "Add more sites and then press Block-Sites when you are ready")
104+
messagebox.showinfo("", "Add more sites and then press Block-Sites when you are ready")
109105
if(len(website_list) == 0):
110-
messagebox.showinfo("No sites to block",
111-
"Please add sites to block first!")
106+
#If the website_list is zero it means the user has not added any site to block
107+
messagebox.showinfo("No sites to block","Please add sites to block first!")
112108
else:
109+
#If the sites are >1 block them directly
113110
print("Ready to block "+str(len(website_list)) + " sites")
114111
Block()
115112

@@ -118,20 +115,18 @@ def BlockSites():
118115
script = os.path.abspath(sys.argv[0])
119116
params = ' '.join([script] + sys.argv[1:] + [ASADMIN])
120117
try:
121-
shell.ShellExecuteEx(
122-
lpVerb='runas', lpFile=sys.executable, lpParameters=params)
123-
messagebox.showinfo(
124-
"Welcome", "Welcome to Website Blocker v1.0.Add Websites to block,Once you are done click on Block-Sites to get started!")
118+
# Try running the code with admin access
119+
shell.ShellExecuteEx(lpVerb='runas', lpFile=sys.executable, lpParameters=params)
120+
messagebox.showinfo("Welcome", "Welcome to Website Blocker v1.0.Add Websites to block,Once you are done click on Block-Sites to get started!")
125121
MyLabel = Label(root, text="Enter Website to Block:")
126122
MyLabel.grid(row=0, column=0)
127123
Input = Entry(root, width=60, borderwidth=3)
128124
Input.insert(0, "Please REMOVE this text and enter website address.")
129125
Input.grid(row=0, column=2)
130-
MyBtn = Button(root, text="Add Website",
131-
command=AddWebsite).grid(row=1, column=2)
132-
MyBtn2 = Button(root, text="Block-Sites",
133-
command=BlockSites).grid(row=3, column=2)
126+
MyBtn = Button(root, text="Add Website",command=AddWebsite).grid(row=1, column=2)
127+
MyBtn2 = Button(root, text="Block-Sites",command=BlockSites).grid(row=3, column=2)
134128
root.mainloop()
135129
except(Exception):
130+
# Show an error when admin access is denied
136131
messagebox.showerror(
137132
"ERR:Admin Denied", "Admin rights are required for the script to work")

0 commit comments

Comments
 (0)