import json, os, re, urllib.request, time base = os.path.dirname(os.path.abspath(__file__)) out_root = os.path.join(base, "products") os.makedirs(out_root, exist_ok=True) def slugify(name): s = re.sub(r'[^a-zA-Z0-9]+', '-', name).strip('-').lower() return s[:60] catalogue = [] with open(os.path.join(base, "products.jsonl")) as f: for line in f: line = line.strip() if not line: continue catalogue.append(json.loads(line)) for i, p in enumerate(catalogue, 1): dirname = f"{i:02d}-{slugify(p['brand'])}-{slugify(p['name'])}" pdir = os.path.join(out_root, dirname) os.makedirs(pdir, exist_ok=True) with open(os.path.join(pdir, "info.json"), "w") as f: json.dump({"name": p["name"], "brand": p["brand"], "price_myr": p["price"], "source_code": p["code"]}, f, indent=2) for j, url in enumerate(p["imgs"], 1): dest = os.path.join(pdir, f"image-{j}.jpg") try: req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"}) with urllib.request.urlopen(req, timeout=15) as r, open(dest, "wb") as out: out.write(r.read()) except Exception as e: print(f"FAIL {url}: {e}") print(f"[{i}/{len(catalogue)}] {p['brand']} - {p['name']} -> {len(p['imgs'])} imgs")