-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
67 lines (50 loc) · 1.91 KB
/
Copy pathbuild.py
File metadata and controls
67 lines (50 loc) · 1.91 KB
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
import datetime
import os
import re
import sys
def update_blocklist(file_path):
if not os.path.isfile(file_path):
print(f"File '{file_path}' not found.")
return
header_lines = []
rules = set()
in_header = True
with open(file_path, 'r', encoding='utf-8') as f:
for line in f:
stripped = line.strip()
if in_header and (not stripped or stripped.startswith('!') or stripped.startswith('[')):
if stripped != "!":
header_lines.append(line)
else:
in_header = False
if stripped:
rules.add(stripped)
sorted_rules = sorted(list(rules))
entries_count = len(sorted_rules)
timezone_moscow = datetime.timezone(datetime.timedelta(hours=3))
now = datetime.datetime.now(timezone_moscow)
last_modified_str = now.strftime("%d %b %Y %H:%M GMT+3")
version_str = now.strftime("%Y%m%d.%H%M")
updated_header = []
for line in header_lines:
if line.startswith('! Version:'):
line = f"! Version: {version_str}\n"
elif line.startswith('! Last modified:'):
line = f"! Last modified: {last_modified_str}\n"
elif line.startswith('! Number of entries:'):
line = f"! Number of entries: {entries_count}\n"
updated_header.append(line)
with open(file_path, 'w', encoding='utf-8', newline='\n') as f:
f.writelines(updated_header)
f.write("!\n")
for rule in sorted_rules:
f.write(f"{rule}\n")
print(f"Blocklist '{file_path}' updated")
print(f"Rules: {entries_count}")
print(f"Version: {version_str}")
print(f"Last modified: {last_modified_str}")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Use: python build.py blocklist.txt")
sys.exit(1)
update_blocklist(sys.argv[1])