Merge pull request #18 from Montekkundan/website-blocker

🚀 Added Website Blocker
This commit is contained in:
Advaita Saha 2022-10-01 01:02:26 +05:30 committed by GitHub
commit eeb13e8264
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 71 additions and 0 deletions

View File

@ -0,0 +1,22 @@
### About
A Python3 script to block certain websites.
### Setup
* Install Python3 from [here](https://python.org).
* Open cmd/terminal
* Navigate inside the ```scripts\Website Blocker``` directory.
* Run using Python
```bash
sudo python admin.py
```
Note: Add your end time in the `admin.py` file.
Blocking:
![img.png](assets/img.png)
![img_2.png](assets/img_2.png)
Unblocking:
![img_1.png](assets/img_1.png)
![img_3.png](assets/img_3.png)

View File

@ -0,0 +1,7 @@
from datetime import datetime
from start_website_blocker import Blocker
end_time = datetime(2021, 10, 1, 20) # y, m, d, h, min
sites_to_block = ['www.facebook.com', 'facebook.com']
block = Blocker(end_time, sites_to_block)
block.block_websites()

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 168 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 202 KiB

View File

@ -0,0 +1,42 @@
# a simple script to block websites
from datetime import datetime
import sys
if sys.platform == 'win32':
hosts_path = r"C:\Windows\System32\drivers\etc\hosts"
else:
hosts_path = '/private/etc/hosts'
redirect = "127.0.0.1"
class Blocker:
def __init__(self, time, block):
self.time = time
self.block = block
def block_websites(self):
if datetime.now() < self.time:
print("Block sites")
with open(hosts_path, 'r+') as hostfile:
hosts_content = hostfile.read()
for site in self.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 self.block):
hostfile.write(line)
hostfile.truncate()
# sudo python main.py
if __name__ == '__main__':
end_time = datetime(2021, 10, 1, 20) # y, m, d, h, min
sites_to_block = ['www.facebook.com', 'facebook.com']
block = Blocker(end_time, sites_to_block)
block.block_websites()