← back to Secrets Manager
hero-polish.py
31 lines
import re,json,sys,urllib.request
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"
APPLY='--apply' in sys.argv
def g():
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 p(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=g()
reps=[
# 1 · trim intro to <=120 chars
("for the interior design trade — {{ collection.products_count }} designs, with samples and trade pricing on request.",
"for the design trade — {{ collection.products_count }} designs, samples and trade pricing on request."),
# 2 · centered, visible "Learn about" pill
('<details class="dw-collection-faq" style="max-width:900px;margin:.75rem auto 0;padding:0 1rem;"><summary style="cursor:pointer;font:500 .95rem/1.4 inherit;letter-spacing:.02em;opacity:.75;">',
'<details class="dw-collection-faq" style="max-width:900px;margin:1rem auto .5rem;text-align:center;"><summary style="cursor:pointer;display:inline-block;list-style:none;font:600 .8rem/1.5 inherit;letter-spacing:.08em;text-transform:uppercase;padding:.55rem 1.5rem;border:1px solid currentColor;border-radius:2px;opacity:.9;">'),
('<div style="padding:.5rem 0 .25rem;">{%- for item in aeo_faq -%}',
'<div style="text-align:left;max-width:760px;margin:.9rem auto 0;padding:0 1rem;">{%- for item in aeo_faq -%}'),
]
miss=[o[:45] for o,_ in reps if o not in v]
if miss: print("already applied or not found:",miss); sys.exit(0 if len(miss)==len(reps) else 1)
if not APPLY:
print(f"DRY: {len(reps)} edits located ✓ (intro trim + centered/visible pill)"); sys.exit(0)
nv=v
for o,n in reps: nv=nv.replace(o,n,1)
print("PUT:", "OK" if 'asset' in p(nv) else "FAIL")