-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
66 lines (51 loc) · 1.81 KB
/
main.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
61
62
63
64
65
66
#!/usr/bin/env python3
"""
Block URLs by rerouting them to localhost through
the addition of rules in the system's hosts file.
"""
import platform
import re
def get_hosts_path():
"""Determine the path to the hosts based on the current OS."""
system_name = platform.system()
if system_name == "Windows":
return "C:\\Windows\\System32\\drivers\\etc\\hosts"
if system_name == "Linux":
return "/etc/hosts"
raise OSError("Unsupported OS!")
def prompt_for_number_of_pages():
"""Prompt for an integer"""
while True:
try:
return int(input("Number of pages: "))
except ValueError:
print("Incorrect input. Enter an integer!")
def prompt_for_url():
"""Prompt for a link and ensure it doesn't contain unnecessary prefix"""
url = input("Enter URL: ").strip()
return re.sub(r"^(?:https?://)?(?:www\.)?", "", url)
def get_all_possible_urls(urls):
"""Generate a list of URLs with common prefixes: 'www', 'http', 'https'"""
urls = urls + [("www." + url) for url in urls]
return (
urls
+ [("http://" + url) for url in urls]
+ [("https://" + url) for url in urls]
)
def append_to_hosts(urls, hosts_path):
"""Append URLs to the hosts file to block them."""
with open(hosts_path, "a", encoding="utf-8") as hosts_file:
hosts_file.write("\n")
for url in urls:
hosts_file.write("0.0.0.0" + "\t\t" + url + "\n")
print("URLs have been successfully blocked!")
def main():
"""Prompt for urls and append them to the hosts file to block them"""
hosts_path = get_hosts_path()
n = prompt_for_number_of_pages()
urls = []
for _ in range(n):
urls.append(prompt_for_url())
append_to_hosts(get_all_possible_urls(urls), hosts_path)
if __name__ == "__main__":
main()