← back to Title Fix
fix_a.py
36 lines
#!/usr/bin/env python3
"""Title-fix Class A (46 no-real-name titles). PREP / DRY-RUN BY DEFAULT.
NOTE: these have NO recoverable real pattern name (never captured; no external key). This applies
CONSTRUCTED clean titles built from available data (Kravet=color, DW Bespoke=DIG-id [unique], MTG=strip):
better than the current broken titles, but NOT the true original names. Steve's call to apply.
DRY-RUN default; live = TITLEFIX_CONFIRM=YES python3 fix_a.py --apply [--canary N]. reverse_a.py undoes."""
import os,sys,json,time,urllib.request,urllib.error
HERE=os.path.dirname(os.path.abspath(__file__)); SHOP="designer-laboratory-sandbox.myshopify.com"; API="2024-10"
def token():
for l in open(os.path.expanduser("~/Projects/secrets-manager/.env")):
if l.startswith("SHOPIFY_ADMIN_TOKEN="): return l.split("=",1)[1].strip()
sys.exit("no token")
def main():
live="--apply" in sys.argv and os.environ.get("TITLEFIX_CONFIRM")=="YES"
if "--apply" in sys.argv and not live: print("REFUSING --apply without TITLEFIX_CONFIRM=YES. DRY-RUN.\n")
canary=int(sys.argv[sys.argv.index("--canary")+1]) if "--canary" in sys.argv else None
work=json.load(open(os.path.join(HERE,"data","worklist-A.json")))
if canary: work=work[:canary]
json.dump([{"num":w["num"],"old":w["old"]} for w in work],
open(os.path.join(HERE,"data","restore-map-A.json"),"w"),indent=0)
print(f"=== title-fix A [{'LIVE' if live else 'DRY-RUN'}] — {len(work)} CONSTRUCTED titles (not true names) ===\n")
tok=token() if live else None
done=err=0
for i,w in enumerate(work,1):
line=f"[{i}/{len(work)}] {w['num']} {w['vendor']}: {w['old']!r} -> {w['new']!r}"
if not live: print("WOULD:",line); continue
try:
req=urllib.request.Request(f"https://{SHOP}/admin/api/{API}/products/{w['num']}.json",
data=json.dumps({"product":{"id":int(w['num']),"title":w['new']}}).encode(),method="PUT",
headers={"X-Shopify-Access-Token":tok,"Content-Type":"application/json"})
urllib.request.urlopen(req,timeout=40); done+=1; print("OK:",line)
except urllib.error.HTTPError as e: err+=1; print(f"FAIL {w['num']} HTTP {e.code}")
time.sleep(0.4)
print(f"\n{'applied=%d failed=%d'%(done,err) if live else 'DRY-RUN complete. Nothing written.'}")
if __name__=="__main__": main()