import os, requests, time
AV_KEY = os.environ["ALPHA_VANTAGE_KEY"]
AV_BASE = "https://www.alphavantage.co/query"
MV = os.environ["MAVERA_API_KEY"]
MV_BASE = "https://app.mavera.io/api/v1"
MV_H = {"Authorization": f"Bearer {MV}", "Content-Type": "application/json"}
PRODUCT = "marketing analytics platform"
# 1. Fetch sector performance
r = requests.get(AV_BASE, params={"function": "SECTOR", "apikey": AV_KEY})
r.raise_for_status()
sector_data = r.json()
timeframes = {
"1 Day": "Rank A: Real-Time Performance",
"1 Month": "Rank D: Month Performance",
"3 Months": "Rank E: Quarter Performance",
"1 Year": "Rank G: Year Performance",
}
sector_summary = []
for label, key in timeframes.items():
data = sector_data.get(key, {})
sorted_sectors = sorted(data.items(), key=lambda x: float(x[1].replace("%","")), reverse=True)
sector_summary.append(f"\n**{label}:**")
for sector, perf in sorted_sectors:
sector_summary.append(f" {sector}: {perf}")
performance_text = "\n".join(sector_summary)
print(f"Sector data retrieved across {len(timeframes)} timeframes")
# 2. Mave prioritization
priority = requests.post(f"{MV_BASE}/mave/chat", headers=MV_H, json={
"message": f"Marketing strategist. We sell a {PRODUCT}.\n\n"
f"SECTOR PERFORMANCE:\n{performance_text}\n\n"
"Create a VERTICAL PRIORITIZATION MATRIX:\n\n"
"For each sector:\n"
"1. **Momentum Score** (1-10) — based on recent vs long-term performance\n"
"2. **Marketing Investment** — Increase / Maintain / Decrease\n"
"3. **Messaging Strategy** — Growth narrative vs efficiency narrative vs stability narrative\n"
"4. **Key Angle** — One sentence positioning for this vertical\n"
"5. **Budget Recommendation** — % of total marketing budget\n\n"
"Rank by priority. Top 3 get detailed campaign angles."
}).json()
print(f"\n{priority.get('content', '')[:2000]}")
# 3. Generate copy for top sector
gen = requests.post(f"{MV_BASE}/generations", headers=MV_H, json={
"prompt": f"Based on this vertical prioritization:\n\n{priority.get('content','')[:2000]}\n\n"
f"Generate campaign copy for the TOP-RANKED sector targeting our {PRODUCT}.\n\n"
"Include:\n"
"1. Landing page headline + subheadline\n"
"2. Three value propositions (sector-specific)\n"
"3. Email subject line + preview text for outbound\n"
"4. LinkedIn ad (headline + body + CTA)\n"
"5. Case study headline template\n\n"
"Reference sector momentum. Make the copy feel industry-native.",
}).json()
print(f"\n{'='*60}\nTOP SECTOR CAMPAIGN COPY\n{'='*60}")
print(gen.get("output", gen.get("content", ""))[:1500])