forked from felixonmars/dnsmasq-china-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_redundant.py
executable file
·45 lines (39 loc) · 1.24 KB
/
find_redundant.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
#!/usr/bin/env python3
''' Find accelerated-domains.china.conf for redundant item.
'''
LEAF = 1
def main():
with open('accelerated-domains.china.conf', 'r') as f:
lines = f.readlines()
# Parse conf file & prepare data structure
data = {}
for line in lines:
if line == '' or line.startswith('#'):
continue
domain = line.split('/')[1]
labels = domain.split('.')
labels.reverse()
data[domain] = labels
domains = list(data.keys())
domains.sort(key=lambda k: len(data[k]))
tree = {}
for domain in domains:
labels = data[domain]
node = tree # Init current node with root node
for i, label in enumerate(labels):
isLastLabel = i + 1 == len(labels)
# Check whether redundant
if (node == LEAF) or (isLastLabel and label in node):
print(f"Redundant found: {domain}")
break
# Create leaf node
if isLastLabel:
node[label] = LEAF
break
# Create branch node
if label not in node:
node[label] = {}
# Iterate to child node
node = node[label]
if __name__ == '__main__':
main()