69 lines
2.2 KiB
Python
69 lines
2.2 KiB
Python
import os
|
||
import json
|
||
from urllib.parse import urlparse, unquote
|
||
|
||
# === 配置路径 ===
|
||
input_json = "kenney_data.json"
|
||
output_json = "kenney_data_local.json"
|
||
|
||
zip_root = "kenney_assets"
|
||
img_root = "kenney_assets_images"
|
||
|
||
# === 工具函数 ===
|
||
|
||
|
||
def sanitize_filename(name):
|
||
return "".join(c for c in name if c.isalnum() or c in "._- ()").strip()
|
||
|
||
|
||
def build_zip_path(entry):
|
||
title = entry["title"]
|
||
version = entry["changelog"][0]["version"] if entry["changelog"] else "1.0"
|
||
category = entry["properties"].get("Category", ["Uncategorized"])[0]
|
||
series = entry["properties"].get("Series", [None])[0]
|
||
|
||
folder = os.path.join(zip_root, sanitize_filename(category))
|
||
if series:
|
||
folder = os.path.join(folder, sanitize_filename(series))
|
||
filename = f"{sanitize_filename(title)} V{version}.zip"
|
||
return os.path.join(folder, filename)
|
||
|
||
|
||
def build_image_paths(entry):
|
||
title = entry["title"]
|
||
category = entry["properties"].get("Category", ["Uncategorized"])[0]
|
||
series = entry["properties"].get("Series", [None])[0]
|
||
images = entry.get("images", [])
|
||
|
||
folder = os.path.join(img_root, sanitize_filename(category))
|
||
if series:
|
||
folder = os.path.join(folder, sanitize_filename(series))
|
||
folder = os.path.join(folder, sanitize_filename(title))
|
||
|
||
local_paths = []
|
||
for img_url in images:
|
||
parsed = urlparse(img_url)
|
||
filename = unquote(os.path.basename(parsed.path))
|
||
local_paths.append(os.path.join(folder, filename))
|
||
|
||
return local_paths
|
||
|
||
|
||
# === 主处理 ===
|
||
with open(input_json, "r", encoding="utf-8") as f:
|
||
data = json.load(f)
|
||
|
||
for entry in data:
|
||
if "download" in entry and entry["download"].endswith(".zip"):
|
||
zip_path = build_zip_path(entry)
|
||
if os.path.exists(zip_path):
|
||
entry["download"] = zip_path # 替换为本地路径
|
||
if "images" in entry and isinstance(entry["images"], list):
|
||
entry["images"] = build_image_paths(entry)
|
||
|
||
# === 保存修改后的 JSON ===
|
||
with open(output_json, "w", encoding="utf-8") as f:
|
||
json.dump(data, f, ensure_ascii=False, indent=2)
|
||
|
||
print("✅ 已更新 JSON:本地路径写入完毕!")
|