← back to Novasuede Onboard

verify_live.py

45 lines

#!/usr/bin/env python3
"""Verify enrichment landed on live Shopify products. Pulls fresh from Shopify."""
import json, re, urllib.request, pathlib, sys

env = pathlib.Path.home()/"Projects/secrets-manager/.env"
TOKEN = next(l.split("=",1)[1].strip().strip('"') for l in env.read_text().splitlines()
             if l.startswith("SHOPIFY_ADMIN_TOKEN="))
API = "https://designer-laboratory-sandbox.myshopify.com/admin/api/2024-01"
ROOT = pathlib.Path(__file__).resolve().parent

STYLES = {"Organic Modern","Desert Modern","Bohemian","Hollywood Regency","Art Deco","Glam",
          "Mediterranean","Mid-Century Modern","Transitional","Scandinavian","Minimalist",
          "Japandi","Contemporary","Industrial","Coastal","Traditional","Eclectic","Maximalist","Biophilic"}

def get(pid):
    r = urllib.request.Request(f"{API}/products/{pid}.json?fields=id,title,tags,body_html",
                               headers={"X-Shopify-Access-Token": TOKEN})
    with urllib.request.urlopen(r, timeout=30) as resp:
        return json.load(resp)["product"]

idx = {r["vendor_name"]: r["id"] for r in
       (json.loads(l) for l in (ROOT/"data/active_index.ndjson").read_text().splitlines())}

# verify a representative spread, including QA-fixed names
want = sys.argv[1:] or ["Acorn","African Violet","Aubergine","Baltic","Ocean","China Blue","Abyss"]
miss_lex = 0; old_left = 0
for name in want:
    pid = idx.get(name)
    if not pid:
        print(f"? {name}: not in index"); continue
    p = get(pid)
    tags = [t.strip() for t in p["tags"].split(",")]
    col = [t for t in tags if t.startswith("Color:")]
    sty = [t for t in tags if t in STYLES]
    lex = "AI-Lexicon-v3" in tags
    oldmark = "AI-Analyzed-v2" in tags
    if not lex: miss_lex += 1
    if oldmark: old_left += 1
    body = re.sub("<[^>]+>", " ", p["body_html"]).strip()
    print(f"\n# {p['title']}")
    print(f"  {col}  +  styles={sty}")
    print(f"  AI-Lexicon-v3={lex}  AI-Analyzed-v2-still-present={oldmark}  tagcount={len(tags)}")
    print(f"  body: {body[:150]}")
print(f"\n=== {len(want)} checked | missing v3 marker: {miss_lex} | stale v2 marker left: {old_left} ===")