-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathnastools_generate.py
112 lines (101 loc) · 4.02 KB
/
nastools_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
import os
import json
from datetime import datetime
import base64
# 遍历文件夹,将json文件转为字典
def process_json_files(folder_path):
indexers = []
confs = {}
for filename in os.listdir(folder_path):
if filename.endswith(".json"):
filepath = os.path.join(folder_path, filename)
try:
with open(filepath, "r", encoding="utf-8") as f:
data = json.load(f)
if isinstance(data, dict):
indexer_data = {k: v for k, v in data.items() if k != "conf"}
indexers.append(indexer_data)
if "conf" in data:
domain = data["domain"].split("//")[-1].split("/")[0]
confs[domain] = data["conf"]
else:
print(f"Error: {filename} cannot be converted to a dictionary.")
except Exception as e:
print(f"Error reading {filename}: {str(e)}")
return indexers, confs
# 将数据保存为json文件
def save_data_to_json(data, json_path, json_pack_path):
version = datetime.now().strftime("%Y%m%d%H%M")
result = {
"version": version,
"indexer": data[0],
"conf": data[1]
}
with open(json_path, "w", encoding="utf-8") as f:
json.dump(result, f, ensure_ascii=True, indent=4)
with open(json_pack_path, "w", encoding="utf-8") as f:
json.dump(result, f, separators=(',', ':'), ensure_ascii=True)
# 将json文件转换为base64并保存到dat文件
def save_json_to_dat(json_path, bin_path):
with open(json_path, "r", encoding="utf-8") as f:
json_data = f.read()
base64_data = base64.b64encode(json_data.encode("utf-8")).decode("utf-8")
with open(bin_path, "w", encoding="utf-8") as f:
f.write(base64_data)
def format_json_file(file_path):
"""
格式化json文件
"""
with open(file_path, "r") as f:
try:
data = json.load(f)
except json.JSONDecodeError as e:
print(f"Error decoding JSON in {file_path}: {e}")
return
with open(file_path, "w") as f:
json.dump(data, f, indent=4)
def format_json_files_in_folder(folder_path):
"""
格式化sites目录下的json文件
"""
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
if os.path.isfile(file_path) and filename.lower().endswith(".json"):
format_json_file(file_path)
def create_or_clear_sites_file(sites_dat_path):
"""
创建或清空文件
"""
sites_dat_dir = os.path.dirname(sites_dat_path)
if not os.path.exists(sites_dat_dir):
os.makedirs(sites_dat_dir, exist_ok=True)
if os.path.exists(sites_dat_path):
with open(sites_dat_path, "w") as f:
f.truncate(0)
else:
with open(sites_dat_path, "w") as f:
pass
def convert_base64_to_json(input_file_path, output_file_path):
"""
旧的json转换方法
"""
with open(input_file_path, 'r') as input_file, open(output_file_path, 'w') as output_file:
line_number = 0
for line in input_file:
line_number += 1
try:
decoded_line = base64.b64decode(line.strip()).decode('utf-8')
json_data = json.loads(decoded_line)
json.dump(json_data, output_file)
output_file.write('\n')
except Exception as e:
print(f"Error on line {line_number}: {str(e)}")
if __name__ == "__main__":
format_json_files_in_folder("sites")
create_or_clear_sites_file("nas-tools/user.sites.bin")
create_or_clear_sites_file("nas-tools/user.sites.json")
create_or_clear_sites_file("nas-tools/user.sites.pack.json")
indexers, confs = process_json_files("sites")
data = (indexers, confs)
save_data_to_json(data, "nas-tools/user.sites.json", "nas-tools/user.sites.pack.json")
save_json_to_dat("nas-tools/user.sites.pack.json", "nas-tools/user.sites.bin")