← back to Secrets Manager

publish-pr-naturals-blog.py

52 lines

#!/usr/bin/env python3
"""
publish-pr-naturals-blog.py — CREATE a new DW blog article:
"Phillipe Romano Naturals: Grasscloth, Sisal & Paperweave Wallcoverings".

Body is taken from the audited draft (/tmp/dw-drafts/pr-naturals-DRAFT.html, scored 96/100).
The <h1> is stripped (the Shopify theme renders the title as the H1); the FAQPage JSON-LD is kept.
No feature image is set — the catalog images point to the source domain (private-label leak),
so a hero should be chosen from the Shopify media library on review.
Creates the article PUBLISHED. Run yourself:
    ! python3 ~/Projects/secrets-manager/publish-pr-naturals-blog.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

# --- pull the audited draft body, strip <head> and the <h1> ---
draft = open('/tmp/dw-drafts/pr-naturals-DRAFT.html').read()
body = re.search(r'<body>(.*)</body>', draft, re.S).group(1)
body = re.sub(r'<h1>.*?</h1>\s*', '', body, count=1, flags=re.S)  # theme renders the title as H1
body = body.strip()

TITLE = "Phillipe Romano Naturals: Grasscloth, Sisal & Paperweave Wallcoverings"
SEO_TITLE = "Phillipe Romano Naturals: Grasscloth, Sisal & Paperweave Wallcoverings | Designer Wallcoverings"
SEO_DESC = "Phillipe Romano Naturals: 53 patterns of grasscloth, sisal, paperweave, jute and mica wallcovering in coastal colorways. Order up to 10 free samples."

payload = {"article": {
    "title": TITLE,
    "author": "Designer Wallcoverings",
    "tags": "Phillipe Romano, Naturals, Grasscloth, Sisal, Paperweave, Jute, Mica, Wallcovering",
    "body_html": body,
    "published": True,
    "metafields": [
        {"namespace": "global", "key": "title_tag", "value": SEO_TITLE, "type": "single_line_text_field"},
        {"namespace": "global", "key": "description_tag", "value": SEO_DESC, "type": "single_line_text_field"},
    ],
}}

data = json.dumps(payload).encode()
req = urllib.request.Request(f"https://{STORE}/admin/api/{API}/blogs/{BLOG}/articles.json",
    data=data, method="POST",
    headers={"X-Shopify-Access-Token": tok, "Content-Type": "application/json"})
res = json.load(urllib.request.urlopen(req, timeout=30))['article']
print("CREATED OK  article id:", res['id'])
print("handle    :", res.get('handle'))
print("published :", res.get('published_at'))
print("live URL  : https://designerwallcoverings.com/blogs/news/" + str(res.get('handle')))
print("body chars:", len(res.get('body_html') or ''), "| faq schema:", 'FAQPage' in (res.get('body_html') or ''))