mirror of
https://github.com/hastagAB/Awesome-Python-Scripts.git
synced 2024-11-23 20:11:07 +00:00
Added Website Blocker
This commit is contained in:
parent
de4b70ab53
commit
e5700bc8b0
20
Website-Blocker/README.md
Normal file
20
Website-Blocker/README.md
Normal 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 OS’s 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
9
Website-Blocker/hosts
Normal 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
|
28
Website-Blocker/website_blocker.py
Normal file
28
Website-Blocker/website_blocker.py
Normal 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)
|
Loading…
Reference in New Issue
Block a user