← back to Schumacher Restore

restore_reverse.py

31 lines

#!/usr/bin/env python3
"""
Reverse a Schumacher restore batch: flip each product in data/restore-map.json back to its
prev_status (archived). DRY-RUN by default; live requires --apply + SCHU_RESTORE_CONFIRM=YES.
Note: does NOT remove re-applied images (harmless if left; delete manually if needed).
"""
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("SCHU_RESTORE_CONFIRM")=="YES"
    rows = json.load(open(os.path.join(HERE,"data","restore-map.json")))
    print(f"=== reverse [{'LIVE' if live else 'DRY-RUN'}] — {len(rows)} products ===")
    tok = token() if live else None
    for r in rows:
        prev = r["prev_status"].lower()
        if not live:
            print(f"WOULD: product {r['num']} -> {prev}"); continue
        try:
            req=urllib.request.Request(f"https://{SHOP}/admin/api/{API}/products/{r['num']}.json",
                data=json.dumps({"product":{"id":int(r['num']),"status":prev}}).encode(),
                method="PUT", headers={"X-Shopify-Access-Token":tok,"Content-Type":"application/json"})
            urllib.request.urlopen(req,timeout=40); print(f"OK: {r['num']} -> {prev}")
        except urllib.error.HTTPError as e: print(f"FAIL {r['num']} -> HTTP {e.code}")
        time.sleep(0.5)
if __name__=="__main__": main()