forked from felixonmars/dnsmasq-china-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updater.py
executable file
·40 lines (33 loc) · 1.14 KB
/
updater.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
#!/usr/bin/env python
from __future__ import unicode_literals
from argparse import ArgumentParser
if __name__ == "__main__":
parser = ArgumentParser(description="dnsmasq-china-list updater")
parser.add_argument(
'-a', '--add',
metavar="DOMAIN",
nargs="+",
help='Add one or more new domain(s) (implies -s)',
)
parser.add_argument(
'-s', '--sort',
action='store_true',
default=True,
help='Sort the list (default action)',
)
options = parser.parse_args()
with open("accelerated-domains.china.conf") as f:
lines = list(f)
if options.add:
options.sort = True
for domain in options.add:
new_line = "server=/%s/114.114.114.114\n" % domain
if new_line in lines:
print("Domain already exists: " + domain)
else:
print("New domain added: " + domain)
lines.append(new_line)
if options.sort:
lines.sort(key=lambda x: x.lstrip("#"))
with open("accelerated-domains.china.conf", "w") as f:
f.write(''.join(filter(lambda line: line.strip(), lines)))