diff --git a/Website-Blocker/README.md b/Website-Blocker/README.md new file mode 100644 index 0000000..1bd9a8a --- /dev/null +++ b/Website-Blocker/README.md @@ -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) diff --git a/Website-Blocker/hosts b/Website-Blocker/hosts new file mode 100644 index 0000000..713a411 --- /dev/null +++ b/Website-Blocker/hosts @@ -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 diff --git a/Website-Blocker/website_blocker.py b/Website-Blocker/website_blocker.py new file mode 100644 index 0000000..90cb43b --- /dev/null +++ b/Website-Blocker/website_blocker.py @@ -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)