← back to Secrets Manager

fix-pr-naturals-schema.py

37 lines

#!/usr/bin/env python3
"""
fix-pr-naturals-schema.py — append the FAQPage JSON-LD to the already-published
"Phillipe Romano Naturals" article (id 568551243827). The create-script sent only the
<body>, and the schema had been placed in the <head>, so the live article has the visible
FAQ but not the structured data. This PUTs the schema onto the end of the body.
Run:  ! python3 ~/Projects/secrets-manager/fix-pr-naturals-schema.py
"""
import re, json, urllib.request

env = open('/Users/macstudio3/Projects/secrets-manager/.env').read()
m = re.search(r'^SHOPIFY_CONTENT_TOKEN=(.+)$', env, re.M) or re.search(r'^SHOPIFY_DRAFT_TOKEN=(.+)$', env, re.M)
tok = m.group(1).strip().strip('"').strip("'")
STORE = "designer-laboratory-sandbox.myshopify.com"; API = "2024-10"; BLOG = 9243689072; ART = 568551243827

def get(p):
    r = urllib.request.Request(f"https://{STORE}/admin/api/{API}/{p}", headers={"X-Shopify-Access-Token": tok})
    return json.load(urllib.request.urlopen(r, timeout=30))
def put(p, payload):
    r = urllib.request.Request(f"https://{STORE}/admin/api/{API}/{p}", data=json.dumps(payload).encode(),
        method="PUT", headers={"X-Shopify-Access-Token": tok, "Content-Type": "application/json"})
    return json.load(urllib.request.urlopen(r, timeout=30))

faq = json.load(open('/tmp/pr-naturals-faq.json'))['faq']
schema = {"@context": "https://schema.org", "@type": "FAQPage",
          "mainEntity": [{"@type": "Question", "name": q["q"],
                          "acceptedAnswer": {"@type": "Answer", "text": q["a"]}} for q in faq]}
schema_html = '\n<script type="application/ld+json">\n' + json.dumps(schema, ensure_ascii=False, indent=2) + '\n</script>'

art = get(f"blogs/{BLOG}/articles/{ART}.json")['article']
body = art['body_html'] or ''
if 'FAQPage' in body:
    print("schema already present — no change"); raise SystemExit(0)
res = put(f"blogs/{BLOG}/articles/{ART}.json", {"article": {"id": ART, "body_html": body + schema_html}})['article']
nb = res['body_html'] or ''
print("FIXED OK  body chars:", len(nb), "| faq schema now:", 'FAQPage' in nb)