-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerator.py
130 lines (120 loc) · 3.33 KB
/
generator.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
import re
import yaml
def level_region(node):
LEVELS = {
"香港": 9,
"新加坡": 8,
"日本": 7,
"美国": 6,
"台湾": 5,
"韩国": 4,
"俄罗斯": 3,
"德国": 2,
"英国": 1,
}
info = node if isinstance(node, str) else node["name"]
for l in LEVELS:
if re.search(l, info) is not None:
return LEVELS[l]
return 0
def level_type(node):
LEVELS = {
" IEPL.*HA": 6,
" IEPL.*(AC|Ma)": 5,
" IEPL.*(Ul|Pl)": 4,
" IEPL.*Pr": 3,
" IEPL.*St": 2,
" IEPL.*Ai": 1,
}
info = node if isinstance(node, str) else node["name"]
for l in LEVELS:
if re.search(l, info) is not None:
return LEVELS[l]
return 0
FILTERS = {
"__AISuite__": ".*",
"__Apple__": ".*",
"__Blizzard__": ".*",
"__Crack__": ".*",
"__Crypto__": ".*",
"__Discord__": ".*",
"__Disney__": ".*",
"__Domestic__": ".*",
"__DomesticTV__": ".*",
"__Douyin__": ".*",
"__Downloader__": ".*",
"__Drive__": ".*",
"__GlobalTV__": ".*",
"__Microsoft__": ".*",
"__miHoYo__": ".*",
"__Netflix__": ".*",
"__Others__": ".*",
"__PayPal__": ".*",
"__Proxy__": ".*",
"__PT__": ".*",
"__Scholar__": ".*",
"__Spam__": ".*",
"__Special__": ".*",
"__Speedtest__": ".*",
"__Spotify__": ".*",
"__SSH__": ".*",
"__Steam__": ".*",
"__Telegram__": ".*",
"__TopBlocked__": ".*",
"__YouTube__": ".*",
}
with open("clash.list", "r", encoding="utf-8") as f:
clash_list = yaml.load(f, yaml.CFullLoader)
clash_list["proxies"].sort(key=level_region, reverse=True)
clash_list["proxies"].sort(key=level_type, reverse=True)
clash_nodes = (
yaml.dump(
clash_list,
Dumper=yaml.CDumper,
default_flow_style=False,
allow_unicode=True,
line_break="\n",
encoding="utf-8",
sort_keys=False,
)
.decode("utf-8")
.rstrip()
)
clash_remarks = {}
for f in FILTERS:
clash_remarks[f] = ""
for p in clash_list["proxies"]:
if re.search(FILTERS[f], p["name"]) is not None:
clash_remarks[f] += " - " + p["name"] + "\n"
clash_remarks[f] = clash_remarks[f].rstrip()
with open("Dler.yaml", "r+", encoding="utf-8", newline="\n") as f:
clash_conf = f.read()
clash_conf = clash_conf.replace("__nodes__", clash_nodes)
for r in clash_remarks:
clash_conf = clash_conf.replace(r, clash_remarks[r])
f.seek(0)
f.truncate()
f.write(clash_conf)
with open("surge.list", "r", encoding="utf-8") as f:
surge_list = f.readlines()
surge_list.sort(key=level_region, reverse=True)
surge_list.sort(key=level_type, reverse=True)
surge_nodes = ""
for l in surge_list:
surge_nodes += l
surge_nodes = surge_nodes.replace(", ", ",").rstrip()
surge_remarks = {}
for f in FILTERS:
surge_remarks[f] = ""
for l in surge_list:
if re.search(FILTERS[f], l) is not None:
surge_remarks[f] += "," + re.match(r"^(.*?) =", l).group(1)
with open("Dler.conf", "r+", encoding="utf-8", newline="\n") as f:
surge_conf = f.read()
surge_conf = surge_conf.replace("__nodes__", surge_nodes)
for r in surge_remarks:
surge_conf = surge_conf.replace(r, surge_remarks[r])
f.seek(0)
f.truncate()
f.write(surge_conf)