-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.py
60 lines (49 loc) · 1.93 KB
/
block.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import json
from tkinter import messagebox
import platform
class WebsiteBlocker:
def __init__(self):
self.hosts_path = self.get_hosts_path()
self.ip_address = "127.0.0.1"
self.file_path = "data/sites.json"
def block_websites(self):
websites = self.load_websites_from_json()
for website in websites:
self.block(website)
def unblock_websites(self):
websites = self.load_websites_from_json()
for website in websites:
self.unblock(website)
def block(self, website):
# Open the hosts file in append mode
with open(self.hosts_path, "a") as file:
file.write(f"{self.ip_address} {website}\n")
print(f"{website} blocked!")
def unblock(self, website):
# Read the hosts file into memory
with open(self.hosts_path, "r") as file:
lines = file.readlines()
# Remove the blocked website entry
with open(self.hosts_path, "w") as file:
for line in lines:
if not line.startswith(f"{self.ip_address} {website}"):
file.write(line)
print(f"{website} unblocked!")
def get_hosts_path(self):
system_name = platform.system()
if system_name == "Linux" or system_name == "Darwin":
return "/etc/hosts"
elif system_name == "Windows":
return r"C:\Windows\System32\drivers\etc\hosts"
else:
raise OSError("Unsupported operating system")
def load_websites_from_json(self):
try:
with open(self.file_path, "r") as file:
data = json.load(file)
return data
except FileNotFoundError:
raise Exception("JSON file not found.")
except json.JSONDecodeError:
messagebox.showerror("Error", "Error decoding JSON file.")
return None