← back to Designerwallcoverings
scripts/af-reimage/republish.py
24 lines
#!/usr/bin/env python3
"""Republish the 23 re-imaged Anna French products DRAFT->ACTIVE. GATED: needs --apply.
Idempotent: skips products that aren't draft or have no image (never ACTIVE-without-image).
Source images were attached from Thibaut's trade-auth CDN (d2g31xe1...cloudfront/products/2520x2520/<sku>.jpg).
"""
import json,urllib.request,sys,os,time
APPLY='--apply' in sys.argv
SEC=os.path.expanduser('~/Projects/secrets-manager/.env')
TOKEN=next((l.split('=',1)[1].strip().strip('"\'') for l in open(SEC) if l.upper().startswith('SHOPIFY_ADMIN_TOKEN=')),None)
STORE='designer-laboratory-sandbox.myshopify.com'
ready=json.load(open(os.path.join(os.path.dirname(__file__),'af23-republish-ready.json')))
print(f"{'APPLY' if APPLY else 'DRY-RUN'} · {len(ready)} Anna French products draft->active")
flipped=0
for r in ready:
pid=r['pid']
p=json.load(urllib.request.urlopen(urllib.request.Request(f"https://{STORE}/admin/api/2024-04/products/{pid}.json?fields=id,status,images",headers={'X-Shopify-Access-Token':TOKEN}),timeout=20))['product']
if p['status']=='active': print(f" ~ {r['sku']} already active"); continue
if len(p['images'])==0: print(f" ! {r['sku']} has NO image — refusing to activate"); continue
if not APPLY: print(f" • would activate {r['sku']} ({r['title']})"); continue
body=json.dumps({"product":{"id":int(pid),"status":"active"}}).encode()
req=urllib.request.Request(f"https://{STORE}/admin/api/2024-04/products/{pid}.json",data=body,method='PUT',headers={'X-Shopify-Access-Token':TOKEN,'Content-Type':'application/json'})
json.load(urllib.request.urlopen(req,timeout=30)); flipped+=1; print(f" ✓ activated {r['sku']}"); time.sleep(0.5)
print(f"done. {'activated '+str(flipped) if APPLY else 'dry-run, nothing changed'}")