-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate.py
114 lines (91 loc) · 2.99 KB
/
generate.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import os
import requests
import re
from joblib import Parallel, delayed
from src.config.lists import categories
def download_list(url, path):
response = requests.get(url)
with open(path, "w", encoding="utf-8") as f:
if response.status_code != 200:
print("Downloading returned status code: " + response.status_code + " for list: " + path)
f.write(response.text)
downloads_list = []
for category in categories.keys():
try:
os.mkdir(category)
except FileExistsError:
pass
for i in categories[category]:
downloads_list.append([i["url"], category + "/" + i["name"] + ".txt"])
Parallel(n_jobs=16)(delayed(download_list)(i[0], i[1]) for i in downloads_list)
with open("allowlist.txt", "r", encoding="utf-8") as f:
ALLOWLIST = f.read().splitlines()
def convert_pihole_list(list):
pihole_list = []
for line in list:
line = line.replace("^", "")
if "||" in line and not "*" in line and not "@@" in line:
line = line.replace("||", "")
if "|" in line:
continue
pihole_list.append(line)
return pihole_list
def convert_line(line):
# if any(x in line for x in ALLOWLIST):
# return ""
line = line.replace("0.0.0.0 ", "").replace("127.0.0.1 ", "")
line = line.replace("^", "")
line = line.split("$")[0]
line += "^"
if line.startswith("||") or line.startswith("@@"):
return line
else:
return "||" + line
def skip_line(line):
line = line.replace("||", "").replace("@@", "").replace("^", "")
if any(x in line for x in ALLOWLIST):
return True
if line == "":
return True
forbidden_symbols = [" ", "#", "!", "/", "$"]
if any(x in line for x in forbidden_symbols):
return True
return False
def is_valid_string(line):
s = line.replace("|", "").replace("@@", "").replace("*", "").replace("^", "")
return re.fullmatch(r'^[-.\w]+$', s) is not None
def convert_list(path):
try:
with open(path, "r", encoding="utf-8") as f:
list = []
for line in f.read().splitlines():
line = convert_line(line)
if skip_line(line):
continue
if not is_valid_string(line):
print(line)
list.append(line)
except FileNotFoundError:
print(f"File not found: {path}")
return []
return list
def write_lists_to_file(filtered_out, adguard_out, pihole_out, path):
with open(path + ".txt", "w", encoding="utf-8") as f:
f.write("\n".join(filtered_out))
with open(path + ".adguard", "w", encoding="utf-8") as f:
f.write("\n".join(adguard_out))
with open(path + ".pihole", "w", encoding="utf-8") as f:
f.write("\n".join(pihole_out))
adguard_list = []
pihole_list = []
for category in categories.keys():
adguard_category_list = []
pihole_category_list = []
for i in categories[category]:
converted_list = convert_list(i["txt"])
adguard_category_list += converted_list
pihole_category_list += convert_pihole_list(converted_list)
write_lists_to_file(pihole_category_list, adguard_category_list, pihole_category_list, category)
adguard_list += adguard_category_list
pihole_list += pihole_category_list
write_lists_to_file(pihole_list, adguard_list, pihole_list, "list")