mirror of
https://github.com/metafy-social/python-scripts.git
synced 2024-11-23 20:11:10 +00:00
🚀 Added Website Blocker
This commit is contained in:
parent
f0c04833b8
commit
ffe9bede31
22
scripts/Website Blocker/README.md
Normal file
22
scripts/Website Blocker/README.md
Normal 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)
|
7
scripts/Website Blocker/admin.py
Normal file
7
scripts/Website Blocker/admin.py
Normal 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()
|
BIN
scripts/Website Blocker/assets/img.png
Normal file
BIN
scripts/Website Blocker/assets/img.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 18 KiB |
BIN
scripts/Website Blocker/assets/img_1.png
Normal file
BIN
scripts/Website Blocker/assets/img_1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 16 KiB |
BIN
scripts/Website Blocker/assets/img_2.png
Normal file
BIN
scripts/Website Blocker/assets/img_2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 168 KiB |
BIN
scripts/Website Blocker/assets/img_3.png
Normal file
BIN
scripts/Website Blocker/assets/img_3.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 202 KiB |
42
scripts/Website Blocker/start_website_blocker.py
Normal file
42
scripts/Website Blocker/start_website_blocker.py
Normal 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()
|
Loading…
Reference in New Issue
Block a user