← back to Secrets Manager
halve-banner.py
47 lines
#!/usr/bin/env python3
"""
halve-banner.py — halve the collection-hero banner min-height on each run. Reads the
current `min-height:clamp(Apx,Bvw,Cpx)` in the live theme's dw-collection-hero.liquid
and rewrites it to half of A/B/C. Idempotent-safe (just halves whatever is there);
run again to halve further. Backs up first.
Run: ! python3 ~/Projects/secrets-manager/halve-banner.py # dry-run (shows old->new)
! python3 ~/Projects/secrets-manager/halve-banner.py --apply
"""
import re, json, sys, urllib.request
APPLY = '--apply' in sys.argv
env = open('/Users/macstudio3/Projects/secrets-manager/.env').read()
tok = re.search(r'^SHOPIFY_THEME_TOKEN=(.+)$', env, re.M).group(1).strip().strip('"').strip("'")
S = "designer-laboratory-sandbox.myshopify.com"; A = "2024-10"; TID = 144396058675
KEY = "sections/dw-collection-hero.liquid"
def get_asset():
r = urllib.request.Request(f"https://{S}/admin/api/{A}/themes/{TID}/assets.json?asset[key]={KEY}",
headers={"X-Shopify-Access-Token": tok})
return json.load(urllib.request.urlopen(r, timeout=30))['asset']['value']
def put_asset(v):
b = json.dumps({"asset": {"key": KEY, "value": v}}).encode()
r = urllib.request.Request(f"https://{S}/admin/api/{A}/themes/{TID}/assets.json", data=b,
method="PUT", headers={"X-Shopify-Access-Token": tok, "Content-Type": "application/json"})
return json.load(urllib.request.urlopen(r, timeout=30))
v = get_asset()
m = re.search(r'min-height:clamp\(\s*([\d.]+)px\s*,\s*([\d.]+)vw\s*,\s*([\d.]+)px\s*\)', v)
if not m:
print("ABORT: no min-height:clamp(...) found"); sys.exit(1)
a, b, c = (float(x) for x in m.groups())
na, nb, nc = round(a/2, 1), round(b/2, 2), round(c/2, 1)
old = m.group(0)
new = f"min-height:clamp({na:g}px,{nb:g}vw,{nc:g}px)"
print(f"banner min-height: {old.split('clamp')[1]} -> clamp({na:g}px,{nb:g}vw,{nc:g}px)")
if not APPLY:
print("DRY-RUN — re-run with --apply to write."); sys.exit(0)
backup = "/Users/macstudio3/Projects/secrets-manager/greenland-cleanup-backups/dw-collection-hero-BACKUP-banner.liquid"
open(backup, "w").write(v)
res = put_asset(v.replace(old, new, 1))
print("PUT:", "OK" if 'asset' in res else json.dumps(res)[:150])
print(f"rollback: re-PUT {backup}")
print("Look (hard-refresh): https://designerwallcoverings.com/collections/grasscloth-wallpaper-collection")