tryhardyoninfinityhz

WebBlocker.py

Mar 5th, 2022 (edited)
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.75 KB | None | 0 0
  1. #by Dhruv Kothari, Aidan Lee, Shivani Dudyala, Tanisha Umbarkar
  2. # a simple script to block websites
  3. from datetime import datetime
  4. curMin = datetime.now().minute
  5. curHour = datetime.now().hour
  6. curDay = datetime.now().day
  7. curMonth = datetime.now().month
  8. curYear = datetime.now().year
  9. print("how many days do you want")
  10. days = int(input())
  11. print("how many hours do you want")
  12. hours = int(input())
  13. print("how many minutes do you want")
  14. minutes = int(input())
  15. end_time = datetime(curYear, curMonth, curDay + days, curHour + hours, curMin + minutes)  # y, m, d, h, min
  16.  
  17. 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']
  18.  
  19. hosts_path = '/etc/hosts'
  20. # r"C:\Windows\System32\drivers\etc\hosts"
  21. redirect = "127.0.0.1"
  22. def block_websites():
  23.     while datetime.now() < end_time:
  24.         with open(hosts_path, 'r+') as hostfile:
  25.             hosts_content = hostfile.read()
  26.             for site in sites_to_block:
  27.                 if site not in hosts_content:
  28.                     hostfile.write(redirect + ' ' + site + '\n')
  29.     else:
  30.         print('Unblock sites')
  31.         with open(hosts_path, 'r+') as hostfile:
  32.             lines = hostfile.readlines()
  33.             hostfile.seek(0)
  34.             for line in lines:
  35.                 if not any(site in line for site in sites_to_block):
  36.                     hostfile.write(line)
  37.             hostfile.truncate()
  38.  
  39.  
  40. # sudo python main.py
  41. if __name__ == '__main__':
  42.     block_websites()
  43.  
  44.     # 1. Trigger script manually every now and then
  45.     # 2. Cron job
  46.     # 3. scipt running in background with while True
  47.     # while True:
  48.     #    block_websites()
  49.     #    time.sleep(5)
Add Comment
Please, Sign In to add comment