← back to Japan Enrich

build_onboarding_manifest.py

53 lines

#!/usr/bin/env python3
"""Build the internal-line onboarding manifest: distributor → collections (patterns) → colorway SKUs.
Fully OFFLINE — derived from staging/sangetsu-staging.jsonl + staging/sangetsu-brands.json (the scraped
per-pattern distributor). No network, no API, $0. Output: staging/onboarding-manifest.json — the shape
the (gated) canonical dw_unified internal-line load + the per-distributor microsites consume."""
import json, os, re, collections

HERE = os.path.dirname(__file__)
STAGING = os.path.join(HERE, 'staging', 'sangetsu-staging.jsonl')
BRANDS = os.path.join(HERE, 'staging', 'sangetsu-brands.json')
OUT = os.path.join(HERE, 'staging', 'onboarding-manifest.json')

def slugify(s):
    return re.sub(r'[^a-z0-9]+', '-', (s or '').lower()).strip('-')

def category_of(url):
    m = re.search(r'/wallpaper-wallcovering/([^/]+)/', url or '')
    return m.group(1) if m else None

def main():
    brands = json.load(open(BRANDS))
    lines = collections.OrderedDict()
    for line in open(STAGING):
        line = line.strip()
        if not line: continue
        r = json.loads(line)
        u = r.get('source_url')
        bd = brands.get(u) or {}
        dist = bd.get('distributor')
        if not dist:  # unbranded patterns stay out of a distributor line (honest)
            dist = '(unassigned)'
        L = lines.setdefault(dist, {'slug': bd.get('slug') or slugify(dist),
                                    'collections': [], 'pattern_count': 0, 'colorway_count': 0})
        skus = r.get('colorway_skus') or []
        L['collections'].append({
            'pattern': r.get('pattern') or u,
            'category': category_of(u),
            'url': u,
            'sku_count': len(skus),
            'skus': skus,
        })
        L['pattern_count'] += 1
        L['colorway_count'] += len(skus)
    json.dump(lines, open(OUT, 'w'), ensure_ascii=False, indent=1)
    print(f"manifest → {OUT}")
    print(f"distributors (lines): {len(lines)}")
    for d, L in sorted(lines.items(), key=lambda x: -x[1]['colorway_count']):
        cats = sorted({c['category'] for c in L['collections'] if c['category']})
        print(f"  {d:24s} {L['pattern_count']:3d} collections / {L['colorway_count']:5d} SKUs  · categories: {', '.join(cats)}")

if __name__ == '__main__':
    main()