Website Blocker
Become more productive! Block sites using a GUI app. Requires admin to run.JO00
python
4 months ago
8.8 kB
5
No Index
#blocker app
#lets be more productive
#this is a expanded upon script of patrick loebers python website blocker
#this script features ui, backups and easier buttons to create the block menu through GUI
#do checkout the original post as its quite useful: https://www.python-engineer.com/posts/website-blocker-in-python/
##imports all modules i need just about
import tkinter as tk
import math
from tkinter import *
from tkinter import filedialog
from tkinter import ttk
from tkinter import messagebox as mb
import time
import datetime
import os
import pathlib
import shutil
#setting up tkinters canvas for aligning stuff on the top
window = tk.Tk()
window.title("Productivity!")
canvas = tk.Canvas(window, width=330, height=20, bg="white")
top = Frame(window)
bottom = Frame(window)
top.pack(side=TOP)
top_2=Frame(window)
top_2.pack(side=TOP)
#labels for the first row
text_0=tk.Label(text="Keep this open to keep blocking sites for a day!")
text_1=tk.Label(text="Open the block list below to add sites")
#labels for second row about host file
text_2=tk.Label(text="Create a backup the first time you load the file")
text_block_number=tk.Label(text="You can block up to 0 sites")
#the color changing if its blocked green or unblocked red
rectangle = canvas.create_rectangle(0, 0, 335, 20, fill="red")
text_0.pack(in_=top)
#parameters to pass on such as default sites or time saved
default_block=["0","Blocked List Below, write below the URL for what you need","www.facebook.com","www.vinted.co.uk","www.spotify.com","www.youtube.com","facebook.com","spotify.com","youtube.com","youtube.co.uk","www.youtube.co.uk","www.artstation.com","www.linkedin.com"]
block_list_data=[]
site_block_number=0
block_time_start=time.time()
blocking=False
#file path for where hosts file is works only on windows
hosts_path = 'C:\Windows\System32\drivers\etc\hosts'
redirect = "127.0.0.1"
#checks if the block list exists = if not makes new one from where this py file is being ran
def checking_txt_exist():
print("Checking for block_text")
thepath=pathlib.Path(__file__).parent.resolve()
block_list_data=[]
textfile_check=thepath.joinpath('block_list.txt')
if textfile_check.is_file():
print("Found it")
read_now()
else:
print("Not found, creating")
with open(textfile_check, "w", encoding="utf-8") as file:
for line in default_block:
line=line+"\n"
file.write(line)
#print("written")
read_now()
#grabs the list and reads what needs to be blocked from it, structure must be followed blocked sites begin from row 2
def read_now():
global site_block_number
site_block_number=0
thepath=pathlib.Path(__file__).parent.resolve()
global block_list_data
block_list_data=[]
textfile_check=thepath.joinpath('block_list.txt')
with open(textfile_check, "r+") as file:
lines = file.readlines()
lines_length=len(lines)
#print("there is this many lines "+str(lines_length)+" many")
for i in range(lines_length):
if i >1:
#print(lines[i])
the_site=lines[i]
site_block_number+=1
block_list_data.append(the_site[:-1])
#print(thepath)
print("Now printing the block list")
print(block_list_data)
#takes the data from the list and blocks it line by line by adding it to host file
def block_sites():
with open(hosts_path, 'r+') as hostfile:
hosts_content = hostfile.read()
for site in block_list_data:
if site not in hosts_content:
print("Blocking site: " +str(site))
hostfile.write(redirect + ' ' + site + '\n')
#print("blocked all complete")
global block_time_start
block_time_start = time.time()
global blocking
blocking=True
#does the reverse, seeks the data and removes it from the bottom of the block list
def unblock_sites():
with open(hosts_path, 'r+') as hostfile:
lines = hostfile.readlines()
hostfile.seek(0)
for line in lines:
if not any(site in line for site in block_list_data):
#print("Unblocking sites")
#print("unblocking this site: "+str(line))
hostfile.write(line)
hostfile.truncate()
print("Unblocked sites")
saving_minutes()
#print("unblocked")
#####
######################
######the button events
######
########################
############
def block():
#print("blocked hoohoo")
checking_txt_exist()
block_sites()
rectangle=canvas.create_rectangle(0, 0, 335, 20, fill="green")
UI_Update()
#rectangle.config(fill="green")
def unblock():
#print("unblocked nows")
checking_txt_exist()
unblock_sites()
rectangle=canvas.create_rectangle(0, 0, 335, 20, fill="red")
UI_Update()
#rectangle.config(fill="red")
def UI_Update():
#print("i will update here")
site_ui_text="You're able to block "+str(site_block_number)+" sites"
text_block_number.config(text=site_ui_text)
def Find_list():
thepath=pathlib.Path(__file__).parent.resolve()
os.startfile(thepath)
def Find_Host():
fix_path_fold=hosts_path[:-6]
#print(str(fix_path_fold))
os.startfile(fix_path_fold)
def Backup_Host():
#print("backing up host file")
thepath=pathlib.Path(__file__).parent.resolve()
backup_location=thepath.joinpath('backup_host_file')
if not os.path.exists(backup_location):
os.makedirs(backup_location)
else:
print("folder already exists")
file_backup=backup_location.joinpath('host')
if not os.path.isfile(file_backup):
shutil.copyfile(hosts_path, file_backup)
else:
question=mb.askyesno("askyesno", "Backup already found, overwrite?")
if question:
shutil.copyfile(hosts_path, file_backup)
print("Overwritten")
else:
print("Not overwritten")
def Restoring_Host():
#print("restoring from backup")
try:
thepath=pathlib.Path(__file__).parent.resolve()
backup_location=thepath.joinpath('backup_host_file')
file_backup=backup_location.joinpath('host')
shutil.copyfile(file_backup, hosts_path)
except:
print("failed to restore")
def saving_minutes():
global blocking
#print("trying save mins")
if blocking==True:
#print("blocking doing calc")
global block_time_start
now = time.time()
runtime=round((now - block_time_start), 2)
#print(runtime)
mins_block=round((runtime/60), 2)
#print("blocking for", mins_block, "minutes")
blocking=False
#now to write the minutes to file
thepath=pathlib.Path(__file__).parent.resolve()
global block_list_data
block_list_data=[]
textfile_check=thepath.joinpath('block_list.txt')
with open(textfile_check, "r+") as file:
lines = file.readlines()
count_old = lines[0]
new_count = float(count_old) + mins_block
lines[0] = str(new_count) + "\n"
file.seek(0)
file.writelines(lines)
file.truncate()
print("You've saved "+str(new_count)+" minutes by blocking sites!")
else:
print("Blocking has not been done so cannot add anymore minutes")
##################
#the buttons
button_block = tk.Button(text="Block", command=block)
button_unblock = tk.Button(text="Unblock", command=unblock)
button_get_list=tk.Button(text="Find List", command=Find_list)
button_save_mins_2=tk.Button(text="Save Blocked Mins", command=saving_minutes)
button_find_host=tk.Button(text="Find Host", command=Find_Host)
button_save_mins=tk.Button(text="Backup Host", command=Backup_Host)
button_restore_host=tk.Button(text="Restore Host", command=Restoring_Host)
##aligning the buttonsto canvas from top via packing theres two rows so two top canvases
button_block.pack(in_=top, side=LEFT)
button_unblock.pack(in_=top, side=LEFT)
button_get_list.pack(in_=top, side=LEFT)
button_save_mins_2.pack(in_=top, side=LEFT)
text_2.pack(in_=top_2)
button_find_host.pack(in_=top_2, side=LEFT)
button_save_mins.pack(in_=top_2, side=LEFT)
button_restore_host.pack(in_=top_2, side=LEFT)
text_block_number.pack()
canvas.pack()
#starts loop
#it goes through the check first to make sure there is a block list
checking_txt_exist()
read_now()
UI_Update()
window.mainloop()
Editor is loading...
Leave a Comment