Added Website Blocker

This commit is contained in:
Ayush Bhardwaj 2018-10-03 00:04:07 +05:30
parent de4b70ab53
commit e5700bc8b0
3 changed files with 57 additions and 0 deletions

20
Website-Blocker/README.md Normal file
View File

@ -0,0 +1,20 @@
# Website Blocker using Python
This is a program which blocks certain distracting website like Facebook, Youtube etc during your work hours.
## Libraby Used
time (datetime is imported from python)
## Host Files
Host is an operating system file which maps hostnames to IP addresses.
Using python file handling manipulation I have changed hostnames on the hosts files for a certain interval of day when I'm working and need no distraction and deleted it then after when the time is over.
## Location of host file
### Host file on Mac and Linux :
$ /etc/hosts
### Host file on windows :
$ C:\Windows\System32\drivers\etc
## Note
* Windows user need to create a duplicate of OSs host file. Now provide the path of the duplicate file in hosts_path mentioned in the script.
* For scheduling above script in Linux you have to open crontab in your terminal as a root.(use sudo command)

9
Website-Blocker/hosts Normal file
View File

@ -0,0 +1,9 @@
127.0.0.1 localhost
127.0.1.1 hastagab-Latitude-6430U
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

View File

@ -0,0 +1,28 @@
import time
from datetime import datetime as dt
host_temp = "hosts" #host file copied to the directory for ease.
host_path = r"/etc/hosts" #original host file address in linux
redirect = "127.0.0.1"
website_list = ["www.facebook.com", "facebook.com"] #You can add your own list of websites
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): #You can choose your own working time period
print("working hours...")
with open(host_path,'r+') as file:
content=file.read()
for website in website_list:
if website in content:
pass
else:
file.write(redirect+" "+ website+"\n")
else:
with open(host_path,'r+') as file:
content=file.readlines()
file.seek(0)
for line in content:
if not any(website in line for website in website_list):
file.write(line)
file.truncate()
print("fun hours...")
time.sleep(10)