← back to Kravet Fabric Fix 2026 04 23

resolve_missing_ids.py

68 lines

#!/usr/bin/env python3
"""Look up Shopify product IDs for DWKK rows flagged on_shopify but missing product_id.
Patches kravet_catalog, then re-runs the fix logic inline."""
import json, re, subprocess, sys, time, urllib.request
import importlib.util

spec = importlib.util.spec_from_file_location('fx',
    '/Users/macstudio3/Projects/kravet-fabric-fix-2026-04-23/fix_dwkk_misclassified.py')
fx = importlib.util.module_from_spec(spec); spec.loader.exec_module(fx)

SHOP, TOKEN, SSH = fx.SHOP, fx.TOKEN, fx.SSH
GQL = fx.GQL

LOOKUP = '''query($q: String!) {
  productVariants(first: 10, query: $q) {
    nodes { sku product { id title productType tags } }
  }
}'''

missing = []
with open('/Users/macstudio3/Projects/kravet-fabric-fix-2026-04-23/fix_results.jsonl') as f:
    for line in f:
        r = json.loads(line)
        if r['status'] == 'fetch_error':
            missing.append(r['dw_sku'])

# sheet_use lookup from CSV
import csv
uses = {}
with open('/Users/macstudio3/Projects/kravet-fabric-fix-2026-04-23/dwkk_misclassified_2026-04-23.csv') as f:
    for row in csv.DictReader(f):
        uses[row['dw_sku']] = row['sheet_use']

print(f'Resolving {len(missing)} missing IDs...')
resolved, still_missing, fixed = 0, 0, 0
out = open('/Users/macstudio3/Projects/kravet-fabric-fix-2026-04-23/resolve_results.jsonl', 'w')

for i, dw in enumerate(missing, 1):
    try:
        data = fx.gql(LOOKUP, {'q': f'sku:{dw}'})
    except Exception as e:
        out.write(json.dumps({'dw_sku': dw, 'status': 'lookup_error', 'error': str(e)}) + '\n')
        continue
    nodes = data['productVariants']['nodes']
    match = next((n for n in nodes if n['sku'] == dw), None)
    if not match:
        out.write(json.dumps({'dw_sku': dw, 'status': 'no_shopify_product'}) + '\n')
        still_missing += 1
        continue
    pid = match['product']['id']
    resolved += 1

    # Patch catalog id
    fx.pg_exec(f"UPDATE kravet_catalog SET shopify_product_id='{pid}' WHERE dw_sku='{dw}'")

    # Run the fix
    row = {'dw_sku': dw, 'shopify_product_id': pid, 'sheet_use': uses.get(dw, 'MULTIPURPOSE')}
    result = fx.fix_one(row)
    out.write(json.dumps(result) + '\n'); out.flush()
    if result['status'] == 'ok':
        fixed += 1
    if i % 50 == 0:
        print(f'  [{i}/{len(missing)}] resolved={resolved} fixed={fixed} still_missing={still_missing}')
    time.sleep(0.12)

out.close()
print(f'Done: resolved={resolved} fixed={fixed} still_missing={still_missing}')