Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #by Dhruv Kothari, Aidan Lee, Shivani Dudyala, Tanisha Umbarkar
- # a simple script to block websites
- from datetime import datetime
- curMin = datetime.now().minute
- curHour = datetime.now().hour
- curDay = datetime.now().day
- curMonth = datetime.now().month
- curYear = datetime.now().year
- print("how many days do you want")
- days = int(input())
- print("how many hours do you want")
- hours = int(input())
- print("how many minutes do you want")
- minutes = int(input())
- end_time = datetime(curYear, curMonth, curDay + days, curHour + hours, curMin + minutes) # y, m, d, h, min
- sites_to_block = ['xfinity.com', 'www.xfinity.com', 'youtube.com', 'www.youtube.com', 'instagram.com', 'www.instagram.com', 'https://my.xfinity.com', 'www.my.xfinity.com', 'my.xfinity.com']
- hosts_path = '/etc/hosts'
- # r"C:\Windows\System32\drivers\etc\hosts"
- redirect = "127.0.0.1"
- def block_websites():
- while datetime.now() < end_time:
- with open(hosts_path, 'r+') as hostfile:
- hosts_content = hostfile.read()
- for site in sites_to_block:
- if site not in hosts_content:
- hostfile.write(redirect + ' ' + site + '\n')
- else:
- print('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 sites_to_block):
- hostfile.write(line)
- hostfile.truncate()
- # sudo python main.py
- if __name__ == '__main__':
- block_websites()
- # 1. Trigger script manually every now and then
- # 2. Cron job
- # 3. scipt running in background with while True
- # while True:
- # block_websites()
- # time.sleep(5)
Add Comment
Please, Sign In to add comment