← back to Title Fix
reverse_a.py
19 lines
#!/usr/bin/env python3
"""Reverse title-fix A: restore old titles from data/restore-map-A.json. DRY-RUN default; live needs --apply + TITLEFIX_CONFIRM=YES."""
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")
live="--apply" in sys.argv and os.environ.get("TITLEFIX_CONFIRM")=="YES"
rows=json.load(open(os.path.join(HERE,"data","restore-map-A.json")))
print(f"=== reverse title-fix [{'LIVE' if live else 'DRY-RUN'}] — {len(rows)} ===")
tok=token() if live else None
for r in rows:
if not live: print(f"WOULD restore {r['num']} -> {r['old']!r}"); continue
req=urllib.request.Request(f"https://{SHOP}/admin/api/{API}/products/{r['num']}.json",
data=json.dumps({"product":{"id":int(r['num']),"title":r['old']}}).encode(),method="PUT",
headers={"X-Shopify-Access-Token":tok,"Content-Type":"application/json"})
urllib.request.urlopen(req,timeout=40); print(f"OK: {r['num']}"); time.sleep(0.4)