├── .idea ├── .gitignore ├── Website-blocker-python.iml ├── inspectionProfiles │ └── profiles_settings.xml ├── misc.xml ├── modules.xml └── vcs.xml ├── README.md ├── app v2.py ├── app.py ├── host_file_location.png ├── site_to_block.png └── time_to_block.png /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /.idea/Website-blocker-python.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Website Blocker Python 2 | 3 | 4 | Intro 5 | ------------ 6 | 7 | Hi Guys 👋 8 | 9 | This repository consists the code of a simple website blocker project implemented in Python. It can be used to block certain websites during work time to reduce distraction thus improving productivity. 10 | 11 | 12 | The magic 13 | -------------- 14 | 15 | The magic of this project lies on modifying the host file within your 16 | computer that manages how you access the web. 17 | 18 | Getting started 19 | --------------------- 20 | 21 | Well to get started with this project just clone the repository 22 | and edit the host file location depending on the OS your using. 23 | 24 | ```bash 25 | $-> git clone https://github.com/Kalebu/Website-blocker-python 26 | $-> cd Website-blocker-python 27 | $ Website-blocker-python -> 28 | ``` 29 | 30 | Adding sites to block + Editing host files 31 | ------------------------------------------------ 32 | 33 | Now open the *app.py* and then go to line 4 with variable *site_to_block* 34 | and you can add the sites you would like to block during work time 35 | 36 | 37 | the script will automaticaly detect your OS and will add the host records to the relevent location 38 | 39 | One more thing 40 | ------------------- 41 | 42 | You would need to set up the starting working + ending working hours where you would like to be restricted from accessing those websites. 43 | To do this go to line last line of our code and edit the hours where by. 44 | 45 | ![time_to_block](./time_to_block.png) 46 | 47 | Congratulations 48 | -------------------- 49 | 50 | Well done you now have a fully functioning website blocker that you have made yourself to improve your productivity, in Python 51 | 52 | Issues 53 | ----------- 54 | 55 | Incase you have any difficulties or issues while trying to run the script 56 | you can raise it on the issues 57 | 58 | Pull Requests 59 | ---------------- 60 | 61 | If you have something to add, I welcome pull requests that improve the project, your helpful contribution will be merged as soon as possible. 62 | 63 | 64 | Give it a Star ✴️ 65 | -------------------- 66 | If you find this repo useful , give it a star 67 | 68 | Credits 69 | ----------- 70 | All the credits to [kalebu](https://github.com/Kalebu) 71 | -------------------------------------------------------------------------------- /app v2.py: -------------------------------------------------------------------------------- 1 | import time 2 | import ctypes 3 | 4 | # Add the websites that you want to block to this list 5 | websites_to_block = ["www.facebook.com", "www.twitter.com", "www.instagram.com"] 6 | 7 | while True: 8 | current_time = time.localtime() 9 | # Set the time range in which the websites should be blocked 10 | if current_time.tm_hour >= 9 and current_time.tm_hour < 18: 11 | for website in websites_to_block: 12 | # Use the Windows hosts file to block the website 13 | # This will only work on Windows systems 14 | ctypes.windll.wininet.InternetSetOptionW(0, 39, ctypes.c_void_p(0), 0) 15 | with open(r"C:\Windows\System32\drivers\etc\hosts", "a") as file: 16 | file.write(f"127.0.0.1 {website}\n") 17 | else: 18 | # Remove the website block if it's outside the set time range 19 | with open(r"C:\Windows\System32\drivers\etc\hosts", "r+") as file: 20 | lines = file.readlines() 21 | file.seek(0) 22 | for line in lines: 23 | if not any(website in line for website in websites_to_block): 24 | file.write(line) 25 | file.truncate() 26 | # Set the time interval at which to check the current time 27 | time.sleep(60) 28 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | import time 2 | from datetime import datetime as dt 3 | import os 4 | 5 | # Enter the site name which you want to block 6 | sites_to_block = [ 7 | "www.facebook.com", 8 | "facebook.com", 9 | "www.youtube.com", 10 | "youtube.com", 11 | "www.gmail.com", 12 | "gmail.com", 13 | ] 14 | 15 | # different hosts for different os 16 | Linux_host = "/etc/hosts" 17 | Window_host = r"C:\Windows\System32\drivers\etc\hosts" 18 | default_hoster = Linux_host # if you are on windows then change it to Window_host 19 | redirect = "127.0.0.1" 20 | 21 | 22 | if os.name == 'posix': 23 | default_hoster = Linux_host 24 | 25 | elif os.name == 'nt': 26 | default_hoster = Window_host 27 | else: 28 | print("OS Unknown") 29 | exit() 30 | 31 | 32 | def block_websites(start_hour, end_hour): 33 | while True: 34 | try: 35 | if ( 36 | dt(dt.now().year, dt.now().month, dt.now().day, start_hour) 37 | < dt.now() 38 | < dt(dt.now().year, dt.now().month, dt.now().day, end_hour) 39 | ): 40 | print("Do the work ....") 41 | with open(default_hoster, "r+") as hostfile: 42 | hosts = hostfile.read() 43 | for site in sites_to_block: 44 | if site not in hosts: 45 | hostfile.write(redirect + " " + site + "\n") 46 | else: 47 | with open(default_hoster, "r+") as hostfile: 48 | hosts = hostfile.readlines() 49 | hostfile.seek(0) 50 | for host in hosts: 51 | if not any(site in host for site in sites_to_block): 52 | hostfile.write(host) 53 | hostfile.truncate() 54 | print("Good Time") 55 | time.sleep(3) 56 | except PermissionError as e: 57 | print(f"Caught a permission error: Try Running as Admin {e}") 58 | # handle the error here or exit the program gracefully 59 | break 60 | 61 | 62 | if __name__ == "__main__": 63 | block_websites(9, 21) 64 | -------------------------------------------------------------------------------- /host_file_location.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kalebu/Website-blocker-python/ef6b10ceb89ac3504c4d544e63c38ccf8097dc6a/host_file_location.png -------------------------------------------------------------------------------- /site_to_block.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kalebu/Website-blocker-python/ef6b10ceb89ac3504c4d544e63c38ccf8097dc6a/site_to_block.png -------------------------------------------------------------------------------- /time_to_block.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kalebu/Website-blocker-python/ef6b10ceb89ac3504c4d544e63c38ccf8097dc6a/time_to_block.png --------------------------------------------------------------------------------