79 lines
3.1 KiB
Python
79 lines
3.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Generate the compact precision-gear catalog from the ecommerce product export."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
from pathlib import Path
|
|
|
|
|
|
DEFAULT_SOURCE = Path(
|
|
"/Users/jacky/Desktop/美好智玩/02_客户项目/高徳乐/商城产品资料_20260806/data/parsed_products.json"
|
|
)
|
|
DEFAULT_OUTPUT = Path("app/precision-gear-data.ts")
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--source", type=Path, default=DEFAULT_SOURCE)
|
|
parser.add_argument("--output", type=Path, default=DEFAULT_OUTPUT)
|
|
args = parser.parse_args()
|
|
|
|
source = json.loads(args.source.read_text(encoding="utf-8"))
|
|
images_root = args.source.parent.parent / "images"
|
|
items = [item for item in source.values() if item.get("top_cat") == "精密齿轮配件"]
|
|
items.sort(key=lambda item: (item.get("sub_cat", ""), int(item.get("id", 0))))
|
|
|
|
products = []
|
|
subcategory_counts: dict[str, int] = {}
|
|
for item in items:
|
|
subcategory = item.get("sub_cat", "未分类")
|
|
subcategory_counts[subcategory] = subcategory_counts.get(subcategory, 0) + 1
|
|
specs = {
|
|
str(label): str(value)
|
|
for label, value in item.get("specs", {}).items()
|
|
if str(value).strip()
|
|
}
|
|
product = {
|
|
"id": f"gear-{item['id']}",
|
|
"category": "精密齿轮配件",
|
|
"subCategory": subcategory,
|
|
"name": item.get("name", ""),
|
|
"code": item.get("code", ""),
|
|
"image": f"/catalog-products/precision-gears/{item['id']}.jpg",
|
|
"page": 0,
|
|
"specs": specs,
|
|
"torqueValue": None,
|
|
"ratedVoltage": None,
|
|
}
|
|
folders = sorted(images_root.glob(f"{item['id']}_*"))
|
|
dimension_files = []
|
|
if folders:
|
|
dimension_files = sorted(folders[0].glob("desc1.*"))
|
|
if not dimension_files:
|
|
dimension_files = sorted(folders[0].glob("slide2.*"))
|
|
if dimension_files:
|
|
product["dimensionImage"] = f"/catalog-products/precision-gears-size/{item['id']}.jpg"
|
|
products.append(product)
|
|
|
|
output = [
|
|
"// Generated from 商城产品资料_20260806/data/parsed_products.json.",
|
|
"// Run scripts/import_precision_gears.py after the source export changes.",
|
|
'import type { CatalogProduct } from "./catalog-data";',
|
|
"",
|
|
"export type PrecisionGearProduct = CatalogProduct & { subCategory: string };",
|
|
"",
|
|
f"export const PRECISION_GEAR_PRODUCTS: PrecisionGearProduct[] = {json.dumps(products, ensure_ascii=False, indent=2)};",
|
|
"",
|
|
f"export const PRECISION_GEAR_SUBCATEGORIES = {json.dumps([[name, count] for name, count in subcategory_counts.items()], ensure_ascii=False, indent=2)} as const;",
|
|
"",
|
|
]
|
|
args.output.parent.mkdir(parents=True, exist_ok=True)
|
|
args.output.write_text("\n".join(output), encoding="utf-8")
|
|
print(f"generated {len(products)} products and {len(subcategory_counts)} subcategories -> {args.output}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|