update
This commit is contained in:
83
fix_json.py
Normal file
83
fix_json.py
Normal file
@@ -0,0 +1,83 @@
|
||||
import re
|
||||
|
||||
with open('/root/dns/blocked-services-rules.json', 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
|
||||
# 从第230行开始,格式有问题
|
||||
# 正确的格式应该是:
|
||||
# "ServiceName": {
|
||||
# "ServiceID": "...",
|
||||
# "Name": "...",
|
||||
# ...
|
||||
# }
|
||||
|
||||
# 但是现在的格式是:
|
||||
# "ServiceID": "shopping",
|
||||
# "Amazon Streaming": { "ServiceID": "amazon_streaming",
|
||||
# "Name": "Amazon Streaming",
|
||||
# ...
|
||||
|
||||
# 需要删除多余的 "ServiceID": "shopping", 这一行
|
||||
# 并修复 "Amazon Streaming": { "ServiceID": "amazon_streaming", 这一行
|
||||
|
||||
lines = content.split('\n')
|
||||
|
||||
# 读取前230行(第1-229行,索引0-228)
|
||||
fixed_lines = lines[:229]
|
||||
|
||||
i = 229 # 从第230行开始(索引229)
|
||||
while i < len(lines):
|
||||
line = lines[i]
|
||||
|
||||
# 检查是否是多余的 ServiceID 行
|
||||
if re.match(r'^\s*"ServiceID":\s*"[^"]+",\s*$', line):
|
||||
# 跳过这一行
|
||||
print(f"第{i+1}行: 跳过多余的ServiceID行: {line.strip()}")
|
||||
i += 1
|
||||
continue
|
||||
|
||||
# 检查是否是格式错误的服务名行
|
||||
# 例如: "Amazon Streaming": { "ServiceID": "amazon_streaming",
|
||||
match = re.match(r'^\s*"([^"]+)":\s*\{\s*"ServiceID":\s*"([^"]+)",\s*$', line)
|
||||
if match:
|
||||
service_name = match.group(1)
|
||||
service_id = match.group(2)
|
||||
# 获取缩进
|
||||
indent_match = re.match(r'^(\s*)', line)
|
||||
indent = indent_match.group(1) if indent_match else ''
|
||||
# 修复这一行
|
||||
fixed_line = f'{indent}"{service_name}": {{'
|
||||
fixed_lines.append(fixed_line)
|
||||
print(f"第{i+1}行: 修复服务 '{service_name}'")
|
||||
i += 1
|
||||
continue
|
||||
|
||||
# 检查是否是空字符串键名
|
||||
if re.match(r'^\s*"":\s*\{', line):
|
||||
# 查找接下来的几行中的 Name 字段
|
||||
service_name = None
|
||||
for j in range(i + 1, min(i + 10, len(lines))):
|
||||
name_match = re.search(r'"Name":\s*"([^"]+)"', lines[j])
|
||||
if name_match:
|
||||
service_name = name_match.group(1)
|
||||
break
|
||||
|
||||
if service_name:
|
||||
# 获取缩进
|
||||
indent_match = re.match(r'^(\s*)', line)
|
||||
indent = indent_match.group(1) if indent_match else ''
|
||||
# 替换空字符串键名为服务名称
|
||||
fixed_lines.append(f'{indent}"{service_name}": {{')
|
||||
print(f"第{i+1}行: 修复空字符串键名为 '{service_name}'")
|
||||
i += 1
|
||||
continue
|
||||
|
||||
fixed_lines.append(line)
|
||||
i += 1
|
||||
|
||||
fixed_content = '\n'.join(fixed_lines)
|
||||
|
||||
with open('/root/dns/blocked-services-rules.json', 'w', encoding='utf-8') as f:
|
||||
f.write(fixed_content)
|
||||
|
||||
print("修复完成")
|
||||
Reference in New Issue
Block a user