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"}
# 1. Fetch economic indicators
indicators = {}
for func, label in [("REAL_GDP", "GDP"), ("CPI", "CPI"), ("UNEMPLOYMENT", "Unemployment")]:
r = requests.get(AV_BASE, params={"function": func, "interval": "quarterly" if func == "REAL_GDP" else "monthly", "apikey": AV_KEY})
r.raise_for_status()
data = r.json().get("data", [])
recent = data[:4]
indicators[label] = {
"latest": recent[0] if recent else {},
"trend": [{"date": d.get("date",""), "value": d.get("value","")} for d in recent],
}
latest = recent[0] if recent else {}
print(f"{label}: {latest.get('value','N/A')} ({latest.get('date','N/A')})")
time.sleep(15)
# 2. Build economic snapshot
snapshot = "\n".join(
f"**{label}**: {d['latest'].get('value','N/A')} (as of {d['latest'].get('date','N/A')})\n"
f" Trend: {' → '.join(t['value'] for t in reversed(d['trend']))}"
for label, d in indicators.items()
)
# 3. Create demographic personas
DEMOGRAPHICS = [
{"name": "Retiree on Fixed Income", "desc": "Age 68. Pension + Social Security. $52K/year. Inflation directly erodes purchasing power. Risk-averse. Watches news daily."},
{"name": "Recent College Graduate", "desc": "Age 23. First job, $55K salary. $38K student debt. Optimistic but financially stressed. Rents. No investments yet."},
{"name": "Small Business Owner", "desc": "Age 42. Restaurant owner, 12 employees. Revenue $1.2M. Margins thin (8%). Labor costs and food costs top concerns."},
{"name": "Dual-Income Suburban Family", "desc": "Ages 36/38. Combined $180K. Mortgage, two kids, saving for college. Feel upper-middle but stretched. Inflation hits grocery and childcare."},
]
persona_ids = []
for demo in DEMOGRAPHICS:
p = requests.post(f"{MV_BASE}/personas", headers=MV_H, json={
"name": f"Econ: {demo['name']}", "description": demo["desc"],
}).json()
persona_ids.append(p["id"])
time.sleep(0.3)
# 4. Focus Group
fg = requests.post(f"{MV_BASE}/focus-groups", headers=MV_H, json={
"name": "Economic Indicator Reactions",
"persona_ids": persona_ids,
"questions": [
f"Here are the latest economic numbers:\n{snapshot}\n\nHow do these numbers make you feel about your financial situation?",
"Based on these numbers, will you spend more, less, or the same in the next 3 months? On what?",
"If a financial services company approached you right now, what message would resonate?",
"What's the one economic indicator you personally watch most? Why?",
],
"responses_per_persona": 2,
}).json()
for _ in range(25):
time.sleep(5)
data = requests.get(f"{MV_BASE}/focus-groups/{fg['id']}", headers=MV_H).json()
if data.get("status") == "completed":
break
# 5. Print responses
print(f"\n{'='*60}\nECONOMIC FOCUS GROUP RESULTS\n{'='*60}")
print(f"Indicators: {snapshot}\n")
for resp in data.get("responses", []):
pid = resp.get("persona_id", "")
idx = persona_ids.index(pid) if pid in persona_ids else -1
name = DEMOGRAPHICS[idx]["name"] if 0 <= idx < len(DEMOGRAPHICS) else "Unknown"
print(f"[{name}] Q: {resp.get('question','')[:80]}")
print(f" A: {resp.get('answer','')[:350]}\n")