import os, requests, time
STORE = os.environ["SHOPIFY_STORE"]
TOKEN = os.environ["SHOPIFY_ACCESS_TOKEN"]
MV = os.environ["MAVERA_API_KEY"]
SH = f"https://{STORE}.myshopify.com/admin/api/2024-10/graphql.json"
SH_H = {"X-Shopify-Access-Token": TOKEN, "Content-Type": "application/json"}
MB = "https://app.mavera.io/api/v1"
MH = {"Authorization": f"Bearer {MV}", "Content-Type": "application/json"}
QUERY = """{
productVariants(first: 100) {
edges { node {
title product { title productType priceRangeV2 { minVariantPrice { amount currencyCode } } }
inventoryItem { inventoryLevels(first: 5) { edges { node { available } } } }
}}
}
}"""
resp = requests.post(SH, json={"query": QUERY}, headers=SH_H)
resp.raise_for_status()
variants = []
for e in resp.json()["data"]["productVariants"]["edges"]:
n = e["node"]
stock = sum(l["node"]["available"] for l in n["inventoryItem"]["inventoryLevels"]["edges"])
if stock >= 50:
vel = max(1, stock // 48)
if vel <= 2:
variants.append({"product": n["product"]["title"], "variant": n["title"], "type": n["product"]["productType"],
"price": n["product"]["priceRangeV2"]["minVariantPrice"]["amount"],
"currency": n["product"]["priceRangeV2"]["minVariantPrice"]["currencyCode"],
"stock": stock, "velocity": vel, "weeks": stock // max(vel, 1)})
variants.sort(key=lambda x: -x["stock"])
print(f"{len(variants)} priority items")
for v in variants[:5]:
gen = requests.post(f"{MB}/generations", json={"prompt":
f"Product needing sales push:\n{v['product']} — {v['variant']}\nType: {v['type']}\nPrice: {v['price']} {v['currency']}\nStock: {v['stock']} units, ~{v['velocity']}/wk\n\nWrite: 1) Urgency description (2 sentences) 2) Social ad with CTA 3) Flash sale email subject 4) Banner headline"
}, headers=MH)
gen.raise_for_status()
print(f"\n--- {v['product']} ({v['variant']}) — {v['stock']} units ---\n{gen.json()['text']}")
time.sleep(0.3)